@bbc/spartacus
Version:
65 lines (52 loc) • 1.67 kB
JavaScript
'use strict';
// Node.js logger utilities using winston
var fs = require('fs');
var path = require('path');
var _require = require('winston'),
createLogger = _require.createLogger,
format = _require.format,
transports = _require.transports;
var combine = format.combine,
label = format.label,
printf = format.printf,
simple = format.simple,
timestamp = format.timestamp;
var LOG_LEVEL = process.env.LOG_LEVEL || 'info';
var LOG_FILE = 'app.log';
var LOG_DIR = process.env.LOG_DIR || 'log';
if (!fs.existsSync(LOG_DIR)) {
fs.mkdirSync(LOG_DIR);
}
var logLocation = path.join(LOG_DIR, LOG_FILE);
// prettier-ignore
var fileTransport = new transports.File({
filename: logLocation,
handleExceptions: true,
humanReadableUnhandledException: true,
json: true,
level: LOG_LEVEL,
maxFiles: 1,
maxsize: 104857600 // 100MB
});
// prettier-ignore
var consoleTransport = new transports.Console({
handleExceptions: true,
humanReadableUnhandledException: true,
level: LOG_LEVEL,
timestamp: true
});
var customFormatting = printf(function (data) {
return data.timestamp + ' ' + data.level + ' [' + data.label + '] ' + data.message;
});
// e.g. outputs 'Article/index.jsx'
var folderAndFilename = function folderAndFilename(name) {
var fileparts = name.split(path.sep);
return fileparts.splice(-2).join(path.sep);
};
var logger = function logger(callingFile) {
return createLogger({
format: combine(label({ label: folderAndFilename(callingFile) }), simple(), timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }), customFormatting),
transports: [fileTransport, consoleTransport]
});
};
module.exports = logger;