-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.ts
35 lines (29 loc) · 1 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
import winston from "winston";
const logger = winston.createLogger({
level: "debug",
format: winston.format.combine(
winston.format.json(),
winston.format.timestamp(),
winston.format.errors({ stack: true }),
),
transports: [
new winston.transports.File({ filename: newLogFileName("error"), level: "error" }),
new winston.transports.File({ filename: newLogFileName("debug") }),
new winston.transports.Console(),
],
});
function newLogFileName(type: string): string {
const pathSegments = [];
pathSegments.push("logs");
if (process.env.NODE_ENV === "testing") { pathSegments.push("tests") }
pathSegments.push(type);
pathSegments.push(type + "_" + dateToTimestamp());
return pathSegments.join("/") + ".log";
}
function dateToTimestamp(date?: Date): string {
if (!date) {
date = new Date();
}
return date.getFullYear() + "-" + date.getMonth() + "-" + date.getDate() + "-" + date.getTime();
}
export default logger;