-
-
Save stefanwalther/2aeb7b28996ee3222a4820bef443379f to your computer and use it in GitHub Desktop.
A simple node module that makes console.log/error/warn/debug/time statements log through winston (simply include at the beginning of your app) ES5/ES6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as winston from 'winston'; | |
import * as path from 'path'; | |
let logger = new winston.Logger(); | |
export default { | |
middleware(req, res, next){ | |
console.log('verbose', req.method, req.url, res.statusCode); | |
next(); | |
} | |
}; | |
//Configure overrides | |
switch((process.env.NODE_ENV || '').toLowerCase()){ | |
case 'production': | |
logger.add(winston.transports.File, { | |
filename: path.resolve('application.log'), | |
// TODO: Set log folder | |
// FIXME: Create it before run or it won't log!! | |
dirname: path.resolve(__dirname, '../logs/'), | |
handleExceptions: true, | |
exitOnError: false, | |
level: 'warn' | |
}); | |
overrideConsole(); | |
break; | |
case 'test': | |
break; // Don't set up the logger overrides | |
default: | |
logger.add(winston.transports.Console, { | |
colorize: true, | |
timestamp: true, | |
level: 'silly' | |
}); | |
// TODO: Enable Growl support | |
// FIXME: npm i winston-growl --save | |
// var growl = require('winston-growl'); | |
// logger.add(growl, {level: 'info'}); | |
overrideConsole(); | |
break; | |
} | |
/** | |
* Override the built-in console methods with winston hooks | |
*/ | |
function overrideConsole(){ | |
/** | |
* Core logging method exposed to Winston. | |
* @param {String} [level='silly'] Level at which to log the message. | |
* @param {String} msg Message to log. | |
* @param {Object} [meta] Additional metadata to attach. | |
* @param {Function} callback Continuation to respond to when complete. | |
*/ | |
console.log = function(level, msg, meta, callback){ //eslint-disable-line no-unused-vars | |
let args = Array.from(arguments); | |
if(!(args[0] in logger.levels)){ | |
args.unshift('silly'); | |
} | |
logger.log(...args); | |
}; | |
console.info = function(){ | |
logger.info(...arguments); | |
}; | |
console.warn = function(){ | |
logger.warn(...arguments); | |
}; | |
console.error = function(){ | |
logger.error(...arguments); | |
}; | |
console.debug = function(){ | |
logger.debug(...arguments); | |
}; | |
console.time = function(){ | |
logger.profile(...arguments); | |
}; | |
console.timeEnd = function(){ | |
logger.profile(...arguments); | |
}; | |
} | |
// TESTS: | |
// console.time('LSP alive'); | |
// console.log('Lumpy Space Princess', {cantouchglob: false}); | |
// console.info({zombiesCount: Infinity}); | |
// console.warn('I am not getting eaten by zombies tonight!', {lumpOut: false}); | |
// console.log('verbose', 'My formula is going to cure the zombies because I made the prettiest formula', {cure: 'Yeah!'}); | |
// console.error('Oh My Glob! Look At Those %s Lips!', 'Luscious', {lips: 'luscious'}); | |
// console.timeEnd('LSP alive'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var path = require('path'); | |
var winston = require('winston'); | |
var logger = new winston.Logger(); | |
module.exports = { | |
middleware: function(req, res, next){ | |
console.log('verbose', req.method, req.url, res.statusCode); | |
next(); | |
} | |
}; | |
//Configure overrides | |
switch((process.env.NODE_ENV || '').toLowerCase()){ | |
case 'production': | |
logger.add(winston.transports.File, { | |
filename: path.resolve('application.log'), | |
// TODO: Set log folder | |
// FIXME: Create it before run or it won't log!! | |
dirname: path.resolve(__dirname, '../logs/'), | |
handleExceptions: true, | |
exitOnError: false, | |
level: 'warn' | |
}); | |
overrideConsole(); | |
break; | |
case 'test': | |
break; // Don't set up the logger overrides | |
default: | |
logger.add(winston.transports.Console, { | |
colorize: true, | |
timestamp: true, | |
level: 'silly' | |
}); | |
// TODO: Enable Growl support | |
// FIXME: npm i winston-growl --save | |
// var growl = require('winston-growl'); | |
// logger.add(growl, {level: 'info'}); | |
overrideConsole(); | |
break; | |
} | |
/** | |
* Override the built-in console methods with winston hooks | |
*/ | |
function overrideConsole(){ | |
/** | |
* Core logging method exposed to Winston. | |
* @param {String} [level='silly'] Level at which to log the message. | |
* @param {String} msg Message to log. | |
* @param {Object} [meta] Additional metadata to attach. | |
* @param {Function} callback Continuation to respond to when complete. | |
*/ | |
console.log = function(level, msg, meta, callback){ //eslint-disable-line no-unused-vars | |
var args = Array.prototype.slice.call(arguments); | |
if(!(args[0] in logger.levels)){ | |
args.unshift('silly'); | |
} | |
logger.log.apply(logger, args); | |
}; | |
console.info = function(){ | |
logger.info.apply(logger, arguments); | |
}; | |
console.warn = function(){ | |
logger.warn.apply(logger, arguments); | |
}; | |
console.error = function(){ | |
logger.error.apply(logger, arguments); | |
}; | |
console.debug = function(){ | |
logger.debug.apply(logger, arguments); | |
}; | |
console.time = function(){ | |
logger.profile.apply(logger, arguments); | |
}; | |
console.timeEnd = function(){ | |
logger.profile.apply(logger, arguments); | |
}; | |
} | |
// TESTS: | |
// console.time('LSP alive'); | |
// console.log('Lumpy Space Princess', {cantouchglob: false}); | |
// console.info({zombiesCount: Infinity}); | |
// console.warn('I am not getting eaten by zombies tonight!', {lumpOut: false}); | |
// console.log('verbose', 'My formula is going to cure the zombies because I made the prettiest formula', {cure: 'Yeah!'}); | |
// console.error('Oh My Glob! Look At Those %s Lips!', 'Luscious', {lips: 'luscious'}); | |
// console.timeEnd('LSP alive'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment