src/traits/HasLogger.ts   A
last analyzed

Complexity

Total Complexity 4
Complexity/F 2

Size

Lines of Code 37
Function Count 2

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 37
rs 10
c 0
b 0
f 0
wmc 4
mnd 2
bc 2
fnc 2
bpm 1
cpm 2
noi 0

2 Functions

Rating   Name   Duplication   Size   Complexity  
A HasLogger.log 0 7 2
A HasLogger.createLogger 0 15 2
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