move from tracer to winston for logging

This commit is contained in:
Emily 2020-04-18 17:31:17 +10:00
parent 2958652eff
commit 293d473a78
2 changed files with 74 additions and 33 deletions

View file

@ -11,44 +11,20 @@ if (Number(process.version.slice(1).split('.')[0]) < 12) {
// Libraries // Libraries
const Discord = require('discord.js') const Discord = require('discord.js')
const client = new Discord.Client({ disabledEvents: ['TYPING_START'] })
const fs = require('fs') const fs = require('fs')
const colors = require('colors')
const isDocker = require('is-docker') const isDocker = require('is-docker')
const sentry = require('@sentry/node') const sentry = require('@sentry/node')
// Helpers // Create bot client instance
const client = new Discord.Client({ disabledEvents: ['TYPING_START'] })
// Load all our useful utilities
client.config = require('./config') client.config = require('./config')
client.version = require('./version.json') client.version = require('./version.json')
client.db = require('./helpers/mongoose') client.db = require('./utils/mongoose')
require('./helpers/_functions')(client) client.logger = require('./utils/logger')
require('./helpers/music')(client) require('./utils/_functions')(client)
require('./utils/music')(client)
// Initialise logger
client.logger = require('tracer').colorConsole({
transport: function (data) {
console.log(data.output)
fs.appendFile('./file.log', data.rawoutput + '\n', err => {
if (err) throw err
})
},
format: [
'{{timestamp}} | {{title}} | {{file}} | {{message}}',
{
debug: `{{timestamp}} | ${'{{title}}'.magenta} | {{file}} | {{message}}`,
log: `{{timestamp}} | ${'{{title}}'.white} | {{file}} | {{message}}`,
info: `{{timestamp}} | ${'{{title}}'.cyan} | {{file}} | {{message}}`,
ready: `{{timestamp}} | ${'{{title}}'.green} | {{file}} | {{message}}`,
warn: `{{timestamp}} | ${'{{title}}'.yellow} | {{file}} | {{message}}`,
error: `{{timestamp}} | ${'{{title}}'.red} | {{file}} | {{message}}`,
fatal: `{{timestamp}} | ${'{{title}}'.red.bold} | {{file}} | {{message}}`
}
],
dateformat: 'yyyy-mm-dd"T"HH:MM:ss',
methods: ['log', 'debug', 'info', 'ready', 'warn', 'error', 'fatal'],
filters: [colors.white]
})
// Check if Woomy is running inside a Docker container // Check if Woomy is running inside a Docker container
if (isDocker() === true) { if (isDocker() === true) {
@ -116,7 +92,7 @@ const init = async () => {
// Login to Discord // Login to Discord
function failedToLogin (err) { function failedToLogin (err) {
client.logger.error('Failed to login: ' + err) client.logger.error('Couldn\'t login: ' + err)
process.exit(0) process.exit(0)
}; };

65
utils/logger.js Normal file
View file

@ -0,0 +1,65 @@
'use strict'
const { createLogger, format, transports, addColors } = require('winston')
require('winston-daily-rotate-file')
const fs = require('fs')
if (!fs.existsSync('logs')) {
fs.mkdirSync('logs')
}
const dailyRotateFileTransport = new transports.DailyRotateFile({
filename: 'logs/%DATE%.log',
datePattern: 'YYYY-MM-DD'
})
const customLevels = {
levels: {
fatal: 0,
error: 1,
warn: 2,
ready: 3,
info: 4,
cmd: 5,
debug: 6
},
colors: {
fatal: 'bold red',
error: 'red',
warn: 'yellow',
ready: 'green',
info: 'cyan',
cmd: 'white',
debug: 'magenta'
}
}
const logger = createLogger({
level: 'info',
levels: customLevels.levels,
format: format.combine(
format.timestamp({
format: 'YYYY-MM-DD HH:mm:ss'
}),
format.printf(info => `${info.timestamp} ${info.level}: ${info.message}`)
),
transports: [
new transports.Console({
level: 'info',
format: format.combine(
format.colorize(),
format.printf(
info => `${info.timestamp} ${info.level}: ${info.message}`
)
)
}),
dailyRotateFileTransport
]
})
addColors(customLevels.colors)
module.exports = logger