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
const Discord = require('discord.js')
const client = new Discord.Client({ disabledEvents: ['TYPING_START'] })
const fs = require('fs')
const colors = require('colors')
const isDocker = require('is-docker')
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.version = require('./version.json')
client.db = require('./helpers/mongoose')
require('./helpers/_functions')(client)
require('./helpers/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]
})
client.db = require('./utils/mongoose')
client.logger = require('./utils/logger')
require('./utils/_functions')(client)
require('./utils/music')(client)
// Check if Woomy is running inside a Docker container
if (isDocker() === true) {
@ -116,7 +92,7 @@ const init = async () => {
// Login to Discord
function failedToLogin (err) {
client.logger.error('Failed to login: ' + err)
client.logger.error('Couldn\'t login: ' + err)
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