Skip to content

Instantly share code, notes, and snippets.

@bubuzzz
Created December 10, 2016 00:41
Show Gist options
  • Save bubuzzz/8a8b577ab09d6d4766a79cd4636b44e1 to your computer and use it in GitHub Desktop.
Save bubuzzz/8a8b577ab09d6d4766a79cd4636b44e1 to your computer and use it in GitHub Desktop.

Revisions

  1. bubuzzz created this gist Dec 10, 2016.
    51 changes: 51 additions & 0 deletions logging.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,51 @@
    'use strict';

    import util from 'util';
    import winston from 'winston';
    import path from 'path';
    import { Meteor } from 'meteor/meteor';

    winston.transports.DailyRotateFile = require('winston-daily-rotate-file');
    let production = (process.env.NODE_ENV || '').toLowerCase() === 'production';

    if (production) {
    let logDir = Meteor.settings['private']['logs'];

    let logOpts = {
    transports: [
    new(winston.transports.Console)(),
    new(winston.transports.DailyRotateFile)({
    name: 'info',
    datePattern: '.yyyy-MM-ddTHH',
    filename: path.join(logDir, "cba.log"),
    level: 'info',
    json : false,
    tailable : true
    }),
    new(winston.transports.DailyRotateFile)({
    name: 'error',
    datePattern: '.yyyy-MM-ddTHH',
    filename: path.join(logDir, "cba_error.log"),
    level: 'error',
    json : false,
    tailable : true
    })
    ]
    }

    let logger = new winston.Logger(logOpts);

    let log = console.log;
    console.log = function() {
    // a special case for Meteor, console log has to return during the LISTERNING log, otherwise, server cannot process request after
    // start up
    if (arguments.length === 1 && arguments[0] === 'LISTENING') return log.call(console, 'LISTENING');
    logger.info.apply(logger, arguments);
    };

    ['info', 'warn', 'error', 'debug'].forEach(function(method) {
    console[method] = function() {
    logger[method].apply(logger, arguments);
    }
    });
    }