zigbee2mqtt/lib/util/logger.js

55 lines
2.1 KiB
JavaScript
Raw Normal View History

2018-04-18 09:25:40 -07:00
const winston = require('winston');
2018-08-07 23:33:53 -07:00
const moment = require('moment');
2018-05-11 20:04:15 -07:00
const data = require('./data');
const settings = require('./settings');
const path = require('path');
const fs = require('fs');
2018-06-14 11:27:43 -07:00
let logLevel = '';
2018-06-13 23:02:07 -07:00
if (process.env.DEBUG) {
logLevel = 'debug';
2018-06-14 11:27:43 -07:00
} else if (settings.get().advanced && settings.get().advanced.log_level) {
logLevel = settings.get().advanced.log_level;
} else {
logLevel = winston.level.info;
2018-06-13 23:02:07 -07:00
}
2018-06-14 11:27:43 -07:00
const logDirectory = settings.get().advanced && settings.get().advanced.log_directory ?
settings.get().advanced.log_directory : data.getPath();
if (settings.get().advanced && settings.get().advanced.log_filename) {
filename = settings.get().advanced.log_filename ? settings.get().advanced.log_filename : '[log-]%filenamedateformat%';
filenamedateformat = settings.get().advanced && settings.get().advanced.log_filename_date_format ? settings.get().advanced.log_filename_date_format : 'YYYY-MM-DD';
completefilename = moment(new Date()).format(filename.replace('%filenamedateformat%', filenamedateformat));
} else {
filename = 'log';
completefilename = filename;
}
2018-04-18 09:25:40 -07:00
const logger = new (winston.Logger)({
transports: [
2018-05-04 14:01:02 -07:00
new (winston.transports.File)({
filename: path.join(logDirectory, completefilename + '.txt'),
2018-05-04 14:01:02 -07:00
json: false,
level: logLevel,
2018-05-04 14:01:02 -07:00
maxFiles: 3,
maxsize: 10000000, // 10MB
timestamp: () => new Date().toLocaleString(),
2018-05-04 14:01:02 -07:00
}),
2018-04-18 09:25:40 -07:00
new (winston.transports.Console)({
timestamp: () => new Date().toLocaleString(),
formatter: function(options) {
return winston.config.colorize(options.level, ' zigbee2mqtt:' + options.level.toLowerCase()) + ' ' +
2018-08-10 08:08:08 -07:00
options.timestamp() + ' ' + (options.message ? options.message : '') +
(options.meta && Object.keys(options.meta).length ? '\n\t'+ JSON.stringify(options.meta) : '' );
2018-05-17 08:20:46 -07:00
},
}),
],
2018-04-18 09:25:40 -07:00
});
2018-06-14 11:27:43 -07:00
logger.info(`Logging to directory: '${logDirectory}' with filename: '${completefilename}.txt'`);
logger.transports.console.level = logLevel;
2018-04-18 09:25:40 -07:00
module.exports = logger;