fix: Fixed logger filters

This commit is contained in:
NGPixel 2017-04-28 18:46:27 -04:00
parent 4ba4f29028
commit a68235190a
6 changed files with 57 additions and 54 deletions

View File

@ -8,10 +8,9 @@ const path = require('path')
const ROOTPATH = process.cwd() const ROOTPATH = process.cwd()
const SERVERPATH = path.join(ROOTPATH, 'server') const SERVERPATH = path.join(ROOTPATH, 'server')
global.PROCNAME = 'AGENT'
global.ROOTPATH = ROOTPATH global.ROOTPATH = ROOTPATH
global.SERVERPATH = SERVERPATH global.SERVERPATH = SERVERPATH
global.IS_DEBUG = process.env.NODE_ENV === 'development' const IS_DEBUG = process.env.NODE_ENV === 'development'
let appconf = require('./libs/config')() let appconf = require('./libs/config')()
global.appconfig = appconf.config global.appconfig = appconf.config
@ -21,13 +20,13 @@ global.appdata = appconf.data
// Load Winston // Load Winston
// ---------------------------------------- // ----------------------------------------
global.winston = require('./libs/logger')(IS_DEBUG) global.winston = require('./libs/logger')(IS_DEBUG, 'AGENT')
// ---------------------------------------- // ----------------------------------------
// Load global modules // Load global modules
// ---------------------------------------- // ----------------------------------------
winston.info('[AGENT] Background Agent is initializing...') winston.info('Background Agent is initializing...')
global.db = require('./libs/db').init() global.db = require('./libs/db').init()
global.upl = require('./libs/uploads-agent').init() global.upl = require('./libs/uploads-agent').init()
@ -64,10 +63,10 @@ db.onReady.then(() => {
// Make sure we don't start two concurrent jobs // Make sure we don't start two concurrent jobs
if (jobIsBusy) { 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 return
} }
winston.info('[AGENT] Running all jobs...') winston.info('Running all jobs...')
jobIsBusy = true jobIsBusy = true
// Prepare async job collector // Prepare async job collector
@ -165,7 +164,7 @@ db.onReady.then(() => {
// ---------------------------------------- // ----------------------------------------
Promise.all(jobs).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) { if (!jobUplWatchStarted) {
jobUplWatchStarted = true jobUplWatchStarted = true
@ -176,7 +175,7 @@ db.onReady.then(() => {
return true return true
}).catch((err) => { }).catch((err) => {
winston.error('[AGENT] One or more jobs have failed: ', err) winston.error('One or more jobs have failed: ', err)
}).finally(() => { }).finally(() => {
jobIsBusy = false jobIsBusy = false
}) })
@ -192,7 +191,7 @@ db.onReady.then(() => {
// ---------------------------------------- // ----------------------------------------
process.on('disconnect', () => { process.on('disconnect', () => {
winston.warn('[AGENT] Lost connection to main server. Exiting...') winston.warn('Lost connection to main server. Exiting...')
job.stop() job.stop()
process.exit() process.exit()
}) })

View File

@ -10,10 +10,9 @@ const path = require('path')
const ROOTPATH = process.cwd() const ROOTPATH = process.cwd()
const SERVERPATH = path.join(ROOTPATH, 'server') const SERVERPATH = path.join(ROOTPATH, 'server')
global.PROCNAME = 'SERVER'
global.ROOTPATH = ROOTPATH global.ROOTPATH = ROOTPATH
global.SERVERPATH = SERVERPATH global.SERVERPATH = SERVERPATH
global.IS_DEBUG = process.env.NODE_ENV === 'development' const IS_DEBUG = process.env.NODE_ENV === 'development'
process.env.VIPS_WARNING = false process.env.VIPS_WARNING = false
@ -25,8 +24,8 @@ global.appdata = appconf.data
// Load Winston // Load Winston
// ---------------------------------------- // ----------------------------------------
global.winston = require('./libs/logger')(IS_DEBUG) global.winston = require('./libs/logger')(IS_DEBUG, 'SERVER')
winston.info('[SERVER] Wiki.js is initializing...') winston.info('Wiki.js is initializing...')
// ---------------------------------------- // ----------------------------------------
// Load global modules // Load global modules
@ -188,7 +187,7 @@ app.use(function (err, req, res, next) {
// Start HTTP server // 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) app.set('port', appconfig.port)
var server = http.createServer(app) var server = http.createServer(app)
@ -203,10 +202,10 @@ server.on('error', (error) => {
// handle specific listen errors with friendly messages // handle specific listen errors with friendly messages
switch (error.code) { switch (error.code) {
case 'EACCES': 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) return process.exit(1)
case 'EADDRINUSE': case 'EADDRINUSE':
console.error('Port ' + appconfig.port + ' is already in use!') winston.error('Port ' + appconfig.port + ' is already in use!')
return process.exit(1) return process.exit(1)
default: default:
throw error throw error
@ -214,7 +213,7 @@ server.on('error', (error) => {
}) })
server.on('listening', () => { server.on('listening', () => {
winston.info('[SERVER] HTTP/WS server started successfully! [RUNNING]') winston.info('HTTP/WS server started successfully! [RUNNING]')
}) })
// ---------------------------------------- // ----------------------------------------

View File

@ -66,13 +66,13 @@ module.exports = {
_initRepo (appconfig) { _initRepo (appconfig) {
let self = this let self = this
winston.info('[' + PROCNAME + '.Git] Checking Git repository...') winston.info('Checking Git repository...')
// -> Check if path is accessible // -> Check if path is accessible
return fs.mkdirAsync(self._repo.path).catch((err) => { return fs.mkdirAsync(self._repo.path).catch((err) => {
if (err.code !== 'EEXIST') { 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(() => { }).then(() => {
self._git = new Git({ 'git-dir': self._repo.path }) self._git = new Git({ 'git-dir': self._repo.path })
@ -87,7 +87,7 @@ module.exports = {
}) })
}).then(() => { }).then(() => {
if (appconfig.git === false) { 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) return Promise.resolve(true)
} }
@ -124,10 +124,10 @@ module.exports = {
}) })
}) })
}).catch((err) => { }).catch((err) => {
winston.error('[' + PROCNAME + '.Git] Git remote error!') winston.error('Git remote error!')
throw err throw err
}).then(() => { }).then(() => {
winston.info('[' + PROCNAME + '.Git] Git repository is OK.') winston.info('Git repository is OK.')
return true return true
}) })
}, },
@ -157,12 +157,12 @@ module.exports = {
// Fetch // 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) => { return self._git.pull('origin', self._repo.branch).then((cProc) => {
winston.info('[' + PROCNAME + '.Git] Pull completed.') winston.info('Git Pull completed.')
}) })
.catch((err) => { .catch((err) => {
winston.error('[' + PROCNAME + '.Git] Unable to fetch from git origin!') winston.error('Unable to fetch from git origin!')
throw err throw err
}) })
.then(() => { .then(() => {
@ -172,19 +172,19 @@ module.exports = {
let out = cProc.stdout.toString() let out = cProc.stdout.toString()
if (_.includes(out, 'commit')) { 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 self._git.push('origin', self._repo.branch).then(() => {
return winston.info('[' + PROCNAME + '.Git] Push completed.') return winston.info('Git Push completed.')
}) })
} else { } else {
winston.info('[' + PROCNAME + '.Git] Push skipped. Repository is already in sync.') winston.info('Git Push skipped. Repository is already in sync.')
} }
return true return true
}) })
}) })
.catch((err) => { .catch((err) => {
winston.error('[' + PROCNAME + '.Git] Unable to push changes to remote!') winston.error('Unable to push changes to remote Git repository!')
throw err throw err
}) })
}, },

View File

@ -98,7 +98,7 @@ module.exports = {
* @return {Void} Void * @return {Void} Void
*/ */
createBaseDirectories (appconfig) { createBaseDirectories (appconfig) {
winston.info('[SERVER.Local] Checking data directories...') winston.info('Checking data directories...')
try { try {
fs.ensureDirSync(path.resolve(ROOTPATH, appconfig.paths.data)) fs.ensureDirSync(path.resolve(ROOTPATH, appconfig.paths.data))
@ -121,7 +121,7 @@ module.exports = {
winston.error(err) winston.error(err)
} }
winston.info('[SERVER.Local] Data and Repository directories are OK.') winston.info('Data and Repository directories are OK.')
}, },
/** /**

View File

@ -1,31 +1,36 @@
'use strict' 'use strict'
const winston = require('winston') module.exports = (isDebug, processName) => {
let winston = require('winston')
module.exports = (isDebug) => { if (typeof processName === 'undefined') {
if (typeof PROCNAME === 'undefined') { processName = 'SERVER'
const PROCNAME = 'SERVER' // eslint-disable-line no-unused-vars
} }
// Console + File Logs // Console
winston.remove(winston.transports.Console) let logger = new (winston.Logger)({
winston.add(winston.transports.Console, {
level: (isDebug) ? 'debug' : 'info', level: (isDebug) ? 'debug' : 'info',
prettyPrint: true, transports: [
colorize: true, new (winston.transports.Console)({
silent: false, level: (isDebug) ? 'debug' : 'info',
timestamp: true, prettyPrint: true,
filters: [(level, msg, meta) => { colorize: true,
return '[' + PROCNAME + '] ' + msg // eslint-disable-line no-undef silent: false,
}] timestamp: true
})
]
})
logger.filters.push((level, msg) => {
return '[' + processName + '] ' + msg
}) })
// External services // External services
if (appconfig.externalLogging.bugsnag) { if (appconfig.externalLogging.bugsnag) {
const bugsnagTransport = require('./winston-transports/bugsnag') const bugsnagTransport = require('./winston-transports/bugsnag')
winston.add(bugsnagTransport, { logger.add(bugsnagTransport, {
level: 'warn', level: 'warn',
key: appconfig.externalLogging.bugsnag key: appconfig.externalLogging.bugsnag
}) })
@ -33,7 +38,7 @@ module.exports = (isDebug) => {
if (appconfig.externalLogging.loggly) { if (appconfig.externalLogging.loggly) {
require('winston-loggly-bulk') require('winston-loggly-bulk')
winston.add(winston.transports.Loggly, { logger.add(winston.transports.Loggly, {
token: appconfig.externalLogging.loggly.token, token: appconfig.externalLogging.loggly.token,
subdomain: appconfig.externalLogging.loggly.subdomain, subdomain: appconfig.externalLogging.loggly.subdomain,
tags: ['wiki-js'], tags: ['wiki-js'],
@ -44,7 +49,7 @@ module.exports = (isDebug) => {
if (appconfig.externalLogging.papertrail) { if (appconfig.externalLogging.papertrail) {
require('winston-papertrail').Papertrail // eslint-disable-line no-unused-expressions 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, host: appconfig.externalLogging.papertrail.host,
port: appconfig.externalLogging.papertrail.port, port: appconfig.externalLogging.papertrail.port,
level: 'warn', level: 'warn',
@ -54,7 +59,7 @@ module.exports = (isDebug) => {
if (appconfig.externalLogging.rollbar) { if (appconfig.externalLogging.rollbar) {
const rollbarTransport = require('./winston-transports/rollbar') const rollbarTransport = require('./winston-transports/rollbar')
winston.add(rollbarTransport, { logger.add(rollbarTransport, {
level: 'warn', level: 'warn',
key: appconfig.externalLogging.rollbar key: appconfig.externalLogging.rollbar
}) })
@ -62,11 +67,11 @@ module.exports = (isDebug) => {
if (appconfig.externalLogging.sentry) { if (appconfig.externalLogging.sentry) {
const sentryTransport = require('./winston-transports/sentry') const sentryTransport = require('./winston-transports/sentry')
winston.add(sentryTransport, { logger.add(sentryTransport, {
level: 'warn', level: 'warn',
key: appconfig.externalLogging.sentry key: appconfig.externalLogging.sentry
}) })
} }
return winston return logger
} }

View File

@ -27,12 +27,12 @@ module.exports = {
stopwords: _.get(stopWord, appconfig.lang, []) stopwords: _.get(stopWord, appconfig.lang, [])
}, (err, si) => { }, (err, si) => {
if (err) { if (err) {
winston.error('[SERVER.Search] Failed to initialize search index.', err) winston.error('Failed to initialize search index.', err)
reject(err) reject(err)
} else { } else {
self._si = Promise.promisifyAll(si) self._si = Promise.promisifyAll(si)
self._si.flushAsync().then(() => { self._si.flushAsync().then(() => {
winston.info('[SERVER.Search] Search index flushed and ready.') winston.info('Search index flushed and ready.')
resolve(true) resolve(true)
}) })
} }
@ -92,7 +92,7 @@ module.exports = {
parent: content.parent || '', parent: content.parent || '',
content: content.content || '' content: content.content || ''
}]).then(() => { }]).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 return true
}).catch((err) => { }).catch((err) => {
winston.error(err) winston.error(err)