Skip to content

Instantly share code, notes, and snippets.

@kirikintha
Last active August 29, 2015 14:08
Show Gist options
  • Save kirikintha/ab58f36558ee955c4678 to your computer and use it in GitHub Desktop.
Save kirikintha/ab58f36558ee955c4678 to your computer and use it in GitHub Desktop.
Sample Winston transport examples, using meanjs, express and a config file (not supplied)
'use strict';
/**
* Module Dependencies
* @type type
*/
var winston = require('winston');
var mail = require('winston-mail').Mail;
var sns = require('winston-sns').SNS;
var mongo = require('winston-mongodb').MongoDB;
var paperTrail = require('winston-papertrail').Papertrail;
var config = require('./config');
var _ = require('lodash');
/**
* Log the output, using Winston's loggers.
* This will look for the appropriate category, and set the log there.
* https://github.com/flatiron/winston#logging-with-metadata
* @param {type} type
* @param {type} string
* @param {type} meta
* @param {type} category
* @returns {unresolved}
*/
var __log = function (type, string, meta, category) {
meta = meta || {};
category = category || 'defaultLog';
var logger = winston.loggers.get(category);
return logger.log(type, string, meta);
};
/**
* This will switch between transport methods on startup and send via .trans()
* which is the alias for setting transportation methods.
* @param {type} type
* @param {type} string
* @param {type} meta
* @returns {unresolved}
*/
var __transport = function (type, string, meta) {
meta = meta || {};
return winston.log(type, string, meta);
};
/**
* Profiler, this can be a little buggy when you use a transport option, so be
* careful about sending out logs when profiling. Mail transport seems to be buggy.
* @returns {__profile}
*/
var __profile = function() {};
/**
* Start a profiling timer.
* @returns {undefined}
*/
__profile.prototype.start = function () {
this.timer = winston.startTimer();
};
/**
* Stop a profiling timer, and log the message.
* You can supply your own meta data and also a callback function.
* @param {type} msg
* @param {type} meta
* @param {type} callback
* @returns {undefined}
*/
__profile.prototype.stop = function (msg, meta, callback) {
msg = msg || 'Winston Profile';
this.timer.done(msg, meta, callback);
};
/**
* Add Categories, based on logging levels.
* Winston Levels:
* debug, verbose, info, warn, error.
* @returns {undefined}
*/
var __configureCategories = function () {
//Look for logging levels.
var dir = './app/logs/',
extension = '.log',
//Log Defaults.
defaults = {
//Defaults, allow you to log any level.
console: {
timestamp: false,
colorize: true,
label: '',
exitOnError: true
},
file: {
filename: './app/logs/default.log',
silent: false,
exitOnError: false,
handleExceptions: true,
//Default to 1 MB for logs.
maxsize: 1000000,
//Default to 10 files.
maxFiles: 10,
json: true,
logstash: false
}
};
//You will *always* have a default logger.
winston.loggers.add('defaultLog', defaults);
//Loop over each category, add configuration options. Automatically adds console
// variables.
if (!_.isUndefined(config.winston) && !_.isUndefined(config.winston.categories)) {
_.forEach(config.winston.categories, function (category) {
if (!_.isUndefined(config.winston[category])) {
var settings = config.winston[category],
name = category + 'Log',
merged;
//If we do not have a filename, then use the category as the file name.
if (!_.isUndefined(settings.file)) {
settings.file.id = name;
if (_.isUndefined(settings.file.filename)) {
settings.file.filename = dir + category + extension;
}
}
merged = _.merge(defaults, settings);
winston.loggers.add(name, merged);
}
});
}
};
/**
* Configure Transports, based on your configuration option. You can only have
* one additional transport option available, if it is not in a category.
* @returns {undefined}
*/
var __configureTransport = function () {
//Look for configuration transport.
if (!_.isUndefined(config.winston) && !_.isUndefined(config.winston.transport)) {
switch (config.winston.transport) {
case 'smtp':
//https://github.com/flatiron/winston#mail-transport
if (config.winston.mail) {
winston.add(mail, config.winston.smtp);
}
break;
case 'sns':
//https://github.com/flatiron/winston#amazon-sns-simple-notification-system-transport
if (config.winston.sns) {
winston.add(sns, config.winston.sns);
}
break;
case 'mongo':
//https://github.com/flatiron/winston#mongodb-transport
if (config.winston.mongo) {
winston.add(mongo, config.winston.mongo);
}
break;
case 'paperTrail':
//https://github.com/flatiron/winston#papertrail-transport
if (config.winston.paperTrail) {
winston.add(paperTrail, config.winston.paperTrail);
}
break;
}
}
};
/**
* Based on https://github.com/flatiron/winston - Winston Logger Docs.
* Set logger configuration, based on your ./config/env/ configiguration file.
* @author - [email protected], based on Don's work.
* File Defaults:
* Max Size, 1MB maximum of 10 files (10MB Total)
* @returns {undefined}
*/
var __init = function () {
//Always enable the .cli() functions.
winston.cli();
//Configure Mail Transport.
__configureTransport();
//Configure Categories.
__configureCategories();
};
module.exports.init = __init;
module.exports.log = __log;
module.exports.trans = __transport;
module.exports.profile = new __profile();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment