/**
 * XadillaX created at 2016-04-19 18:43:02 With ♥
 *
 * Copyright (c) 2016 Souche.com, all rights
 * reserved.
 */
"use strict";

var path = require("path");

var winston = require("winston");
require("sugar");

var formatters = require("./formatters");

var defaultLogger = {
    type: "console",
    colorize: true,
    level: "silly",
    stderrLevels: ["error", "warn", "info", "verbose", "debug", "silly"],
    formatter: "default"
};

var Logger = function(akyuu) {
    this.akyuu = akyuu;
    this.config = akyuu.config.logger || [];    // transport option array

    this._checkConsoleTransport();

    this.loggerContainer = new winston.Container();
    this.loggers = this.loggerContainer.loggers;
};

/**
 * promise only one console transport; if set multi console transport, only
 * the first one effect
 * @private
 */
Logger.prototype._checkConsoleTransport = function() {
    var consoleTypeFirstIndex = this.config.findIndex(function(n) {
        return n.type && n.type.toLocaleLowerCase() === "console";
    });

    if (consoleTypeFirstIndex === -1) {
        this.config.push(defaultLogger);
    } else {
        this.config.remove(function(n, index) {
            return n.type && n.type === "console" && index !== consoleTypeIndex;
        });
    }
}

Logger.prototype.getTransports = function (label) {
    var result = [];

    for (var i = 0; i < this.config.length; i++) {
        var type = this.config[i].type.capitalize();
        var transportOption = Object.assign({}, this.config[i], {
            timestamp: function () {
                return Date.now();
            },
            id: label,
            label: label
        });

        if (type === "File") {
            transportOption.filename = path.join(this.akyuu.projectRoot, transportOption.filename);
        }

        if (typeof transportOption.formatter !== "function") {
            transportOption.formatter = formatters[transportOption.formatter] || formatters["default"];
        }

        if (!winston.transports[type]) {
            throw new Error("Cannot add unknown transport: " + type);
        }

        result.push(new (winston.transports[type])(transportOption));
    }

    return result;
}

Logger.prototype.get = function(name) {
    if (this.loggers[name]) {
        return this.loggers[name];
    }

    // create a logger
    var logger = this.loggers[name] = new winston.Logger({
        transports: this.getTransports(name)
    });

    this.loggers[name].on("close", function () {
        self._delete(name);
    });

    // hack logger
    logger.$error = logger.error;
    logger.error = function() {
        if(arguments.length) {
            if(arguments[0] && arguments[0].stack) {
                arguments[0] = arguments[0].stack;
            }
        }

        this.$error.apply(this, arguments);
    };

    if(process.env.NODE_ENV === "test") {
        logger = {
            error: function() {},
            warn: function() {},
            info: function() {},
            verbose: function() {},
            debug: function() {},
            silly: function() {}
        };
    }

    return logger;
};

Logger.prototype.load = function() {
    /** { error: 0, warn: 1, info: 2, verbose: 3, debug: 4, silly: 5 }
     *
     * 1. if a transport config has `level` prop,
     *    log shown when  logger.level <= transport.level
     *    [src:]  transport.level && self.levels[transport.level] >= self.levels[level]
     *
     * 2. if a transport do not have `level` prop,
     *    log shown whenlogger.level <= logger.options.level || 'info'
     *    [src:]  !transport.level && self.levels[self.level] >= self.levels[level]
     */
    winston.addColors({
        error: "red",
        warn: "magenta",
        info: "green",
        verbose: "gray",
        debug: "blue",
        silly: "grey"
    });
};

module.exports = Logger;

@@ -193,26 +193,8 @@
 n%22);
-%0Arequire(%22sugar%22);
 %0A%0Ava
@@ -336,16 +336,17 @@
 evels: %5B
+ 
 %22error%22,
@@ -389,16 +389,17 @@
  %22silly%22
+ 
 %5D,%0A    f
@@ -422,16 +422,98 @@
 lt%22%0A%7D;%0A%0A
+/**%0A * Logger%0A * @param %7BAkyuu%7D akyuu the akyuu parent object%0A * @constructor%0A */%0A
 var Logg
@@ -779,16 +779,42 @@
 %7D;%0A%0A/**%0A
+ * _checkConsoleTransport%0A
  * promi
@@ -1336,16 +1336,21 @@
 soleType
+First
 Index;%0A 
@@ -1367,18 +1367,126 @@
 %0A    %7D%0A%7D
+;
 %0A%0A
+/**%0A * getTransports%0A * @param %7BString%7D label the label string%0A * @return %7BArray%7D the transports array%0A */%0A
 Logger.p
@@ -1559,12 +1559,80 @@
     
+var _timestamp = function() %7B%0A        return Date.now();%0A    %7D;%0A%0A    
 for
- 
 (var
@@ -1816,70 +1816,18 @@
 mp: 
-function () %7B%0A                return Date.now();%0A            %7D
+_timestamp
 ,%0A  
@@ -2424,17 +2424,112 @@
 esult;%0A%7D
-%0A
+;%0A%0A/**%0A * get%0A * @param %7BString%7D name the logger name%0A * @return %7BLogger%7D the winston logger%0A */
 %0ALogger.
@@ -2765,24 +2765,45 @@
 e)%0A    %7D);%0A%0A
+    var self = this;%0A
     this.log
@@ -3478,16 +3478,32 @@
 er;%0A%7D;%0A%0A
+/**%0A * load%0A */%0A
 Logger.p

add comment