mirror of
https://activitypub.software/TransFem-org/Sharkey.git
synced 2024-12-14 06:00:44 +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 { JanitorService } from '@/daemons/JanitorService.js';
|
||||||
import { QueueStatsService } from '@/daemons/QueueStatsService.js';
|
import { QueueStatsService } from '@/daemons/QueueStatsService.js';
|
||||||
import { ServerStatsService } from '@/daemons/ServerStatsService.js';
|
import { ServerStatsService } from '@/daemons/ServerStatsService.js';
|
||||||
|
import { NestLogger } from '@/NestLogger.js';
|
||||||
import { envOption } from '../env.js';
|
import { envOption } from '../env.js';
|
||||||
|
|
||||||
const _filename = fileURLToPath(import.meta.url);
|
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);
|
bootLogger.succ(`Now listening on port ${config.port} on ${config.url}`, null, true);
|
||||||
|
|
||||||
if (!envOption.noDaemons) {
|
if (!envOption.noDaemons) {
|
||||||
const daemons = await NestFactory.createApplicationContext(DaemonModule);
|
const daemons = await NestFactory.createApplicationContext(DaemonModule, {
|
||||||
|
logger: new NestLogger(),
|
||||||
|
});
|
||||||
daemons.enableShutdownHooks();
|
daemons.enableShutdownHooks();
|
||||||
daemons.get(JanitorService).start();
|
daemons.get(JanitorService).start();
|
||||||
daemons.get(QueueStatsService).start();
|
daemons.get(QueueStatsService).start();
|
||||||
|
|
|
@ -4,13 +4,16 @@ import { envOption } from '@/env.js';
|
||||||
import { ChartManagementService } from '@/core/chart/ChartManagementService.js';
|
import { ChartManagementService } from '@/core/chart/ChartManagementService.js';
|
||||||
import { ServerService } from '@/server/ServerService.js';
|
import { ServerService } from '@/server/ServerService.js';
|
||||||
import { QueueProcessorService } from '@/queue/QueueProcessorService.js';
|
import { QueueProcessorService } from '@/queue/QueueProcessorService.js';
|
||||||
|
import { NestLogger } from '@/NestLogger.js';
|
||||||
import { RootModule } from '../RootModule.js';
|
import { RootModule } from '../RootModule.js';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Init worker process
|
* Init worker process
|
||||||
*/
|
*/
|
||||||
export async function workerMain() {
|
export async function workerMain() {
|
||||||
const app = await NestFactory.createApplicationContext(RootModule);
|
const app = await NestFactory.createApplicationContext(RootModule, {
|
||||||
|
logger: new NestLogger(),
|
||||||
|
});
|
||||||
app.enableShutdownHooks();
|
app.enableShutdownHooks();
|
||||||
|
|
||||||
// start server
|
// start server
|
||||||
|
|
|
@ -5,7 +5,7 @@ import { format as dateFormat } from 'date-fns';
|
||||||
import { bindThis } from '@/decorators.js';
|
import { bindThis } from '@/decorators.js';
|
||||||
import { envOption } from './env.js';
|
import { envOption } from './env.js';
|
||||||
|
|
||||||
type Domain = {
|
type Context = {
|
||||||
name: string;
|
name: string;
|
||||||
color?: string;
|
color?: string;
|
||||||
};
|
};
|
||||||
|
@ -13,14 +13,14 @@ type Domain = {
|
||||||
type Level = 'error' | 'success' | 'warning' | 'debug' | 'info';
|
type Level = 'error' | 'success' | 'warning' | 'debug' | 'info';
|
||||||
|
|
||||||
export default class Logger {
|
export default class Logger {
|
||||||
private domain: Domain;
|
private context: Context;
|
||||||
private parentLogger: Logger | null = null;
|
private parentLogger: Logger | null = null;
|
||||||
private store: boolean;
|
private store: boolean;
|
||||||
private syslogClient: any | null = null;
|
private syslogClient: any | null = null;
|
||||||
|
|
||||||
constructor(domain: string, color?: string, store = true, syslogClient = null) {
|
constructor(context: string, color?: string, store = true, syslogClient = null) {
|
||||||
this.domain = {
|
this.context = {
|
||||||
name: domain,
|
name: context,
|
||||||
color: color,
|
color: color,
|
||||||
};
|
};
|
||||||
this.store = store;
|
this.store = store;
|
||||||
|
@ -28,20 +28,20 @@ export default class Logger {
|
||||||
}
|
}
|
||||||
|
|
||||||
@bindThis
|
@bindThis
|
||||||
public createSubLogger(domain: string, color?: string, store = true): Logger {
|
public createSubLogger(context: string, color?: string, store = true): Logger {
|
||||||
const logger = new Logger(domain, color, store);
|
const logger = new Logger(context, color, store);
|
||||||
logger.parentLogger = this;
|
logger.parentLogger = this;
|
||||||
return logger;
|
return logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
@bindThis
|
@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 (envOption.quiet) return;
|
||||||
if (!this.store) store = false;
|
if (!this.store) store = false;
|
||||||
if (level === 'debug') store = false;
|
if (level === 'debug') store = false;
|
||||||
|
|
||||||
if (this.parentLogger) {
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -54,7 +54,7 @@ export default class Logger {
|
||||||
level === 'debug' ? chalk.gray('VERB') :
|
level === 'debug' ? chalk.gray('VERB') :
|
||||||
level === 'info' ? chalk.blue('INFO') :
|
level === 'info' ? chalk.blue('INFO') :
|
||||||
null;
|
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 =
|
const m =
|
||||||
level === 'error' ? chalk.red(message) :
|
level === 'error' ? chalk.red(message) :
|
||||||
level === 'warning' ? chalk.yellow(message) :
|
level === 'warning' ? chalk.yellow(message) :
|
||||||
|
@ -63,7 +63,7 @@ export default class Logger {
|
||||||
level === 'info' ? message :
|
level === 'info' ? message :
|
||||||
null;
|
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;
|
if (envOption.withLogTime) log = chalk.gray(time) + ' ' + log;
|
||||||
|
|
||||||
console.log(important ? chalk.bold(log) : log);
|
console.log(important ? chalk.bold(log) : log);
|
||||||
|
|
Loading…
Reference in a new issue