mirror of
https://activitypub.software/TransFem-org/Sharkey.git
synced 2024-12-14 01:50:45 +01:00
improve nest logger
This commit is contained in:
parent
b4b742ca89
commit
2067180328
4 changed files with 68 additions and 13 deletions
49
packages/backend/src/NestLogger.ts
Normal file
49
packages/backend/src/NestLogger.ts
Normal file
|
@ -0,0 +1,49 @@
|
|||
import { LoggerService } from '@nestjs/common';
|
||||
import Logger from '@/logger.js';
|
||||
|
||||
const logger = new Logger('core', 'cyan');
|
||||
const nestLogger = logger.createSubLogger('nest', 'green', false);
|
||||
|
||||
export class NestLogger implements LoggerService {
|
||||
/**
|
||||
* Write a 'log' level log.
|
||||
*/
|
||||
log(message: any, ...optionalParams: any[]) {
|
||||
const ctx = optionalParams[0];
|
||||
nestLogger.info(ctx + ': ' + message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write an 'error' level log.
|
||||
*/
|
||||
error(message: any, ...optionalParams: any[]) {
|
||||
const ctx = optionalParams[0];
|
||||
nestLogger.error(ctx + ': ' + message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write a 'warn' level log.
|
||||
*/
|
||||
warn(message: any, ...optionalParams: any[]) {
|
||||
const ctx = optionalParams[0];
|
||||
nestLogger.warn(ctx + ': ' + message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write a 'debug' level log.
|
||||
*/
|
||||
debug?(message: any, ...optionalParams: any[]) {
|
||||
if (process.env.NODE_ENV === 'production') return;
|
||||
const ctx = optionalParams[0];
|
||||
nestLogger.debug(ctx + ': ' + message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write a 'verbose' level log.
|
||||
*/
|
||||
verbose?(message: any, ...optionalParams: any[]) {
|
||||
if (process.env.NODE_ENV === 'production') return;
|
||||
const ctx = optionalParams[0];
|
||||
nestLogger.debug(ctx + ': ' + message);
|
||||
}
|
||||
}
|
|
@ -16,6 +16,7 @@ import { DaemonModule } from '@/daemons/DaemonModule.js';
|
|||
import { JanitorService } from '@/daemons/JanitorService.js';
|
||||
import { QueueStatsService } from '@/daemons/QueueStatsService.js';
|
||||
import { ServerStatsService } from '@/daemons/ServerStatsService.js';
|
||||
import { NestLogger } from '@/NestLogger.js';
|
||||
import { envOption } from '../env.js';
|
||||
|
||||
const _filename = fileURLToPath(import.meta.url);
|
||||
|
@ -78,7 +79,9 @@ export async function masterMain() {
|
|||
bootLogger.succ(`Now listening on port ${config.port} on ${config.url}`, null, true);
|
||||
|
||||
if (!envOption.noDaemons) {
|
||||
const daemons = await NestFactory.createApplicationContext(DaemonModule);
|
||||
const daemons = await NestFactory.createApplicationContext(DaemonModule, {
|
||||
logger: new NestLogger(),
|
||||
});
|
||||
daemons.enableShutdownHooks();
|
||||
daemons.get(JanitorService).start();
|
||||
daemons.get(QueueStatsService).start();
|
||||
|
|
|
@ -4,13 +4,16 @@ import { envOption } from '@/env.js';
|
|||
import { ChartManagementService } from '@/core/chart/ChartManagementService.js';
|
||||
import { ServerService } from '@/server/ServerService.js';
|
||||
import { QueueProcessorService } from '@/queue/QueueProcessorService.js';
|
||||
import { NestLogger } from '@/NestLogger.js';
|
||||
import { RootModule } from '../RootModule.js';
|
||||
|
||||
/**
|
||||
* Init worker process
|
||||
*/
|
||||
export async function workerMain() {
|
||||
const app = await NestFactory.createApplicationContext(RootModule);
|
||||
const app = await NestFactory.createApplicationContext(RootModule, {
|
||||
logger: new NestLogger(),
|
||||
});
|
||||
app.enableShutdownHooks();
|
||||
|
||||
// start server
|
||||
|
|
|
@ -5,7 +5,7 @@ import { format as dateFormat } from 'date-fns';
|
|||
import { bindThis } from '@/decorators.js';
|
||||
import { envOption } from './env.js';
|
||||
|
||||
type Domain = {
|
||||
type Context = {
|
||||
name: string;
|
||||
color?: string;
|
||||
};
|
||||
|
@ -13,14 +13,14 @@ type Domain = {
|
|||
type Level = 'error' | 'success' | 'warning' | 'debug' | 'info';
|
||||
|
||||
export default class Logger {
|
||||
private domain: Domain;
|
||||
private context: Context;
|
||||
private parentLogger: Logger | null = null;
|
||||
private store: boolean;
|
||||
private syslogClient: any | null = null;
|
||||
|
||||
constructor(domain: string, color?: string, store = true, syslogClient = null) {
|
||||
this.domain = {
|
||||
name: domain,
|
||||
constructor(context: string, color?: string, store = true, syslogClient = null) {
|
||||
this.context = {
|
||||
name: context,
|
||||
color: color,
|
||||
};
|
||||
this.store = store;
|
||||
|
@ -28,20 +28,20 @@ export default class Logger {
|
|||
}
|
||||
|
||||
@bindThis
|
||||
public createSubLogger(domain: string, color?: string, store = true): Logger {
|
||||
const logger = new Logger(domain, color, store);
|
||||
public createSubLogger(context: string, color?: string, store = true): Logger {
|
||||
const logger = new Logger(context, color, store);
|
||||
logger.parentLogger = this;
|
||||
return logger;
|
||||
}
|
||||
|
||||
@bindThis
|
||||
private log(level: Level, message: string, data?: Record<string, any> | null, important = false, subDomains: Domain[] = [], store = true): void {
|
||||
private log(level: Level, message: string, data?: Record<string, any> | null, important = false, subContexts: Context[] = [], store = true): void {
|
||||
if (envOption.quiet) return;
|
||||
if (!this.store) store = false;
|
||||
if (level === 'debug') store = false;
|
||||
|
||||
if (this.parentLogger) {
|
||||
this.parentLogger.log(level, message, data, important, [this.domain].concat(subDomains), store);
|
||||
this.parentLogger.log(level, message, data, important, [this.context].concat(subContexts), store);
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -54,7 +54,7 @@ export default class Logger {
|
|||
level === 'debug' ? chalk.gray('VERB') :
|
||||
level === 'info' ? chalk.blue('INFO') :
|
||||
null;
|
||||
const domains = [this.domain].concat(subDomains).map(d => d.color ? chalk.rgb(...convertColor.keyword.rgb(d.color))(d.name) : chalk.white(d.name));
|
||||
const contexts = [this.context].concat(subContexts).map(d => d.color ? chalk.rgb(...convertColor.keyword.rgb(d.color))(d.name) : chalk.white(d.name));
|
||||
const m =
|
||||
level === 'error' ? chalk.red(message) :
|
||||
level === 'warning' ? chalk.yellow(message) :
|
||||
|
@ -63,7 +63,7 @@ export default class Logger {
|
|||
level === 'info' ? message :
|
||||
null;
|
||||
|
||||
let log = `${l} ${worker}\t[${domains.join(' ')}]\t${m}`;
|
||||
let log = `${l} ${worker}\t[${contexts.join(' ')}]\t${m}`;
|
||||
if (envOption.withLogTime) log = chalk.gray(time) + ' ' + log;
|
||||
|
||||
console.log(important ? chalk.bold(log) : log);
|
||||
|
|
Loading…
Reference in a new issue