Custom loggers
To use your own logger with @node-ts/bus, create an adapter class by implementing the Logger interface. In this example winston is being used
1
import { Logger } from '@node-ts/bus-core'
2
import winston from 'winston'
3
​
4
export class WinstonLogger implements Logger {
5
​
6
private winstonLogger: winston.Logger
7
​
8
constructor (
9
name: string // This is the name of the class the logger will be injected into
10
) {
11
this.winstonLogger = winston.createLogger({
12
format: winston.format.json(),
13
defaultMeta: { name }
14
})
15
}
16
​
17
debug (message: string, meta?: object): void {
18
this.winstonLogger.debug(message, meta)
19
}
20
​
21
trace (message: string, meta?: object): void {
22
this.winstonLogger.verbose(message, meta)
23
}
24
​
25
info (message: string, meta?: object): void {
26
this.winstonLogger.info(message, meta)
27
}
28
​
29
warn (message: string, meta?: object): void {
30
this.winstonLogger.warn(message, meta)
31
}
32
​
33
error (message: string, meta?: object): void {
34
this.winstonLogger.error(message, meta)
35
}
36
​
37
fatal (message: string, meta?: object): void {
38
this.winstonLogger.crit(message, meta)
39
}
40
}
41
​
Copied!
Configure @node-ts/bus-core to use your log adapter
1
import { Bus } from '@node-ts/bus-core'
2
import { WinstonLogger } from './winston-logger'
3
​
4
async function run () {
5
const bus = await Bus.configure()
6
.withLogger((target: string) => new WinstonLogger(target))
7
.initialize()
8
}
Copied!
Copy link