From a68235190ab6325fd097fcd3fada779af20795b3 Mon Sep 17 00:00:00 2001 From: NGPixel Date: Fri, 28 Apr 2017 18:46:27 -0400 Subject: [PATCH] fix: Fixed logger filters --- server/agent.js | 17 ++++++++-------- server/index.js | 15 +++++++-------- server/libs/git.js | 24 +++++++++++------------ server/libs/local.js | 4 ++-- server/libs/logger.js | 45 ++++++++++++++++++++++++------------------- server/libs/search.js | 6 +++--- 6 files changed, 57 insertions(+), 54 deletions(-) diff --git a/server/agent.js b/server/agent.js index cb5495f3..c38d7aae 100644 --- a/server/agent.js +++ b/server/agent.js @@ -8,10 +8,9 @@ const path = require('path') const ROOTPATH = process.cwd() const SERVERPATH = path.join(ROOTPATH, 'server') -global.PROCNAME = 'AGENT' global.ROOTPATH = ROOTPATH global.SERVERPATH = SERVERPATH -global.IS_DEBUG = process.env.NODE_ENV === 'development' +const IS_DEBUG = process.env.NODE_ENV === 'development' let appconf = require('./libs/config')() global.appconfig = appconf.config @@ -21,13 +20,13 @@ global.appdata = appconf.data // Load Winston // ---------------------------------------- -global.winston = require('./libs/logger')(IS_DEBUG) +global.winston = require('./libs/logger')(IS_DEBUG, 'AGENT') // ---------------------------------------- // Load global modules // ---------------------------------------- -winston.info('[AGENT] Background Agent is initializing...') +winston.info('Background Agent is initializing...') global.db = require('./libs/db').init() global.upl = require('./libs/uploads-agent').init() @@ -64,10 +63,10 @@ db.onReady.then(() => { // Make sure we don't start two concurrent jobs if (jobIsBusy) { - winston.warn('[AGENT] Previous job has not completed gracefully or is still running! Skipping for now. (This is not normal, you should investigate)') + winston.warn('Previous job has not completed gracefully or is still running! Skipping for now. (This is not normal, you should investigate)') return } - winston.info('[AGENT] Running all jobs...') + winston.info('Running all jobs...') jobIsBusy = true // Prepare async job collector @@ -165,7 +164,7 @@ db.onReady.then(() => { // ---------------------------------------- Promise.all(jobs).then(() => { - winston.info('[AGENT] All jobs completed successfully! Going to sleep for now.') + winston.info('All jobs completed successfully! Going to sleep for now.') if (!jobUplWatchStarted) { jobUplWatchStarted = true @@ -176,7 +175,7 @@ db.onReady.then(() => { return true }).catch((err) => { - winston.error('[AGENT] One or more jobs have failed: ', err) + winston.error('One or more jobs have failed: ', err) }).finally(() => { jobIsBusy = false }) @@ -192,7 +191,7 @@ db.onReady.then(() => { // ---------------------------------------- process.on('disconnect', () => { - winston.warn('[AGENT] Lost connection to main server. Exiting...') + winston.warn('Lost connection to main server. Exiting...') job.stop() process.exit() }) diff --git a/server/index.js b/server/index.js index 31288d35..d7a13945 100644 --- a/server/index.js +++ b/server/index.js @@ -10,10 +10,9 @@ const path = require('path') const ROOTPATH = process.cwd() const SERVERPATH = path.join(ROOTPATH, 'server') -global.PROCNAME = 'SERVER' global.ROOTPATH = ROOTPATH global.SERVERPATH = SERVERPATH -global.IS_DEBUG = process.env.NODE_ENV === 'development' +const IS_DEBUG = process.env.NODE_ENV === 'development' process.env.VIPS_WARNING = false @@ -25,8 +24,8 @@ global.appdata = appconf.data // Load Winston // ---------------------------------------- -global.winston = require('./libs/logger')(IS_DEBUG) -winston.info('[SERVER] Wiki.js is initializing...') +global.winston = require('./libs/logger')(IS_DEBUG, 'SERVER') +winston.info('Wiki.js is initializing...') // ---------------------------------------- // Load global modules @@ -188,7 +187,7 @@ app.use(function (err, req, res, next) { // Start HTTP server // ---------------------------------------- -winston.info('[SERVER] Starting HTTP/WS server on port ' + appconfig.port + '...') +winston.info('Starting HTTP/WS server on port ' + appconfig.port + '...') app.set('port', appconfig.port) var server = http.createServer(app) @@ -203,10 +202,10 @@ server.on('error', (error) => { // handle specific listen errors with friendly messages switch (error.code) { case 'EACCES': - console.error('Listening on port ' + appconfig.port + ' requires elevated privileges!') + winston.error('Listening on port ' + appconfig.port + ' requires elevated privileges!') return process.exit(1) case 'EADDRINUSE': - console.error('Port ' + appconfig.port + ' is already in use!') + winston.error('Port ' + appconfig.port + ' is already in use!') return process.exit(1) default: throw error @@ -214,7 +213,7 @@ server.on('error', (error) => { }) server.on('listening', () => { - winston.info('[SERVER] HTTP/WS server started successfully! [RUNNING]') + winston.info('HTTP/WS server started successfully! [RUNNING]') }) // ---------------------------------------- diff --git a/server/libs/git.js b/server/libs/git.js index ab78489b..72223855 100644 --- a/server/libs/git.js +++ b/server/libs/git.js @@ -66,13 +66,13 @@ module.exports = { _initRepo (appconfig) { let self = this - winston.info('[' + PROCNAME + '.Git] Checking Git repository...') + winston.info('Checking Git repository...') // -> Check if path is accessible return fs.mkdirAsync(self._repo.path).catch((err) => { if (err.code !== 'EEXIST') { - winston.error('[' + PROCNAME + '.Git] Invalid Git repository path or missing permissions.') + winston.error('Invalid Git repository path or missing permissions.') } }).then(() => { self._git = new Git({ 'git-dir': self._repo.path }) @@ -87,7 +87,7 @@ module.exports = { }) }).then(() => { if (appconfig.git === false) { - winston.info('[' + PROCNAME + '.Git] Remote syncing is disabled. Not recommended!') + winston.info('Remote Git syncing is disabled. Not recommended!') return Promise.resolve(true) } @@ -124,10 +124,10 @@ module.exports = { }) }) }).catch((err) => { - winston.error('[' + PROCNAME + '.Git] Git remote error!') + winston.error('Git remote error!') throw err }).then(() => { - winston.info('[' + PROCNAME + '.Git] Git repository is OK.') + winston.info('Git repository is OK.') return true }) }, @@ -157,12 +157,12 @@ module.exports = { // Fetch - winston.info('[' + PROCNAME + '.Git] Performing pull from remote repository...') + winston.info('Performing pull from remote Git repository...') return self._git.pull('origin', self._repo.branch).then((cProc) => { - winston.info('[' + PROCNAME + '.Git] Pull completed.') + winston.info('Git Pull completed.') }) .catch((err) => { - winston.error('[' + PROCNAME + '.Git] Unable to fetch from git origin!') + winston.error('Unable to fetch from git origin!') throw err }) .then(() => { @@ -172,19 +172,19 @@ module.exports = { let out = cProc.stdout.toString() if (_.includes(out, 'commit')) { - winston.info('[' + PROCNAME + '.Git] Performing push to remote repository...') + winston.info('Performing push to remote Git repository...') return self._git.push('origin', self._repo.branch).then(() => { - return winston.info('[' + PROCNAME + '.Git] Push completed.') + return winston.info('Git Push completed.') }) } else { - winston.info('[' + PROCNAME + '.Git] Push skipped. Repository is already in sync.') + winston.info('Git Push skipped. Repository is already in sync.') } return true }) }) .catch((err) => { - winston.error('[' + PROCNAME + '.Git] Unable to push changes to remote!') + winston.error('Unable to push changes to remote Git repository!') throw err }) }, diff --git a/server/libs/local.js b/server/libs/local.js index 7c48606d..e624d23a 100644 --- a/server/libs/local.js +++ b/server/libs/local.js @@ -98,7 +98,7 @@ module.exports = { * @return {Void} Void */ createBaseDirectories (appconfig) { - winston.info('[SERVER.Local] Checking data directories...') + winston.info('Checking data directories...') try { fs.ensureDirSync(path.resolve(ROOTPATH, appconfig.paths.data)) @@ -121,7 +121,7 @@ module.exports = { winston.error(err) } - winston.info('[SERVER.Local] Data and Repository directories are OK.') + winston.info('Data and Repository directories are OK.') }, /** diff --git a/server/libs/logger.js b/server/libs/logger.js index 3908ed59..4a6f7884 100644 --- a/server/libs/logger.js +++ b/server/libs/logger.js @@ -1,31 +1,36 @@ 'use strict' -const winston = require('winston') +module.exports = (isDebug, processName) => { + let winston = require('winston') -module.exports = (isDebug) => { - if (typeof PROCNAME === 'undefined') { - const PROCNAME = 'SERVER' // eslint-disable-line no-unused-vars + if (typeof processName === 'undefined') { + processName = 'SERVER' } - // Console + File Logs + // Console - winston.remove(winston.transports.Console) - winston.add(winston.transports.Console, { + let logger = new (winston.Logger)({ level: (isDebug) ? 'debug' : 'info', - prettyPrint: true, - colorize: true, - silent: false, - timestamp: true, - filters: [(level, msg, meta) => { - return '[' + PROCNAME + '] ' + msg // eslint-disable-line no-undef - }] + transports: [ + new (winston.transports.Console)({ + level: (isDebug) ? 'debug' : 'info', + prettyPrint: true, + colorize: true, + silent: false, + timestamp: true + }) + ] + }) + + logger.filters.push((level, msg) => { + return '[' + processName + '] ' + msg }) // External services if (appconfig.externalLogging.bugsnag) { const bugsnagTransport = require('./winston-transports/bugsnag') - winston.add(bugsnagTransport, { + logger.add(bugsnagTransport, { level: 'warn', key: appconfig.externalLogging.bugsnag }) @@ -33,7 +38,7 @@ module.exports = (isDebug) => { if (appconfig.externalLogging.loggly) { require('winston-loggly-bulk') - winston.add(winston.transports.Loggly, { + logger.add(winston.transports.Loggly, { token: appconfig.externalLogging.loggly.token, subdomain: appconfig.externalLogging.loggly.subdomain, tags: ['wiki-js'], @@ -44,7 +49,7 @@ module.exports = (isDebug) => { if (appconfig.externalLogging.papertrail) { require('winston-papertrail').Papertrail // eslint-disable-line no-unused-expressions - winston.add(winston.transports.Papertrail, { + logger.add(winston.transports.Papertrail, { host: appconfig.externalLogging.papertrail.host, port: appconfig.externalLogging.papertrail.port, level: 'warn', @@ -54,7 +59,7 @@ module.exports = (isDebug) => { if (appconfig.externalLogging.rollbar) { const rollbarTransport = require('./winston-transports/rollbar') - winston.add(rollbarTransport, { + logger.add(rollbarTransport, { level: 'warn', key: appconfig.externalLogging.rollbar }) @@ -62,11 +67,11 @@ module.exports = (isDebug) => { if (appconfig.externalLogging.sentry) { const sentryTransport = require('./winston-transports/sentry') - winston.add(sentryTransport, { + logger.add(sentryTransport, { level: 'warn', key: appconfig.externalLogging.sentry }) } - return winston + return logger } diff --git a/server/libs/search.js b/server/libs/search.js index 1e852dd6..0738e574 100644 --- a/server/libs/search.js +++ b/server/libs/search.js @@ -27,12 +27,12 @@ module.exports = { stopwords: _.get(stopWord, appconfig.lang, []) }, (err, si) => { if (err) { - winston.error('[SERVER.Search] Failed to initialize search index.', err) + winston.error('Failed to initialize search index.', err) reject(err) } else { self._si = Promise.promisifyAll(si) self._si.flushAsync().then(() => { - winston.info('[SERVER.Search] Search index flushed and ready.') + winston.info('Search index flushed and ready.') resolve(true) }) } @@ -92,7 +92,7 @@ module.exports = { parent: content.parent || '', content: content.content || '' }]).then(() => { - winston.log('verbose', '[SERVER.Search] Entry ' + content._id + ' added/updated to index.') + winston.log('verbose', 'Entry ' + content._id + ' added/updated to search index.') return true }).catch((err) => { winston.error(err)