lib/easylogger.js
function EasyLogger(configFile){

  // ___ Modules ___________________________________________________________

  var cm = require('jsonfile-config-manager')       // Config Manager object
  var contains = require("multiple-contains")       // Contains library
  var winston = require('winston')                  // Logger library

  // __ Properties _________________________________________________________

  var defaultFormatters = new Object()

  // Log formatter with timestamp, level and message
  defaultFormatters.fileFormatterWithTimestamp = function(args) {
    var date = new Date().toLocaleDateString().replace(/-/g, '/')
    var time = new Date().toLocaleTimeString()
    var offset = (args.level == 'error' || args.level == 'debug') ? '] -  ' : ']  - '
    var logMessage = '[' + date + ' ' + time + '] [' + args.level + offset
    var blank = ''
    for ( var i = 0; i < logMessage.length; i++ )  { blank += ' '}
    args.message = args.message.replace(/\n/g, '\n' + blank);
    logMessage += args.message
    return logMessage;
  }

  // Simple log formatter with level and message
  defaultFormatters.consoleFormatter = function(args) {
    var offset = (args.level == 'error' || args.level == 'debug') ? '] -  ' : '] - '
    var logMessage = '[' + args.level + offset
    var blank = ''
    for ( var i = 0; i < logMessage.length; i++ )  { blank += ' '}
    args.message = args.message.replace(/\n/g, '\n' + blank);
    logMessage += args.message
    return logMessage;
  }

  // __ Private Methods ____________________________________________________

  // Logger builder
  var buildLogger = function() {

    // If logger is disabled, dont build anything
    if (!cm.config.easylogger.enabled) return

    // Building logger
    logger = new winston.Logger({})

    // Adding transports
    for (var i = 0; i < cm.config.easylogger.transports.length; i++) {
      var transport = cm.config.easylogger.transports[i]
      if (transport.enabled){
        logger.add(winston.transports[transport.type], transport)
        setCustomFormatter(transport.type, defaultFormatters[transport.formatter])
        print('info', transport.type + ' logger ... OK')
      }

      if( i == cm.config.easylogger.transports.length)
        console.dir(logger.transports)
    }
  }

  // __ Public Methods _____________________________________________________

  // Print a message
  var print = function(level, text) {

    // If 'text' is object, convert to string
    if (typeof(text) === 'object') text = JSON.stringify(text)

    // If no logger, console output
    if (!logger) {
      switch(level.toLowerCase()) {
        case 'error':
            console.error(text)
            break
        case 'warn':
            console.warn(text)
            break
        case 'info':
            console.info(text)
            break
        case 'debug':
            break
        default:
            if (!text) text = level
            console.log(text)
      }
    } else {

      if (!text) {
        text = level
        level = 'info'
      }
      logger.log(level, text)
      return
    }
  }

  // Print Error/response message
  var printError = function(err, data) {
    if (err)  print('error', err)
    if (data) print('info', data)
  }

  // Set custom formatter to trasnsports
  //   transportId : transport type where apply formatter
  //   formatter : function to format messages
  var setCustomFormatter = function(transportType, formatter) {

    var exists = contains( cm.config.easylogger.transports
                         , { "key": "type", "value": transportType }
                         , "exists"
                         )
    // It is needed because transport type begins whit upper case if you add a new instance
    // but, in object, its property is saved beggining with lower chase
    var firstChar = transportType.charAt(0)
    transportType = transportType.replace(firstChar, firstChar.toLowerCase())

    if (exists) logger.transports[transportType].formatter = formatter
  }

  // __ Main _______________________________________________________________

  var logger = null // Easy Winston Logger with multiple transporters
  if (!configFile) configFile = "../config/default.logger.json"
  cm.addFile(configFile, 'easylogger', true, buildLogger)

  // __ Return _____________________________________________________________

  return { "print": print
         , "printError": printError
         , "setCustomFormatter": setCustomFormatter
         }
}
function EasyLogger(configFile){

  // ___ Modules ___________________________________________________________

  var cm = require('jsonfile-config-manager')       // Config Manager object
  var contains = require("multiple-contains")       // Contains library
  var winston = require('winston')                  // Logger library

  // __ Properties _________________________________________________________

  var defaultFormatters = new Object()

  // Log formatter with timestamp, level and message
  defaultFormatters.fileFormatterWithTimestamp = function(args) {
    var date = new Date().toLocaleDateString().replace(/-/g, '/')
    var time = new Date().toLocaleTimeString()
    var offset = (args.level == 'error' || args.level == 'debug') ? '] -  ' : ']  - '
    var logMessage = '[' + date + ' ' + time + '] [' + args.level + offset
    var blank = ''
    for ( var i = 0; i < logMessage.length; i++ )  { blank += ' '}
    args.message = args.message.replace(/\n/g, '\n' + blank);
    logMessage += args.message
    return logMessage;
  }

  // Simple log formatter with level and message
  defaultFormatters.consoleFormatter = function(args) {
    var offset = (args.level == 'error' || args.level == 'debug') ? '] -  ' : '] - '
    var logMessage = '[' + args.level + offset
    var blank = ''
    for ( var i = 0; i < logMessage.length; i++ )  { blank += ' '}
    args.message = args.message.replace(/\n/g, '\n' + blank);
    logMessage += args.message
    return logMessage;
  }

  // __ Private Methods ____________________________________________________

  // Logger builder
  var buildLogger = function() {

    // If logger is disabled, dont build anything
    if (!cm.config.easylogger.enabled) return

    // Building logger
    logger = new winston.Logger({})

    // Adding transports
    for (var i = 0; i < cm.config.easylogger.transports.length; i++) {
      var transport = cm.config.easylogger.transports[i]
      if (transport.enabled){
        logger.add(winston.transports[transport.type], transport)
        setCustomFormatter(transport.type, defaultFormatters[transport.formatter])
        print('info', transport.type + ' logger ... OK')
      }

      if( i == cm.config.easylogger.transports.length)
        console.dir(logger.transports)
    }
  }

  // __ Public Methods _____________________________________________________

  // Print a message
  var print = function(level, text) {

    // If 'text' is object, convert to string
    if (typeof(text) === 'object') text = JSON.stringify(text)

    // If no logger, console output
    if (!logger) {
      switch(level.toLowerCase()) {
        case 'error':
            console.error(text)
            break
        case 'warn':
            console.warn(text)
            break
        case 'info':
            console.info(text)
            break
        case 'debug':
            break
        default:
            if (!text) text = level
            console.log(text)
      }
    } else {

      if (!text) {
        text = level
        level = 'info'
      }
      logger.log(level, text)
      return
    }
  }

  // Print Error/response message
  var printError = function(err, data) {
    if (err)  print('error', err)
    if (data) print('info', data)
  }

  // Set custom formatter to trasnsports
  //   transportId : transport type where apply formatter
  //   formatter : function to format messages
  var setCustomFormatter = function(transportType, formatter) {

    var exists = contains( cm.config.easylogger.transports
                         , { "key": "type", "value": transportType }
                         , "exists"
                         )
    // It is needed because transport type begins whit upper case if you add a new instance
    // but, in object, its property is saved beggining with lower chase
    var firstChar = transportType.charAt(0)
    transportType = transportType.replace(firstChar, firstChar.toLowerCase())

    if (exists) logger.transports[transportType].formatter = formatter
  }

  // __ Main _______________________________________________________________

  var logger = null // Easy Winston Logger with multiple transporters
  if (!configFile) configFile = "../config/default.logger.json"
  cm.addFile(configFile, 'easylogger', true, buildLogger)

  // __ Return _____________________________________________________________

  return { "print": print
         , "printError": printError
         , "setCustomFormatter": setCustomFormatter
         }
}
/* ___________________________________________________________________________

Module: easylogger.js   Version: v0.0.4
Repository: http://github.com/GuillermoPena/private-telegram-bot
Author: Guillermo Peña (guillermo.pena.cardano@gmail.com)
Last update: 14/07/2015

Easy winston logger builder by json files

____________________________________________________________________________