Skip to content

Commit 9121f90

Browse files
Configure winston only once
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.
1 parent f6dffb0 commit 9121f90

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed

src/logging.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,22 @@ import * as Koa from 'koa';
22
import { config } from './config';
33
import * as winston from 'winston';
44

5-
export function logger(winstonInstance) {
5+
export function logger(winstonInstance) {
6+
winstonInstance.configure({
7+
level: config.debugLogging ? 'debug' : 'info',
8+
transports: [
9+
//
10+
// - Write all logs error (and below) to `error.log`.
11+
new winston.transports.File({ filename: 'error.log', level: 'error' }),
12+
//
13+
// - Write to all logs with specified level to console.
14+
new winston.transports.Console({ format: winston.format.combine(
15+
winston.format.colorize(),
16+
winston.format.simple()
17+
) })
18+
]
19+
});
20+
621
return async(ctx: Koa.Context, next: () => Promise<any>) => {
722

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

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

27-
winstonInstance.configure({
28-
level: config.debugLogging ? 'debug' : 'info',
29-
transports: [
30-
//
31-
// - Write all logs error (and below) to `error.log`.
32-
new winston.transports.File({ filename: 'error.log', level: 'error' }),
33-
//
34-
// - Write to all logs with specified level to console.
35-
new winston.transports.Console({ format: winston.format.combine(
36-
winston.format.colorize(),
37-
winston.format.simple()
38-
) })
39-
]
40-
});
41-
4242
winstonInstance.log(logLevel, msg);
4343
};
44-
}
44+
}

0 commit comments

Comments
 (0)