From 2188b5a6c0ded29cf3d0a349bddbc56c5d5ad4c2 Mon Sep 17 00:00:00 2001 From: Cole Gleason Date: Wed, 15 Jan 2014 23:26:18 -0600 Subject: [PATCH] chore(logging): add logging via Winston and wrapped it with a custom logging module --- .gitignore | 2 ++ package.json | 3 ++- src/logging.js | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 src/logging.js diff --git a/.gitignore b/.gitignore index bb5ec66ebf..4a6eb3925d 100644 --- a/.gitignore +++ b/.gitignore @@ -12,6 +12,8 @@ newrelic_agent.log .bower-tmp .bower-registry .bower-cache + +*.log src/*/*.map src/*/*/*.map test/*.js diff --git a/package.json b/package.json index 338485de34..ac9ffa0831 100644 --- a/package.json +++ b/package.json @@ -45,7 +45,8 @@ "passport": "~0.1.18", "passport-facebook": "~1.0.2", "newrelic": "~1.3.0", - "connect-ratelimit": "0.0.6" + "connect-ratelimit": "0.0.6", + "winston": "~0.7.2" }, "private": true, "subdomain": "habitrpg", diff --git a/src/logging.js b/src/logging.js new file mode 100644 index 0000000000..32ff5ef883 --- /dev/null +++ b/src/logging.js @@ -0,0 +1,34 @@ +var nconf = require('nconf'); +var winston = require('winston'); + +var logger; +if (logger == null) { + // We currently don't support logging on Heroku + if (nconf.get('NODE_ENV') != 'production') { + logger = new (winston.Logger)({ + transports: [ + new (winston.transports.Console)({colorize: true}), + new (winston.transports.File)({ filename: 'habitrpg.log' }) + // TODO: Add email, loggly, or mongodb transports + ] + }); + } +} + +// A custom log function that wraps Winston. Makes it easy to instrument code +// and still possible to replace Winston in the future. +module.exports.log = function(/* variable args */) { + logger.log.apply(logger, arguments); +}; + +module.exports.info = function(/* variable args */) { + logger.info.apply(logger, arguments); +}; + +module.exports.warn = function(/* variable args */) { + logger.warn.apply(logger, arguments); +}; + +module.exports.error = function(/* variable args */) { + winston.error(arguments); +};