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');
|
2019-09-25 01:55:50 -07:00
|
|
|
const colorizer = winston.format.colorize();
|
2020-01-16 15:33:17 -07:00
|
|
|
const assert = require('assert');
|
2018-06-14 11:27:43 -07:00
|
|
|
|
2019-11-29 15:36:57 -07:00
|
|
|
// What transports to enable
|
|
|
|
const output = settings.get().advanced.log_output;
|
|
|
|
|
|
|
|
// Directory 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);
|
2020-02-05 11:37:13 -07:00
|
|
|
const logFilename = settings.get().advanced.log_file.replace('%TIMESTAMP%', timestamp);
|
2018-08-16 11:04:29 -07:00
|
|
|
|
2019-11-29 15:36:57 -07:00
|
|
|
// Make sure that log directoy exsists when not logging to stdout only
|
|
|
|
if (output.includes('file')) {
|
|
|
|
fx.mkdirSync(directory);
|
|
|
|
}
|
2018-08-16 11:04:29 -07:00
|
|
|
|
2019-09-25 01:55:50 -07:00
|
|
|
// Determine the log level.
|
|
|
|
const level = settings.get().advanced.log_level;
|
2020-01-16 15:33:17 -07:00
|
|
|
const validLevels = ['info', 'error', 'warn', 'debug'];
|
|
|
|
assert(validLevels.includes(level), `'${level}' is not a valid log_level, use one of '${validLevels.join(', ')}'`);
|
2019-09-09 10:48:09 -07:00
|
|
|
|
2019-09-25 01:55:50 -07:00
|
|
|
const levelWithCompensatedLength = {
|
|
|
|
'info': 'info ',
|
|
|
|
'error': 'error',
|
|
|
|
'warn': 'warn ',
|
|
|
|
'debug': 'debug',
|
|
|
|
};
|
2019-09-09 10:48:09 -07:00
|
|
|
|
2019-11-17 13:22:24 -07:00
|
|
|
/* istanbul ignore next */
|
2019-11-17 13:29:53 -07:00
|
|
|
const timestampFormat = () => moment().format(settings.get().advanced.timestamp_format);
|
2019-11-15 15:04:23 -07:00
|
|
|
|
2019-12-13 11:09:18 -07:00
|
|
|
// Setup default console logger
|
|
|
|
const transportsToUse = [
|
|
|
|
new winston.transports.Console({
|
2019-09-25 01:55:50 -07:00
|
|
|
level,
|
2019-12-13 11:09:18 -07:00
|
|
|
silent: !output.includes('console'),
|
2019-09-25 01:55:50 -07:00
|
|
|
format: winston.format.combine(
|
2019-11-15 15:04:23 -07:00
|
|
|
winston.format.timestamp({format: timestampFormat}),
|
2019-09-25 01:55:50 -07:00
|
|
|
winston.format.printf(/* istanbul ignore next */(info) => {
|
2020-06-03 11:44:11 -07:00
|
|
|
let {timestamp, level, message} = info;
|
|
|
|
level = level === 'warning' ? 'warn' : level;
|
2019-12-13 11:09:18 -07:00
|
|
|
const prefix = colorizer.colorize(level, `zigbee2mqtt:${levelWithCompensatedLength[level]}`);
|
|
|
|
return `${prefix} ${timestamp.split('.')[0]}: ${message}`;
|
2019-09-25 01:55:50 -07:00
|
|
|
}),
|
|
|
|
),
|
|
|
|
}),
|
2019-12-13 11:09:18 -07:00
|
|
|
];
|
|
|
|
|
|
|
|
// Add file logger when enabled
|
|
|
|
// NOTE: the initiation of the logger, even when not added as transport tries to create the logging directory
|
2020-04-04 10:46:43 -07:00
|
|
|
const transportFileOptions = {
|
|
|
|
filename: path.join(directory, logFilename),
|
|
|
|
json: false,
|
|
|
|
level,
|
|
|
|
format: winston.format.combine(
|
|
|
|
winston.format.timestamp({format: timestampFormat}),
|
|
|
|
winston.format.printf(/* istanbul ignore next */(info) => {
|
2020-06-03 11:44:11 -07:00
|
|
|
let {timestamp, level, message} = info;
|
|
|
|
level = level === 'warning' ? 'warn' : level;
|
2020-04-04 10:46:43 -07:00
|
|
|
return `${levelWithCompensatedLength[level]} ${timestamp.split('.')[0]}: ${message}`;
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
};
|
|
|
|
|
|
|
|
if (settings.get().advanced.log_rotation) {
|
|
|
|
transportFileOptions.tailable = true;
|
|
|
|
transportFileOptions.maxFiles = 3; // Keep last 3 files
|
|
|
|
transportFileOptions.maxsize = 10000000; // 10MB
|
|
|
|
}
|
|
|
|
|
2019-12-13 11:09:18 -07:00
|
|
|
if (output.includes('file')) {
|
2020-04-04 10:46:43 -07:00
|
|
|
transportsToUse.push(new winston.transports.File(transportFileOptions));
|
2019-11-29 15:36:57 -07:00
|
|
|
}
|
|
|
|
|
2020-06-03 11:44:11 -07:00
|
|
|
/* istanbul ignore next */
|
|
|
|
if (output.includes('syslog')) {
|
|
|
|
require('winston-syslog').Syslog;
|
|
|
|
const options = {app_name: 'zigbee2mqtt', ...settings.get().advanced.log_syslog};
|
2020-06-12 07:51:49 -07:00
|
|
|
if (options.hasOwnProperty('type')) options.type = options.type.toString();
|
2020-06-03 11:44:11 -07:00
|
|
|
transportsToUse.push(new winston.transports.Syslog(options));
|
|
|
|
}
|
|
|
|
|
2019-12-13 11:09:18 -07:00
|
|
|
// Create logger
|
2020-06-03 11:44:11 -07:00
|
|
|
const logger = winston.createLogger({transports: transportsToUse, levels: winston.config.syslog.levels});
|
2018-06-14 11:27:43 -07:00
|
|
|
|
2018-08-16 11:04:29 -07:00
|
|
|
// Cleanup any old log directory.
|
2019-06-09 15:01:48 -07:00
|
|
|
function cleanup() {
|
|
|
|
if (settings.get().advanced.log_directory.includes('%TIMESTAMP%')) {
|
|
|
|
const rootDirectory = path.join(directory, '..');
|
2018-09-18 08:51:34 -07:00
|
|
|
|
2019-06-09 15:01:48 -07:00
|
|
|
let directories = fs.readdirSync(rootDirectory).map((d) => {
|
|
|
|
d = path.join(rootDirectory, d);
|
2019-10-21 09:11:16 -07:00
|
|
|
return {path: d, birth: fs.statSync(d).mtime};
|
2019-06-09 15:01:48 -07:00
|
|
|
});
|
2018-09-18 08:51:34 -07:00
|
|
|
|
2019-06-09 15:01:48 -07:00
|
|
|
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-09-18 08:51:34 -07:00
|
|
|
}
|
2018-04-18 09:25:40 -07:00
|
|
|
|
2019-06-09 15:01:48 -07:00
|
|
|
logger.cleanup = cleanup;
|
2019-12-13 11:09:18 -07:00
|
|
|
logger.getLevel = () => transportsToUse[0].level;
|
2019-09-25 01:55:50 -07:00
|
|
|
logger.setLevel = (level) => {
|
2019-12-13 11:09:18 -07:00
|
|
|
transportsToUse.forEach((transport) => transport.level = level);
|
2019-09-25 01:55:50 -07:00
|
|
|
};
|
2019-06-09 15:01:48 -07:00
|
|
|
|
2019-11-29 15:36:57 -07:00
|
|
|
// Print to user what logging is enabled
|
|
|
|
if (output.includes('file')) {
|
|
|
|
if (output.includes('console')) {
|
2020-02-05 11:37:13 -07:00
|
|
|
logger.info(`Logging to console and directory: '${directory}' filename: ${logFilename}`);
|
2019-11-29 15:36:57 -07:00
|
|
|
} else {
|
2020-02-05 11:37:13 -07:00
|
|
|
logger.info(`Logging to directory: '${directory}' filename: ${logFilename}`);
|
2019-11-29 15:36:57 -07:00
|
|
|
}
|
|
|
|
logger.cleanup();
|
|
|
|
} else if (output.includes('console')) {
|
|
|
|
logger.info(`Logging to console only'`);
|
|
|
|
}
|
|
|
|
|
2020-06-03 11:44:11 -07:00
|
|
|
// winston.config.syslog.levels doesnt have warn, but is required for syslog.
|
|
|
|
/* istanbul ignore next */
|
|
|
|
logger.warn = (message) => logger.warning(message);
|
|
|
|
|
2018-04-18 09:25:40 -07:00
|
|
|
module.exports = logger;
|