Newer
Older
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
Lloyd Hilaiel
committed
/*
* A very thin wrapper around winston for general server logging.
* Exports a winston Logger instance in exports.logger with several functions
* corresponding to different log levels. use it like this:
Lloyd Hilaiel
committed
*
Lloyd Hilaiel
committed
* const logger = require('../libs/logging.js').logger;
* logger.debug("you can probably ignore this. just for debugging.");
* logger.info("something happened, here's info about it!");
* logger.warn("this isn't good. it's not a fatal error, but needs attention");
* logger.error("this isn't good at all. I will probably crash soon.");
*/
const
winston = require("winston"),
configuration = require("./configuration"),
path = require('path'),
fs = require('fs');
// existsSync moved from path in 0.6.x to fs in 0.8.x
if (typeof fs.existsSync === 'function') {
var existsSync = fs.existsSync;
} else {
var existsSync = path.existsSync;
}
Lloyd Hilaiel
committed
// go through the configuration and determine log location
Lloyd Hilaiel
committed
var log_path = path.join(configuration.get('var_path'), 'log');
Lloyd Hilaiel
committed
// simple inline function for creation of dirs
function mkdir_p(p) {
if (!existsSync(p)) {
Lloyd Hilaiel
committed
mkdir_p(path.dirname(p));
fs.mkdirSync(p, "0755");
}
}
mkdir_p(log_path);
var filename = path.join(log_path, configuration.get('process_type') + ".log");
exports.logger = new (winston.Logger)({
transports: [new (winston.transports.File)({
timestamp: function () { return new Date().toISOString() },
Lloyd Hilaiel
committed
filename: filename,
Lloyd Hilaiel
committed
colorize: true,
Lloyd Hilaiel
committed
handleExceptions: true
Lloyd Hilaiel
committed
})]
});
Lloyd Hilaiel
committed
exports.enableConsoleLogging = function() {
Lloyd Hilaiel
committed
exports.logger.add(winston.transports.Console, {
colorize: true,
handleExceptions: true
});
};
if (process.env['LOG_TO_CONSOLE']) exports.enableConsoleLogging();
Lloyd Hilaiel
committed
exports.logger.exitOnError = false;