1
0
Fork 0
mirror of https://github.com/FrankerFaceZ/FrankerFaceZ.git synced 2025-06-28 05:15:54 +00:00
FrankerFaceZ/src/utilities/logging.js

128 lines
2.5 KiB
JavaScript
Raw Normal View History

2017-11-13 01:23:39 -05:00
'use strict';
const RAVEN_LEVELS = {
1: 'debug',
2: 'info',
4: 'warn',
8: 'error'
};
export class Logger {
constructor(parent, name, level, raven) {
this.root = parent ? parent.root : this;
2017-11-13 01:23:39 -05:00
this.parent = parent;
this.name = name;
if ( this.root == this ) {
this.captured_init = [];
this.label = 'FFZ';
}
this.init = false;
2017-11-13 01:23:39 -05:00
this.enabled = true;
this.level = level || (parent && parent.level) || Logger.DEFAULT_LEVEL;
this.raven = raven || (parent && parent.raven);
2017-11-13 01:23:39 -05:00
this.children = {};
}
get(name, level) {
if ( ! this.children[name] )
this.children[name] = new Logger(this, (this.name ? `${this.name}.${name}` : name), level);
return this.children[name];
}
debug(...args) {
return this.invoke(Logger.DEBUG, args);
}
info(...args) {
return this.invoke(Logger.INFO, args);
}
warn(...args) {
return this.invoke(Logger.WARN, args);
}
warning(...args) {
return this.invoke(Logger.WARN, args);
}
2017-11-13 01:23:39 -05:00
error(...args) {
return this.invoke(Logger.ERROR, args);
}
crumb(...args) {
if ( this.raven )
return this.raven.captureBreadcrumb(...args);
}
capture(exc, opts, ...args) {
if ( this.raven ) {
opts = opts || {};
if ( ! opts.logger )
opts.logger = this.name;
this.raven.captureException(exc, opts);
}
if ( args.length )
return this.error(...args);
}
2017-11-13 01:23:39 -05:00
/* eslint no-console: "off" */
invoke(level, args) {
if ( ! this.enabled || level < this.level )
return;
const message = Array.prototype.slice.call(args);
if ( this.root.init )
this.root.captured_init.push({
time: Date.now(),
category: this.name,
message: message.join(' '),
level: RAVEN_LEVELS[level] || level
});
this.crumb({
message: message.join(' '),
category: this.name,
level: RAVEN_LEVELS[level] || level
});
2017-11-13 01:23:39 -05:00
if ( this.name )
message.unshift(`%c${this.root.label} [%c${this.name}%c]:%c`, 'color:#755000; font-weight:bold', '', 'color:#755000; font-weight:bold', '');
2017-11-13 01:23:39 -05:00
else
message.unshift(`%c${this.root.label}:%c`, 'color:#755000; font-weight:bold', '');
2017-11-13 01:23:39 -05:00
if ( level === Logger.DEBUG )
console.debug(...message);
2017-11-13 01:23:39 -05:00
else if ( level === Logger.INFO )
console.info(...message);
else if ( level === Logger.WARN )
console.warn(...message);
else if ( level === Logger.ERROR )
console.error(...message);
else
console.log(...message);
}
}
Logger.DEFAULT_LEVEL = 2;
Logger.DEBUG = 1;
Logger.INFO = 2;
Logger.WARN = 4;
Logger.WARNING = 4;
2017-11-13 01:23:39 -05:00
Logger.ERROR = 8;
Logger.OFF = 99;
export default Logger;