mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
feat: add winston-based logger
This commit is contained in:
committed by
Ruben Taelman
parent
b0671031f1
commit
5b825bc2d4
19
src/logging/WinstonLogger.ts
Normal file
19
src/logging/WinstonLogger.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import type { Logger as WinstonInnerLogger } from 'winston';
|
||||
import type { Logger } from './Logger';
|
||||
import type { LogLevel } from './LogLevel';
|
||||
|
||||
/**
|
||||
* A WinstonLogger implements the {@link Logger} interface using a given winston logger.
|
||||
*/
|
||||
export class WinstonLogger implements Logger {
|
||||
private readonly logger: WinstonInnerLogger;
|
||||
|
||||
public constructor(logger: WinstonInnerLogger) {
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
public log(level: LogLevel, message: string, meta?: any): this {
|
||||
this.logger.log(level, message, meta);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
37
src/logging/WinstonLoggerFactory.ts
Normal file
37
src/logging/WinstonLoggerFactory.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import { createLogger, format, transports } from 'winston';
|
||||
import type * as Transport from 'winston-transport';
|
||||
import type { Logger } from './Logger';
|
||||
import type { LoggerFactory } from './LoggerFactory';
|
||||
import { WinstonLogger } from './WinstonLogger';
|
||||
|
||||
/**
|
||||
* Uses the winston library to create loggers for the given logging level.
|
||||
* By default, it will print to the console with colorized logging levels.
|
||||
*
|
||||
* This creates instances of {@link WinstonLogger}.
|
||||
*/
|
||||
export class WinstonLoggerFactory implements LoggerFactory {
|
||||
private readonly level: string;
|
||||
|
||||
public constructor(level: string) {
|
||||
this.level = level;
|
||||
}
|
||||
|
||||
public createLogger(label: string): Logger {
|
||||
return new WinstonLogger(createLogger({
|
||||
level: this.level,
|
||||
format: format.combine(
|
||||
format.label({ label }),
|
||||
format.colorize(),
|
||||
format.timestamp(),
|
||||
format.printf(({ level: levelInner, message, label: labelInner, timestamp }: {[key: string]: any}): string =>
|
||||
`${timestamp} [${labelInner}] ${levelInner}: ${message}`),
|
||||
),
|
||||
transports: this.createTransports(),
|
||||
}));
|
||||
}
|
||||
|
||||
protected createTransports(): Transport[] {
|
||||
return [ new transports.Console() ];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user