UNPKG

1.25 kBPlain TextView Raw
1import * as pad from 'pad';
2import { Logger, createLogger, format, transports } from 'winston';
3import { OUTPUT_DIR } from '.';
4import { trim } from './util';
5
6const { combine, timestamp, printf } = format;
7
8const trimAndPadLeft = (text: string, length: number): string =>
9 pad(trim(text, length, '…'), length);
10
11const trimAndPadRight = (text: string, length: number): string =>
12 pad(length, trim(text, length, '…'));
13
14const myFormat = printf(
15 (info): string =>
16 `${info.timestamp} | ${trimAndPadLeft(
17 info.scenario || 'no scenario',
18 15,
19 )} | ${trimAndPadLeft(
20 info.action || 'no action',
21 15,
22 )} | ${trimAndPadRight(`${info.level.toUpperCase()}`, 5)} | ${
23 info.message
24 }`,
25);
26
27export const getLogger = (scenario = 'unknown'): Logger =>
28 createLogger({
29 level: 'debug',
30 format: combine(timestamp(), myFormat),
31 transports: [
32 new transports.Console({ level: 'info' }),
33 new transports.File({
34 filename: `${OUTPUT_DIR()}/${scenario}.log`,
35 level: 'debug',
36 }),
37 ],
38 });
39
40export interface LoggingContext {
41 scenario?: string;
42 action?: string;
43}
44
\No newline at end of file