Skip to content

Instantly share code, notes, and snippets.

@greg-hoarau
Created May 21, 2019 08:39
Show Gist options
  • Save greg-hoarau/d1b0d579b53bb893644d4357b997d0b3 to your computer and use it in GitHub Desktop.
Save greg-hoarau/d1b0d579b53bb893644d4357b997d0b3 to your computer and use it in GitHub Desktop.
Typescript Logger Test
interface ILogger {
info(toLog: string | object, ...meta: object[]): ILogger;
warn(toLog: string | object, ...meta: object[]): ILogger;
error(toLog: string | object, ...meta: object[]): ILogger;
debug(toLog: string | object, ...meta: object[]): ILogger;
}
export { ILogger };
import path from 'path';
import { createLogger, format, Logger, transports } from 'winston';
import { ILogger } from '../../interfaces/logging/ILogger';
class LoggerImplWinston implements ILogger {
private readonly _winston: Logger;
constructor() {
this._winston = createLogger({
level: 'DEBUG', // Should come from config file
format: format.combine(
format.label({
label: path.basename(process.mainModule!.filename),
}),
format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' })
),
transports: [
new transports.Console({
format: format.combine(
format.colorize(),
format.printf(
info =>
`${info.timestamp} ${info.level} [${info.label}]: ${
info.message
}`
)
),
}),
new transports.File({
filename: 'logs/log.txt',
format: format.json(),
}),
],
});
}
public debug(toLog: string | object): ILogger {
if (typeof toLog === 'string') {
this._winston.debug(toLog);
} else {
this._winston.debug(toLog);
}
return this;
}
public info(toLog: string | object, meta: object | undefined): ILogger {
if (typeof toLog === 'string') {
this._winston.info(toLog, meta);
} else {
this._winston.info(toLog);
}
return this;
}
public warn(toLog: string | object): ILogger {
if (typeof toLog === 'string') {
this._winston.warn(toLog);
} else {
this._winston.warn(toLog);
}
return this;
}
public error(toLog: string | object): ILogger {
if (typeof toLog === 'string') {
this._winston.error(toLog);
} else {
this._winston.error(toLog);
}
return this;
}
}
export { LoggerImplWinston };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment