1 | import * as pad from 'pad';
|
2 | import { Logger, createLogger, format, transports } from 'winston';
|
3 | import { OUTPUT_DIR } from '.';
|
4 | import { trim } from './util';
|
5 |
|
6 | const { combine, timestamp, printf } = format;
|
7 |
|
8 | const trimAndPadLeft = (text: string, length: number): string =>
|
9 | pad(trim(text, length, '…'), length);
|
10 |
|
11 | const trimAndPadRight = (text: string, length: number): string =>
|
12 | pad(length, trim(text, length, '…'));
|
13 |
|
14 | const 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 |
|
27 | export 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 |
|
40 | export interface LoggingContext {
|
41 | scenario?: string;
|
42 | action?: string;
|
43 | }
|
44 |
|
\ | No newline at end of file |