-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy-logger.ts
53 lines (49 loc) · 1.65 KB
/
my-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
import { LoggerService, LogLevel } from '@nestjs/common';
import { createLogger, format, Logger, transports } from 'winston';
import * as chalk from 'chalk';
import * as dayjs from 'dayjs';
export class MyLogger implements LoggerService {
private logger: Logger;
constructor() {
this.logger = createLogger({
level: 'debug',
// format: format.combine(
// format.colorize(),
// format.simple()
// ),
transports: [
new transports.Console({
format: format.combine(
format.colorize(),
format.printf(({context, level, message, time, name}) => {
const appStr = chalk.green('[NEST]');
const contextStr = chalk.yellow(`[${context}]`);
const nameStr = chalk.blue(`${name || '暂无'}`);
return `${nameStr} =>${appStr} ${time} ${level} ${contextStr} ${message}`;
})
)
}),
new transports.File({
format: format.combine(
format.timestamp(),
format.json()
),
filename: 'test.log',
dirname: 'log',
})
]
});
}
log(message: string, context: string) {
const time = dayjs(Date.now()).format('YYYY-MM-DD HH:mm:ss');
this.logger.log('info', message, { context, time, name: 123 });
}
error(message: any, context: string) {
const time = dayjs(Date.now()).format('YYYY-MM-DD HH:mm:ss');
this.logger.log('error', message, { context, time });
}
warn(message: any, context: string) {
const time = dayjs(Date.now()).format('YYYY-MM-DD HH:mm:ss');
this.logger.log('warn', message, { context, time });
}
}