Skip to content

Commit

Permalink
Configure winston only once
Browse files Browse the repository at this point in the history
Moves winston configuration into body of logger factory -- winston was being re-configured after every middleware pipeline invocation, resulting in memory leaks and duplicate log output.
  • Loading branch information
steve-a-jones authored Nov 26, 2018
1 parent f6dffb0 commit 9121f90
Showing 1 changed file with 17 additions and 17 deletions.
34 changes: 17 additions & 17 deletions src/logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,22 @@ import * as Koa from 'koa';
import { config } from './config';
import * as winston from 'winston';

export function logger(winstonInstance) {
export function logger(winstonInstance) {
winstonInstance.configure({
level: config.debugLogging ? 'debug' : 'info',
transports: [
//
// - Write all logs error (and below) to `error.log`.
new winston.transports.File({ filename: 'error.log', level: 'error' }),
//
// - Write to all logs with specified level to console.
new winston.transports.Console({ format: winston.format.combine(
winston.format.colorize(),
winston.format.simple()
) })
]
});

return async(ctx: Koa.Context, next: () => Promise<any>) => {

const start = new Date().getMilliseconds();
Expand All @@ -24,21 +39,6 @@ export function logger(winstonInstance) {

const msg: string = `${ctx.method} ${ctx.originalUrl} ${ctx.status} ${ms}ms`;

winstonInstance.configure({
level: config.debugLogging ? 'debug' : 'info',
transports: [
//
// - Write all logs error (and below) to `error.log`.
new winston.transports.File({ filename: 'error.log', level: 'error' }),
//
// - Write to all logs with specified level to console.
new winston.transports.Console({ format: winston.format.combine(
winston.format.colorize(),
winston.format.simple()
) })
]
});

winstonInstance.log(logLevel, msg);
};
}
}

0 comments on commit 9121f90

Please sign in to comment.