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-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-08-16 11:04:29 -07:00
|
|
|
const fx = require('mkdir-recursive');
|
|
|
|
const rimraf = require('rimraf');
|
2018-06-14 11:27:43 -07:00
|
|
|
|
2018-08-16 11:04:29 -07:00
|
|
|
// Determine the log level.
|
2018-09-18 08:51:34 -07:00
|
|
|
const level = settings.get().advanced.log_level;
|
2018-06-14 11:27:43 -07:00
|
|
|
|
2018-08-16 11:04:29 -07:00
|
|
|
// Directoy to log to
|
2018-09-18 08:51:34 -07:00
|
|
|
const timestamp = moment(Date.now()).format('YYYY-MM-DD.HH-mm-ss');
|
|
|
|
const directory = settings.get().advanced.log_directory.replace('%TIMESTAMP%', timestamp);
|
2018-08-16 11:04:29 -07:00
|
|
|
|
|
|
|
// Make sure that log directoy exsists
|
|
|
|
fx.mkdirSync(directory);
|
|
|
|
|
|
|
|
// Create logger
|
2019-03-08 09:39:47 -07:00
|
|
|
const logger = new winston.Logger({
|
2018-04-18 09:25:40 -07:00
|
|
|
transports: [
|
2019-03-08 09:39:47 -07:00
|
|
|
new winston.transports.File({
|
2018-08-16 11:04:29 -07:00
|
|
|
filename: path.join(directory, 'log.txt'),
|
2018-05-04 14:01:02 -07:00
|
|
|
json: false,
|
2019-03-08 09:39:47 -07:00
|
|
|
level,
|
2018-09-18 08:51:34 -07:00
|
|
|
maxFiles: 3, // Keep last 3 files
|
|
|
|
maxsize: 10000000, // 10MB
|
2018-08-16 09:56:33 -07:00
|
|
|
timestamp: () => new Date().toLocaleString(),
|
2018-05-04 14:01:02 -07:00
|
|
|
}),
|
2019-03-08 09:39:47 -07:00
|
|
|
new winston.transports.Console({
|
2018-04-18 09:25:40 -07:00
|
|
|
timestamp: () => new Date().toLocaleString(),
|
2019-03-08 09:39:47 -07:00
|
|
|
formatter: (options) =>
|
2019-03-09 13:36:07 -07:00
|
|
|
winston.config.colorize(options.level, ' zigbee2mqtt:' + options.level.toLowerCase()) + ' ' +
|
|
|
|
options.timestamp() + ' ' + (options.message ? options.message : '') +
|
2019-03-09 13:36:26 -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-16 11:04:29 -07:00
|
|
|
logger.info(`Logging to directory: '${directory}'`);
|
2018-08-12 11:42:06 -07:00
|
|
|
|
2018-08-16 11:04:29 -07:00
|
|
|
logger.transports.console.level = level;
|
2018-08-05 09:55:26 -07:00
|
|
|
|
2018-08-16 11:04:29 -07:00
|
|
|
// Cleanup any old log directory.
|
2018-09-18 08:51:34 -07:00
|
|
|
if (settings.get().advanced.log_directory.includes('%TIMESTAMP%')) {
|
|
|
|
const rootDirectory = path.join(directory, '..');
|
|
|
|
|
|
|
|
let directories = fs.readdirSync(rootDirectory).map((d) => {
|
|
|
|
d = path.join(rootDirectory, d);
|
|
|
|
return {path: d, birth: fs.statSync(d).birthtimeMs};
|
|
|
|
});
|
|
|
|
|
|
|
|
directories.sort((a, b) => b.birth - a.birth);
|
|
|
|
directories = directories.slice(10, directories.length);
|
|
|
|
directories.forEach((dir) => {
|
|
|
|
logger.debug(`Removing old log directory '${dir.path}'`);
|
|
|
|
rimraf.sync(dir.path);
|
|
|
|
});
|
|
|
|
}
|
2018-04-18 09:25:40 -07:00
|
|
|
|
|
|
|
module.exports = logger;
|