Repository navigation
Expand file tree
/
Copy pathlogger.ts
More file actions
33 lines (29 loc) · 1.31 KB
/
Copy pathlogger.ts
File metadata and controls
33 lines (29 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import { createLogger, LoggerOptions, transports, format } from 'winston';
import Timer from './timer';
const { combine, timestamp, colorize, printf } = format;
const options: LoggerOptions = {
format: combine(
colorize(),
timestamp(),
printf((info) => `${info.timestamp} ${info.level}: ${info.message}`),
),
transports: [
new transports.Console({
level: process.env.NODE_ENV === 'production' ? 'info' : 'debug',
}),
],
};
const logger = createLogger(options);
export default (service: string, timer?: ReturnType<typeof Timer>) => {
const buildServiceMessage = (...messages: (string | number | object)[]) => {
const timeLog = timer ? ` (${timer.time()} | ${timer.step()})` : '';
const strings: (string | number)[] = messages.map((m) => (typeof m === 'object' ? JSON.stringify(m, null, 2) : m));
return `[${service}${timeLog}] ${strings.join(' - ')}`;
};
return {
debug: (...messages: (string | number | object)[]) => logger.debug(buildServiceMessage(...messages)),
info: (...messages: (string | number | object)[]) => logger.info(buildServiceMessage(...messages)),
warn: (...messages: (string | number | object)[]) => logger.warn(buildServiceMessage(...messages)),
error: (message: string, ...meta: any[]) => logger.error(buildServiceMessage(message), ...meta),
};
};