forked from animo/aries-framework-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.ts
91 lines (74 loc) · 2.45 KB
/
logger.ts
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/* eslint-disable @typescript-eslint/no-explicit-any */
import { ILogObject, Logger } from 'tslog'
import { LogLevel } from '../logger'
import { BaseLogger } from '../logger/BaseLogger'
import { appendFileSync } from 'fs'
function logToTransport(logObject: ILogObject) {
appendFileSync('logs.txt', JSON.stringify(logObject) + '\n')
}
export class TestLogger extends BaseLogger {
private logger: Logger
// Map our log levels to tslog levels
private tsLogLevelMap = {
[LogLevel.test]: 'silly',
[LogLevel.trace]: 'trace',
[LogLevel.debug]: 'debug',
[LogLevel.info]: 'info',
[LogLevel.warn]: 'warn',
[LogLevel.error]: 'error',
[LogLevel.fatal]: 'fatal',
} as const
public constructor(logLevel: LogLevel) {
super(logLevel)
this.logger = new Logger({
minLevel: this.logLevel == LogLevel.off ? undefined : this.tsLogLevelMap[this.logLevel],
ignoreStackLevels: 5,
attachedTransports: [
{
transportLogger: {
silly: logToTransport,
debug: logToTransport,
trace: logToTransport,
info: logToTransport,
warn: logToTransport,
error: logToTransport,
fatal: logToTransport,
},
// always log to file
minLevel: 'silly',
},
],
})
}
private log(level: Exclude<LogLevel, LogLevel.off>, message: string, data?: Record<string, any>): void {
const tsLogLevel = this.tsLogLevelMap[level]
if (data) {
this.logger[tsLogLevel](message, data)
} else {
this.logger[tsLogLevel](message)
}
}
public test(message: string, data?: Record<string, any>): void {
this.log(LogLevel.test, message, data)
}
public trace(message: string, data?: Record<string, any>): void {
this.log(LogLevel.trace, message, data)
}
public debug(message: string, data?: Record<string, any>): void {
this.log(LogLevel.debug, message, data)
}
public info(message: string, data?: Record<string, any>): void {
this.log(LogLevel.info, message, data)
}
public warn(message: string, data?: Record<string, any>): void {
this.log(LogLevel.warn, message, data)
}
public error(message: string, data?: Record<string, any>): void {
this.log(LogLevel.error, message, data)
}
public fatal(message: string, data?: Record<string, any>): void {
this.log(LogLevel.fatal, message, data)
}
}
const testLogger = new TestLogger(LogLevel.error)
export default testLogger