-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlogger.ts
31 lines (28 loc) · 1.09 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
import winston from "winston";
import * as fs from "fs";
const { combine, timestamp, label, printf } = winston.format;
export function createNewLatest() {
if (fs.existsSync(process.cwd() + '/logs/latest.log')) {
const date = new Date(Date.now()).toJSON().slice(2, 10) + '.'
+ new Date(Date.now()).getHours() + '.'
+ new Date(Date.now()).getMinutes();
fs.renameSync(process.cwd() + '/logs/latest.log', process.cwd() + '/logs/' + date + '.log');
}
}
export function createLogger(options?: { label?: string }) {
const debug = process.env.DEBUG === 'true';
return winston.createLogger({
level: debug ? 'debug' : 'info',
format: combine(
label({ label: options?.label ?? 'NSM' }),
timestamp(),
printf(({ level, message, label, timestamp }) => {
return `${timestamp} [${label}] ${level}: ${message}`;
})
),
transports: [
new winston.transports.Console(),
new winston.transports.File({dirname: 'logs', filename: 'latest.log'})
]
});
}