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');
|
2018-06-13 13:55:52 -07:00
|
|
|
const settings = require('./settings');
|
2018-08-05 09:55:26 -07:00
|
|
|
const path = require('path');
|
2018-08-12 11:39:06 -07:00
|
|
|
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
|
|
|
|
2018-08-05 09:55:26 -07:00
|
|
|
const logDirectory = settings.get().advanced && settings.get().advanced.log_directory ?
|
2018-08-12 11:39:06 -07:00
|
|
|
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-08-05 09:55:26 -07:00
|
|
|
|
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)({
|
2018-08-12 11:39:06 -07:00
|
|
|
filename: path.join(logDirectory, completefilename + '.txt'),
|
2018-05-04 14:01:02 -07:00
|
|
|
json: false,
|
2018-06-13 13:55:52 -07:00
|
|
|
level: logLevel,
|
2018-05-04 14:01:02 -07:00
|
|
|
maxFiles: 3,
|
|
|
|
maxsize: 10000000, // 10MB
|
2018-08-16 09:56:33 -07:00
|
|
|
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) {
|
2018-08-09 11:21:10 -07:00
|
|
|
return winston.config.colorize(options.level, ' zigbee2mqtt:' + options.level.toLowerCase()) + ' ' +
|
2018-08-10 08:08:08 -07:00
|
|
|
options.timestamp() + ' ' + (options.message ? options.message : '') +
|
2018-08-09 11:21:10 -07:00
|
|
|
(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
|
|
|
|
2018-08-12 11:39:06 -07:00
|
|
|
logger.info(`Logging to directory: '${logDirectory}' with filename: '${completefilename}.txt'`);
|
2018-08-05 09:55:26 -07:00
|
|
|
|
2018-06-13 13:55:52 -07:00
|
|
|
logger.transports.console.level = logLevel;
|
2018-04-18 09:25:40 -07:00
|
|
|
|
|
|
|
module.exports = logger;
|