# simple-logger

window.simpleLogger = {

    loggers: [],

    load: function (str) {
        console.warn("simpleLogger fallback logger to console");
    },
    getLogger: function (loggerName) {
        var logger = this.loggers[loggerName];
        if (!logger) {
            logger = new SimpleLogger(loggerName, console);
            this.loggers[loggerName] = logger;
        }
        return logger;
    }
};

SimpleLogger._LVL_ERROR = 1;
SimpleLogger._LVL_WARN = 2;
SimpleLogger._LVL_INFO = 3;
SimpleLogger._LVL_DEBUG = 4;

function SimpleLogger(name, console) {
    this.name = name;
    this.console = console;
    this.level = SimpleLogger._LVL_ERROR;
}

SimpleLogger.prototype.setLevel = function (level) {
    this.level = level;

    var noop = function () {
    };

    this.error = noop;
    this.warn = noop;
    this.info = noop;
    this.debug = noop;

    if (level === SimpleLogger._LVL_ERROR) {
        this.error = window.console.error.bind(window.console, '[ERROR] [' + this.name + '] - %s');
    }
    if (level === SimpleLogger._LVL_WARN) {
        this.warn = window.console.warn.bind(window.console, '[WARN] [' + this.name + '] - %s');
    }
    if (level === SimpleLogger._LVL_INFO) {
        this.info = window.console.info.bind(window.console, '[INFO] [' + this.name + '] - %s');
    }
    if (level === SimpleLogger._LVL_DEBUG) {
        this.debug = window.console.debug.bind(window.console, '[DEBUG] [' + this.name + '] - %s');
        this.log = window.console.log.bind(window.console, '[DEBUG] [' + this.name + '] - %s');
    }
};

SimpleLogger.prototype.log = function (message) {
    if (this.level >= SimpleLogger._LVL_DEBUG) {
        this.log = window.console.log.bind(window.console, '[DEBUG] [' + this.name + '] - %s');
    }
};

SimpleLogger.prototype.debug = function (message) {
    if (this.level >= SimpleLogger._LVL_DEBUG) {
        this.debug = window.console.debug.bind(window.console, '[DEBUG] [' + this.name + '] - %s');
    }
};

SimpleLogger.prototype.info = function (message) {
    if (this.level >= SimpleLogger._LVL_INFO) {
        this.info = window.console.info.bind(window.console, '[INFO] [' + this.name + '] - %s');
    }
};

SimpleLogger.prototype.warn = function (message) {
    if (this.level >= SimpleLogger._LVL_WARN) {
        this.warn = window.console.warn.bind(window.console, '[WARN] [' + this.name + '] - %s');
    }
};

SimpleLogger.prototype.error = function (message) {
    if (this.level >= SimpleLogger._LVL_ERROR) {
        this.error = window.console.error.bind(window.console, '[ERROR] [' + this.name + '] - %s');
    }
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83