Skip to content

Commit

Permalink
lib/logging: Update winston to 3.x
Browse files Browse the repository at this point in the history
  • Loading branch information
sogehige committed Mar 21, 2018
1 parent 212f619 commit 7883c68
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 109 deletions.
2 changes: 1 addition & 1 deletion ISSUE_TEMPLATE.md
Expand Up @@ -5,7 +5,7 @@
### Steps to reproduce

### Additional informations
<!-- Please run `!_debug` command in chat and paste information below this line -->
<!-- Please run `!_debug` command in chat and paste debug information of log/sogebot.log below this line -->


<!-- Attach your logs/exception.log file if bot crashes -->
162 changes: 56 additions & 106 deletions libs/logging.js
@@ -1,7 +1,9 @@
'use strict'

var winston = require('winston')
require('winston-logrotate')

const format = winston.format

var fs = require('fs')
var _ = require('lodash')
var logDir = './logs'
Expand All @@ -10,113 +12,61 @@ const glob = require('glob')

if (!fs.existsSync(logDir)) fs.mkdirSync(logDir)

global.log = new (winston.Logger)({
levels: {
error: 0,
chatIn: 1,
chatOut: 2,
whisperIn: 3,
whisperOut: 4,
host: 5,
follow: 6,
unfollow: 7,
cheer: 7,
sub: 7,
subgift: 7,
resub: 7,
timeout: 7,
ban: 9,
warning: 10,
debug: 11,
info: 12
},
transports: [
new (winston.transports.Console)({
handleExceptions: true,
timestamp: function () {
return new Date().toISOString()
},
formatter: function (options) {
// Return string will be passed to logger.
let level = options.level
options.meta = options.meta || {}
if (level === 'error') level = '!!! ERROR !!!'
if (level === 'debug') level = 'DEBUG:'
if (level === 'chatIn') level = '<<<'
if (level === 'chatOut') level = '>>>'
if (level === 'whisperIn') level = '<w<'
if (level === 'whisperOut') level = '>w>'
if (level === 'info') level = '|'
if (level === 'warning') level = '|!'
if (level === 'timeout') level = '+timeout'
if (level === 'ban') level = '+ban'
if (level === 'follow') level = '+follow'
if (level === 'host') level = '+host'
if (level === 'unfollow') level = '-follow'
if (level === 'cheer') level = '+cheer'
if (level === 'sub') level = '+sub'
if (level === 'subgift') level = '+subgift'
if (level === 'resub') level = '+resub'
let username = !_.isUndefined(options.meta.username) ? options.meta.username : ''
let fnc = !_.isUndefined(options.meta.fnc) ? options.meta.fnc : ''
return moment().format('YYYY-MM-DDTHH:mm:ss.SSS') + (level ? ' ' + level + ' ' : ' ') + (options.message ? options.message : '') + (username ? ' [' + username + ']' : '') + (fnc ? ' [function: ' + fnc + ']' : '') + (_.size(options.meta) > 0 && level === 'DEBUG:' ? '\n' + options.timestamp() + ' DEBUG: ' + JSON.stringify(options.meta) : '')
}
}),
new winston.transports.Rotate({
level: 'info',
timestamp: function () {
return new Date().toISOString()
},
formatter: function (options) {
// Return string will be passed to logger.
let level = options.level
options.meta = options.meta || {}
if (level === 'error') level = '!!! ERROR !!!'
if (level === 'debug') level = 'DEBUG:'
if (level === 'chatIn') level = '<<<'
if (level === 'chatOut') level = '>>>'
if (level === 'whisperIn') level = '<w<'
if (level === 'whisperOut') level = '>w>'
if (level === 'info') level = '|'
if (level === 'warning') level = '|!'
if (level === 'timeout') level = '+timeout'
if (level === 'ban') level = '+ban'
if (level === 'follow') level = '+follow'
if (level === 'host') level = '+host'
if (level === 'unfollow') level = '-follow'
if (level === 'cheer') level = '+cheer'
if (level === 'sub') level = '+sub'
if (level === 'resub') level = '+resub'
let username = !_.isUndefined(options.meta.username) ? options.meta.username : ''
return moment().format('YYYY-MM-DDTHH:mm:ss.SSS') +
(level ? ' ' + level + ' ' : ' ') +
(options.message ? options.message : '') +
(username ? ' [' + username + ']' : '') +
(_.size(options.meta) > 0 && level === 'DEBUG:' ? '\n' + moment().format('YYYY-MM-DDTHH:mm:ss.SSS') + ' DEBUG: ' + JSON.stringify(options.meta) : '')
},
file: logDir + '/sogebot.log',
handleExceptions: false,
colorize: false,
json: false,
size: '5m',
keep: 5,
compress: true
const levels = {
error: 1,
chatIn: 2,
chatOut: 2,
whisperIn: 2,
whisperOut: 2,
host: 5,
follow: 5,
unfollow: 5,
cheer: 5,
sub: 5,
subgift: 5,
resub: 5,
timeout: 8,
ban: 8,
warning: 11,
info: 12,
debug: 13
}

global.log = winston.createLogger({
exitOnError: true,
json: false,
levels: levels,
level: 'debug',
format: format.combine(
format.timestamp(),
format.printf(info => {
let level
if (info.level === 'error') level = '!!! ERROR !!!'
if (info.level === 'debug') level = 'DEBUG:'
if (info.level === 'chatIn') level = '<<<'
if (info.level === 'chatOut') level = '>>>'
if (info.level === 'whisperIn') level = '<w<'
if (info.level === 'whisperOut') level = '>w>'
if (info.level === 'info') level = '|'
if (info.level === 'warning') level = '|!'
if (info.level === 'timeout') level = '+timeout'
if (info.level === 'ban') level = '+ban'
if (info.level === 'follow') level = '+follow'
if (info.level === 'host') level = '+host'
if (info.level === 'unfollow') level = '-follow'
if (info.level === 'cheer') level = '+cheer'
if (info.level === 'sub') level = '+sub'
if (info.level === 'subgift') level = '+subgift'
if (info.level === 'resub') level = '+resub'
return `${info.timestamp} ${level} ${info.message}`
})
],
),
exceptionHandlers: [
new winston.transports.File({
exitOnError: true,
filename: logDir + '/exceptions-' + moment().format('YYYY-MM-DDTHH_mm_ss') + '.log',
json: false,
formatter: function (options) {
global.log.error('+------------------------------------------------------------------------------+')
global.log.error('| BOT HAS UNEXPECTEDLY CRASHED |')
global.log.error('| PLEASE CHECK https://github.com/sogehige/SogeBot/wiki/How-to-report-an-issue |')
global.log.error('| AND ADD logs/exceptions.log file to your report |')
global.log.error('+------------------------------------------------------------------------------+')
return JSON.stringify(options.meta)
}
})
new winston.transports.File({ filename: logDir + '/exceptions.log', colorize: false, maxsize: 5000 })
],
transports: [
new winston.transports.File({ filename: logDir + '/sogebot.log', colorize: false, maxsize: 5000, maxFiles: 5 }),
new winston.transports.Console()
]
})

Expand Down
10 changes: 10 additions & 0 deletions main.js
Expand Up @@ -377,4 +377,14 @@ process.on('unhandledRejection', function (reason, p) {
global.log.error(p)
})

process.on('uncaughtException', (err) => {
global.log.error(err.stack)
global.log.error('+------------------------------------------------------------------------------+')
global.log.error('| BOT HAS UNEXPECTEDLY CRASHED |')
global.log.error('| PLEASE CHECK https://github.com/sogehige/SogeBot/wiki/How-to-report-an-issue |')
global.log.error('| AND ADD logs/exceptions.log file to your report |')
global.log.error('+------------------------------------------------------------------------------+')
process.exit(1)
})

exports = module.exports = global
3 changes: 1 addition & 2 deletions package.json
Expand Up @@ -55,8 +55,7 @@
"twitter": "^1.7.1",
"url-join": "^4.0.0",
"velocity-animate": "^1.5.1",
"winston": "^2.4.0",
"winston-logrotate": "^1.3.0",
"winston": "^3.0.0-rc3",
"xregexp": "4.1.1",
"youtube-search": "^1.0.10",
"ytdl-core": "^0.20.2"
Expand Down

0 comments on commit 7883c68

Please sign in to comment.