utils: use reggol for logging

This commit is contained in:
undefined committed 2022-09-23 18:27:45 +08:00
1 parent 5b7067d77d
commit ab933bc9d5
7 files changed
+12 -97

No files matched your search

+2 -2
View File
@@ -1,3 +1,3 @@
import { Logger } from '@hydrooj/utils/lib/logger';
export * from '@hydrooj/utils/lib/logger';
import { Logger } from '@hydrooj/utils/lib/utils';
export * from '@hydrooj/utils/lib/utils';
export default new Logger('judge');
+1 -2
View File
@@ -13,7 +13,7 @@ import cac from 'cac';
import './ui';
import { Logger } from './logger';
import { ApiMixin, Context } from './context';
import { Context } from './context';
// eslint-disable-next-line import/no-duplicates
import { unwrapExports } from './utils';
// This is the main entry. So let's re-export some modules.
@@ -118,7 +118,6 @@ export function addScript(name: string, description: string) {
}
Context.service('loader');
app.api = new ApiMixin(app);
const loader = new Loader();
app.loader = loader;
loader.app = app;
+2 -2
View File
@@ -1,5 +1,5 @@
import { Logger } from '@hydrooj/utils/lib/logger';
export { Logger } from '@hydrooj/utils/lib/logger';
import { Logger } from '@hydrooj/utils/lib/utils';
export { Logger } from '@hydrooj/utils/lib/utils';
global.Hydro.Logger = Logger;
export const logger = new Logger('*');
+1 -1
View File
@@ -72,7 +72,7 @@ export default class Watcher extends Service {
}
});
this.watcher.on('unlink', (path) => {
logger.debug('change detected: ', `-${relative(this.root, path)}`);
logger.debug('change detected:', `-${relative(this.root, path)}`);
bus.emit('app/watch/unlink', path);
});
}
-90
View File
@@ -1,90 +0,0 @@
import { format, inspect, InspectOptions } from 'util';
const colors = [
20, 21, 26, 27, 32, 33, 38, 39, 40, 41, 42, 43, 44, 45, 56, 57, 62,
63, 68, 69, 74, 75, 76, 77, 78, 79, 80, 81, 92, 93, 98, 99, 112, 113,
129, 134, 135, 148, 149, 160, 161, 162, 163, 164, 165, 166, 167, 168,
169, 170, 171, 172, 173, 178, 179, 184, 185, 196, 197, 198, 199, 200,
201, 202, 203, 204, 205, 206, 207, 208, 209, 214, 215, 220, 221,
];
const instances: Record<string, Logger> = {};
type LogFunction = (format: any, ...param: any[]) => void;
type LogType = 'success' | 'error' | 'info' | 'warn' | 'debug';
export interface Logger extends Record<LogType, LogFunction> { }
export class Logger {
static readonly SUCCESS = 1;
static readonly ERROR = 1;
static readonly INFO = 2;
static readonly WARN = 2;
static readonly DEBUG = 3;
static levels: Record<string, number> = {};
private code: number;
private displayName: string;
public stream: NodeJS.WriteStream = process.stdout;
static options: InspectOptions = {
colors: process.stderr.isTTY,
};
static formatters: Record<string, (this: Logger, value: any) => string> = {
c: Logger.prototype.color,
C: (value) => Logger.color(15, value, ';1'),
o: (value) => inspect(value, Logger.options).replace(/\s*\n\s*/g, ' '),
};
static color(code: number, value: any, decoration = '') {
if (!Logger.options.colors) return `${value}`;
return `\u001B[3${code < 8 ? code : `8;5;${code}`}${decoration}m${value}\u001B[0m`;
}
constructor(public name: string) {
// eslint-disable-next-line no-constructor-return
if (name in instances) return instances[name];
let hash = 0;
for (let i = 0; i < name.length; i++) {
hash = ((hash << 3) - hash) + name.charCodeAt(i);
hash |= 0;
}
instances[name] = this;
this.code = colors[Math.abs(hash) % colors.length];
this.displayName = name ? this.color(`${name} `, ';1') : '';
this.createMethod('success', '[S] ', Logger.SUCCESS);
this.createMethod('error', '[E] ', Logger.ERROR);
this.createMethod('info', '[I] ', Logger.INFO);
this.createMethod('warn', '[W] ', Logger.WARN);
this.createMethod('debug', '[D] ', Logger.DEBUG);
}
private color(value: any, decoration = '') {
return Logger.color(this.code, value, decoration);
}
private createMethod(name: LogType, prefix: string, minLevel: number) {
this[name] = (...args: [any, ...any[]]) => {
if (this.level < minLevel) return;
this.stream.write(`${prefix} ${this.displayName} ${this.format(...args)}\n`);
};
}
get level() {
return process.env.DEV ? 3 : 2;
}
format: (format: any, ...param: any[]) => string = (...args) => {
if (args[0] instanceof Error) args[0] = args[0].stack || args[0].message;
else if (typeof args[0] !== 'string') args.unshift('%O');
let index = 0;
args[0] = (args[0] as string).replace(/%([a-zA-Z%])/g, (match, fmt) => {
if (match === '%%') return '%';
index += 1;
const formatter = Logger.formatters[fmt];
if (typeof formatter === 'function') {
match = formatter.call(this, args[index]);
args.splice(index, 1);
index -= 1;
}
return match;
}).split('\n').join('\n ');
return format(...args);
};
}
+5
View File
@@ -7,6 +7,11 @@ import fs from 'fs-extra';
import type { Moment } from 'moment-timezone';
import { isMoment } from 'moment-timezone';
import { ObjectID } from 'mongodb';
import Logger from 'reggol';
Logger.levels.base = process.env.DEV ? 3 : 2;
export { Logger };
const encrypt = (algorithm, content) => crypto.createHash(algorithm).update(content).digest('hex');
export const sha1 = (content: string) => encrypt('sha1', content);
+1
View File
@@ -14,6 +14,7 @@
"js-yaml": "^4.1.0",
"moment-timezone": "^0.5.37",
"mongodb": "^3.7.3",
"reggol": "^1.1.0",
"source-map-support": "^0.5.21",
"systeminformation": "^5.12.6"
},