mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 09:46:28 +08:00
core: update logger
This commit is contained in:
1 parent
54ef77b108
commit
ff7adf0937
5 files changed
+44
-28
No files matched your search
@@ -7,7 +7,7 @@ import yaml from 'js-yaml';
|
|||||||
import { Logger } from '../logger';
|
import { Logger } from '../logger';
|
||||||
import * as bus from '../service/bus';
|
import * as bus from '../service/bus';
|
||||||
|
|
||||||
const logger = new Logger('common', true);
|
const logger = new Logger('common');
|
||||||
|
|
||||||
export const builtinLib = [
|
export const builtinLib = [
|
||||||
'jwt', 'download', 'i18n', 'mail', 'useragent',
|
'jwt', 'download', 'i18n', 'mail', 'useragent',
|
||||||
|
|||||||
@@ -2,8 +2,8 @@ import { FilterQuery, ObjectID } from 'mongodb';
|
|||||||
import { pick } from 'lodash';
|
import { pick } from 'lodash';
|
||||||
import { postJudge } from './judge';
|
import { postJudge } from './judge';
|
||||||
import {
|
import {
|
||||||
ContestNotFoundError, PermissionError, ProblemNotFoundError,
|
ContestNotAttendedError, ContestNotFoundError, PermissionError,
|
||||||
RecordNotFoundError, UserNotFoundError,
|
ProblemNotFoundError, RecordNotFoundError, UserNotFoundError,
|
||||||
} from '../error';
|
} from '../error';
|
||||||
import { buildProjection } from '../utils';
|
import { buildProjection } from '../utils';
|
||||||
import { RecordDoc } from '../interface';
|
import { RecordDoc } from '../interface';
|
||||||
@@ -41,6 +41,9 @@ class RecordListHandler extends Handler {
|
|||||||
tdoc = await contest.get(domainId, tid, -1);
|
tdoc = await contest.get(domainId, tid, -1);
|
||||||
if (!tdoc) throw new ContestNotFoundError(domainId, pid);
|
if (!tdoc) throw new ContestNotFoundError(domainId, pid);
|
||||||
if (!contest.canShowScoreboard.call(this, tdoc, true)) throw new PermissionError(PERM.PERM_VIEW_CONTEST_HIDDEN_SCOREBOARD);
|
if (!contest.canShowScoreboard.call(this, tdoc, true)) throw new PermissionError(PERM.PERM_VIEW_CONTEST_HIDDEN_SCOREBOARD);
|
||||||
|
if (!this.user.own(tdoc) && !(await contest.getStatus(domainId, tid, this.user._id))?.attend) {
|
||||||
|
throw new ContestNotAttendedError(domainId, tid);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (uidOrName) {
|
if (uidOrName) {
|
||||||
const udoc = await user.getById(domainId, +uidOrName) ?? await user.getByUname(domainId, uidOrName);
|
const udoc = await user.getById(domainId, +uidOrName) ?? await user.getByUname(domainId, uidOrName);
|
||||||
@@ -59,16 +62,16 @@ class RecordListHandler extends Handler {
|
|||||||
}
|
}
|
||||||
let cursor = record.getMulti(domainId, q).sort('_id', -1);
|
let cursor = record.getMulti(domainId, q).sort('_id', -1);
|
||||||
if (!full) cursor = cursor.project(buildProjection(record.PROJECTION_LIST));
|
if (!full) cursor = cursor.project(buildProjection(record.PROJECTION_LIST));
|
||||||
const [rdocs] = invalid ? [[]] : await paginate(cursor, page, full ? 10 : system.get('pagination.record'));
|
const [rdocs] = invalid ? [[] as RecordDoc[]] : await paginate(cursor, page, full ? 10 : system.get('pagination.record'));
|
||||||
const canViewProblem = this.user.hasPerm(PERM.PERM_VIEW_PROBLEM);
|
const canViewProblem = this.user.hasPerm(PERM.PERM_VIEW_PROBLEM);
|
||||||
const canViewProblemHidden = this.user.hasPerm(PERM.PERM_VIEW_PROBLEM_HIDDEN);
|
const canViewProblemHidden = (!!tid) || this.user.hasPerm(PERM.PERM_VIEW_PROBLEM_HIDDEN);
|
||||||
const [udict, pdict] = full ? [{}, {}]
|
const [udict, pdict] = full ? [{}, {}]
|
||||||
: await Promise.all([
|
: await Promise.all([
|
||||||
user.getList(domainId, rdocs.map((rdoc) => rdoc.uid)),
|
user.getList(domainId, rdocs.map((rdoc) => rdoc.uid)),
|
||||||
canViewProblem
|
canViewProblem
|
||||||
? problem.getList(domainId, rdocs.map(
|
? problem.getList(domainId, rdocs.map(
|
||||||
(rdoc) => (rdoc.domainId === domainId ? rdoc.pid : `${rdoc.pdomain}:${rdoc.pid}`),
|
(rdoc) => (rdoc.domainId === domainId ? rdoc.pid : `${rdoc.pdomain}:${rdoc.pid}`),
|
||||||
), (!!tid) || canViewProblemHidden || this.user._id, false)
|
), canViewProblemHidden || this.user._id, false)
|
||||||
: Object.fromEntries([rdocs.map((rdoc) => [rdoc.pid, { ...problem.default, pid: rdoc.pid }])]),
|
: Object.fromEntries([rdocs.map((rdoc) => [rdoc.pid, { ...problem.default, pid: rdoc.pid }])]),
|
||||||
]);
|
]);
|
||||||
this.response.body = {
|
this.response.body = {
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { inspect, format, InspectOptions } from 'util';
|
import { nanoid } from 'nanoid';
|
||||||
import { Time } from './utils';
|
import { format, inspect, InspectOptions } from 'util';
|
||||||
|
|
||||||
const colors = [
|
const colors = [
|
||||||
20, 21, 26, 27, 32, 33, 38, 39, 40, 41, 42, 43, 44, 45, 56, 57, 62,
|
20, 21, 26, 27, 32, 33, 38, 39, 40, 41, 42, 43, 44, 45, 56, 57, 62,
|
||||||
@@ -20,9 +20,7 @@ export class Logger {
|
|||||||
static readonly WARN = 2;
|
static readonly WARN = 2;
|
||||||
static readonly DEBUG = 3;
|
static readonly DEBUG = 3;
|
||||||
static baseLevel = process.env.DEV ? 3 : 2;
|
static baseLevel = process.env.DEV ? 3 : 2;
|
||||||
static showDiff = false;
|
|
||||||
static levels: Record<string, number> = {};
|
static levels: Record<string, number> = {};
|
||||||
static lastTime = 0;
|
|
||||||
private code: number;
|
private code: number;
|
||||||
private displayName: string;
|
private displayName: string;
|
||||||
public stream: NodeJS.WritableStream = process.stderr;
|
public stream: NodeJS.WritableStream = process.stderr;
|
||||||
@@ -41,7 +39,7 @@ export class Logger {
|
|||||||
return `\u001B[3${code < 8 ? code : `8;5;${code}`}${decoration}m${value}\u001B[0m`;
|
return `\u001B[3${code < 8 ? code : `8;5;${code}`}${decoration}m${value}\u001B[0m`;
|
||||||
}
|
}
|
||||||
|
|
||||||
constructor(public name: string, private showDiff = false) {
|
constructor(public name: string) {
|
||||||
if (name in instances) return instances[name];
|
if (name in instances) return instances[name];
|
||||||
let hash = 0;
|
let hash = 0;
|
||||||
for (let i = 0; i < name.length; i++) {
|
for (let i = 0; i < name.length; i++) {
|
||||||
@@ -59,13 +57,32 @@ export class Logger {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private color(value: any, decoration = '') {
|
private color(value: any, decoration = '') {
|
||||||
return Logger.color(this.code, value, decoration);
|
if (!process.stderr.isTTY) return value;
|
||||||
|
return `\u001B[3${this.code < 8 ? this.code : `8;5;${this.code}`}${decoration}m${value}\u001B[0m`;
|
||||||
}
|
}
|
||||||
|
|
||||||
private createMethod(name: LogType, prefix: string, minLevel: number) {
|
private createMethod(name: LogType, prefix: string, minLevel: number) {
|
||||||
this[name] = (...args: [any, ...any[]]) => {
|
this[name] = (...args: [any, ...any[]]) => {
|
||||||
if (this.level < minLevel) return false;
|
if (this.level < minLevel) return false;
|
||||||
const msg = `${prefix} ${this.displayName} ${this.format(...args)}`;
|
if (typeof args[0] === 'string' && /^[a-zA-Z]+\.[a-zA-Z]+(\.[a-zA-Z]+)?$/.test(args[0])) {
|
||||||
|
const [type, ...payload] = args;
|
||||||
|
if (global.Hydro.service.db?.started) {
|
||||||
|
global.Hydro.service.db.collection('log').insertOne({
|
||||||
|
_id: nanoid(),
|
||||||
|
_pid: process.pid,
|
||||||
|
_time: new Date(),
|
||||||
|
_level: name,
|
||||||
|
_category: this.name,
|
||||||
|
_message: type,
|
||||||
|
...payload,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
const msg = `${prefix} ${this.displayName} ${type.translate('en').format({ _inspect: true, payload })}`;
|
||||||
|
if (process.send) process.send({ event: 'message/log', payload: [msg] });
|
||||||
|
else global.Hydro.service.bus.parallel('message/log', msg);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
const msg = `${prefix} ${this.displayName} ${this.format(...args)})}`;
|
||||||
if (process.send) process.send({ event: 'message/log', payload: [msg] });
|
if (process.send) process.send({ event: 'message/log', payload: [msg] });
|
||||||
else global.Hydro.service.bus.parallel('message/log', msg);
|
else global.Hydro.service.bus.parallel('message/log', msg);
|
||||||
return true;
|
return true;
|
||||||
@@ -76,8 +93,6 @@ export class Logger {
|
|||||||
return Logger.levels[this.name] ?? Logger.baseLevel;
|
return Logger.levels[this.name] ?? Logger.baseLevel;
|
||||||
}
|
}
|
||||||
|
|
||||||
extend = (namespace: string, showDiff = this.showDiff) => new Logger(`${this.name}:${namespace}`, showDiff);
|
|
||||||
|
|
||||||
format: (format: any, ...param: any[]) => string = (...args) => {
|
format: (format: any, ...param: any[]) => string = (...args) => {
|
||||||
if (args[0] instanceof Error) args[0] = args[0].stack || args[0].message;
|
if (args[0] instanceof Error) args[0] = args[0].stack || args[0].message;
|
||||||
else if (typeof args[0] !== 'string') args.unshift('%O');
|
else if (typeof args[0] !== 'string') args.unshift('%O');
|
||||||
@@ -93,13 +108,6 @@ export class Logger {
|
|||||||
}
|
}
|
||||||
return match;
|
return match;
|
||||||
}).split('\n').join('\n ');
|
}).split('\n').join('\n ');
|
||||||
if (Logger.showDiff || this.showDiff) {
|
|
||||||
const now = Date.now();
|
|
||||||
if (Logger.lastTime) {
|
|
||||||
args.push(this.color(`+${Time.formatTimeShort(now - Logger.lastTime)}`));
|
|
||||||
}
|
|
||||||
Logger.lastTime = now;
|
|
||||||
}
|
|
||||||
return format(...args);
|
return format(...args);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ import type { ProblemSolutionHandler } from '../handler/problem';
|
|||||||
import type { UserRegisterHandler } from '../handler/user';
|
import type { UserRegisterHandler } from '../handler/user';
|
||||||
|
|
||||||
const _hooks: Record<keyof any, Array<(...args: any[]) => any>> = {};
|
const _hooks: Record<keyof any, Array<(...args: any[]) => any>> = {};
|
||||||
const logger = new Logger('bus', true);
|
const logger = new Logger('bus');
|
||||||
const argv = cac().parse();
|
const argv = cac().parse();
|
||||||
|
|
||||||
function isBailed(value: any) {
|
function isBailed(value: any) {
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import { Duplex } from 'stream';
|
|||||||
import { ObjectID } from 'mongodb';
|
import { ObjectID } from 'mongodb';
|
||||||
import { isMoment } from 'moment';
|
import { isMoment } from 'moment';
|
||||||
import type { Moment } from 'moment-timezone';
|
import type { Moment } from 'moment-timezone';
|
||||||
|
import { inspect } from 'util';
|
||||||
|
|
||||||
declare global {
|
declare global {
|
||||||
interface StringConstructor {
|
interface StringConstructor {
|
||||||
@@ -41,12 +42,16 @@ String.random = function random(digit = 32, dict = defaultDict) {
|
|||||||
|
|
||||||
String.prototype.format = function formatStr(...args) {
|
String.prototype.format = function formatStr(...args) {
|
||||||
let result = this;
|
let result = this;
|
||||||
if (args.length > 0) {
|
if (args.length) {
|
||||||
if (args.length === 1 && typeof (args[0]) === 'object') {
|
if (args.length === 1 && typeof args[0] === 'object') {
|
||||||
for (const key in args) {
|
const t = args[0];
|
||||||
if (args[key] !== undefined) {
|
for (const key in t) {
|
||||||
|
if (!key.startsWith('_') && t[key] !== undefined) {
|
||||||
|
if (t._inspect && typeof t[key] === 'object') {
|
||||||
|
t[key] = inspect(t[key], { colors: process?.stderr?.isTTY });
|
||||||
|
}
|
||||||
const reg = new RegExp(`(\\{${key}\\})`, 'g');
|
const reg = new RegExp(`(\\{${key}\\})`, 'g');
|
||||||
result = result.replace(reg, args[key]);
|
result = result.replace(reg, t[key]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else return this.formatFromArray(args);
|
} else return this.formatFromArray(args);
|
||||||
|
|||||||
Reference in new issue
Block a user