DiscordTickets/src/lib/logger.js

68 lines
1.4 KiB
JavaScript
Raw Normal View History

2022-11-01 23:10:57 +02:00
const {
ConsoleTransport,
FileTransport,
Logger,
} = require('leekslazylogger');
2022-05-05 23:29:28 +03:00
const DTF = require('@eartharoid/dtf');
const { short } = require('leeks.js');
2023-03-07 00:09:05 +02:00
const { format } = require('util');
2022-03-18 18:27:32 +02:00
2022-05-06 13:09:56 +03:00
const dtf = new DTF('en-GB');
2022-03-18 18:27:32 +02:00
const colours = {
critical: ['&!4&f', '&!4&f'],
debug: ['&1', '&9'],
error: ['&4', '&c'],
info: ['&3', '&b'],
notice: ['&!6&0', '&!6&0'],
success: ['&2', '&a'],
2022-09-07 23:24:16 +03:00
verbose: ['&7', '&f'],
2022-03-18 18:27:32 +02:00
warn: ['&6', '&e'],
};
2022-05-05 23:29:28 +03:00
module.exports = config => {
2022-03-18 18:27:32 +02:00
const transports = [
2022-11-01 23:10:57 +02:00
new ConsoleTransport({
2022-03-18 18:27:32 +02:00
format: log => {
const timestamp = dtf.fill('DD/MM/YY HH:mm:ss', log.timestamp);
const colour = colours[log.level.name];
2023-03-07 00:09:05 +02:00
return format(
short(`&f&!7 %s &r ${colour[0]}[%s]&r %s${colour[1]}%s&r`),
timestamp,
log.level.name.toUpperCase(),
log.namespace ? short(`&d(${log.namespace.toUpperCase()})&r `) : '',
log.content,
);
2022-03-18 18:27:32 +02:00
},
level: config.logs.level,
2022-05-05 23:29:28 +03:00
}),
2022-03-18 18:27:32 +02:00
];
if (config.logs.files.enabled) {
transports.push(
2022-11-01 23:10:57 +02:00
new FileTransport({
2022-03-18 18:27:32 +02:00
clean_directory: config.logs.files.keepFor,
directory: config.logs.files.directory,
2022-11-01 23:10:57 +02:00
format: '[{timestamp}] [{LEVEL}] ({NAMESPACE}) @{file}:{line}:{column} {content}',
2022-03-18 18:27:32 +02:00
level: config.logs.level,
name: 'Discord Tickets by eartharoid',
2022-05-05 23:29:28 +03:00
}),
2022-03-18 18:27:32 +02:00
);
}
return new Logger({
2022-11-01 23:10:57 +02:00
namespaces: [
'autocomplete',
'buttons',
'commands',
'http',
'listeners',
'menus',
'modals',
'settings',
'tickets',
],
2022-03-18 18:27:32 +02:00
transports,
});
};