Total Complexity | 4 |
Complexity/F | 2 |
Lines of Code | 37 |
Function Count | 2 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | /* eslint-disable sort-keys */ |
||
2 | |||
3 | import dayjs from 'dayjs'; |
||
4 | import merge from 'deepmerge'; |
||
5 | import winston, { Logger } from 'winston'; |
||
6 | |||
7 | export type LogTarget = 'console' | 'file'; |
||
8 | |||
9 | export class HasLogger { |
||
10 | public logger!: Logger; |
||
11 | |||
12 | public createLogger(targets: LogTarget[], defaultMeta: Record<string, any>) { |
||
13 | this.logger = winston.createLogger({ |
||
14 | level: 'info', |
||
15 | format: winston.format.json(), |
||
16 | defaultMeta, |
||
17 | transports: [], |
||
18 | }); |
||
19 | |||
20 | if (targets.includes('file')) { |
||
21 | new winston.transports.File({ filename: `${__dirname}/codeboost.log` }); |
||
22 | } |
||
23 | |||
24 | //if (targets.includes('console')) { |
||
25 | this.logger.add(new winston.transports.Console({ format: winston.format.simple() })); |
||
26 | //} |
||
27 | } |
||
28 | |||
29 | public log(message: string, meta: any[] = []) { |
||
30 | if (!this.logger || this.logger.transports.length === 0) { |
||
31 | return; |
||
32 | } |
||
33 | |||
34 | this.logger.info(message, ...[merge.all([{ _ts: dayjs().toISOString() }, ...meta])]); |
||
35 | } |
||
36 | } |
||
37 |