This repository has been archived by the owner on Dec 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.js
67 lines (62 loc) · 2.07 KB
/
logger.js
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
"use strict";
var appRoot = require('app-root-path')
const winston = require('winston')
const config = require('./config.json')
const { format, createLogger, transports } = winston
const fs = require('fs')
// If the logs folder doesn't exist, create it.
if (!fs.existsSync(`${appRoot}/logs/`)) {
fs.mkdirSync(`${appRoot}/logs/`);
}
if (config.logName === undefined) {
throw new Error(`The property config.logName is not set in config.json.`);
} else if (config.logErrorName === undefined) {
throw new Error(`The property config.logErrorName is not set in config.json.`);
}
// define the custom settings for each transport (file, console)
const options = {
fileInfos: {
name: 'info-log',
filename: `${appRoot}/logs/${config.logName}`,
level: config.logLevel,
format: format.combine(
format.timestamp({format: 'YYYY-MM-DD HH:mm:ss'}),
format.printf(info => `${info.timestamp} ${info.level}: ${info.message}`)
),
maxsize: 500*1000, // 500KB
maxFiles: 2,
tailable: true // The filename will always have the most recent log lines. The larger the appended number, the older the log file.
},
fileWarns: {
name: 'warning-log',
level: 'warn',
format: format.combine(
format.timestamp({format: 'YYYY-MM-DD HH:mm:ss'}),
format.printf(info => `${info.timestamp} ${info.level}: ${info.message}`)
),
filename: `${appRoot}/logs/${config.logErrorName}`,
maxsize: 500*1000, // 500KB
maxFiles: 2,
tailable: true // The filename will always have the most recent log lines. The larger the appended number, the older the log file.
},
console: {
format: format.combine(
format.timestamp({
format: 'YYYY-MM-DD HH:mm:ss'
}),
format.colorize(),
format.printf(info => `${info.timestamp} ${info.level}: ${info.message}`)
),
level: 'debug'
}
}
// instantiate a new Winston Logger with the settings defined above
const logger = createLogger({
transports: [
// new winston.transports.File(options.file),
new transports.File(options.fileInfos),
new transports.File(options.fileWarns),
new transports.Console(options.console)
]
})
module.exports = logger;