Blame view

services/Logger.service.js 4.56 KB
6e6aa9b0   Tarpit Grover   Basic Setup
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
(function (window, angular) {
    var app = angular.module("framework.services.logging", []);

    app.provider('Logger', function () {
        var providers = [];
        var hiddenCatagorys = [];
        var loggingLevel = 1;

        this.DEBUG = 1;
        this.INFO = 2;
        this.WARNING = 3;
        this.ERROR = 4;

        this.setGlobalLoggingLevel = setGlobalLoggingLevel;
        this.addProvider = addProvider;
        this.hideCatagory = hideCatagory;

        function setGlobalLoggingLevel(level) {
            loggingLevel = level;
        }

        function addProvider(provider) {
            providers.push(provider);
        }

        function hideCatagory(catagory) {
            hiddenCatagorys.push(catagory);
        }

        this.$get = buildLoggerService;

        buildLoggerService.$inject = ['$injector'];
        function buildLoggerService($injector) {
            var instance = createLogger('Default');
            this.createLogger = createLogger;
            this.log = instance.log;
            this.debug = instance.debug;
            this.info = instance.info;
            this.warning = instance.warning;
            this.error = instance.error;
            this.DEBUG = 1;
            this.INFO = 2;
            this.WARNING = 3;
            this.ERROR = 4;
            var concreteProviders = [];
            angular.forEach(providers, function (value) {
                if (angular.isObject(value)) {
                    concreteProviders.push(value);
                } else if (angular.isFunction(value)) {
                    var pro = {
                        log: value,
                        debug: log.bind(null, 1),
                        info: log.bind(null, 2),
                        warning: log.bind(null, 3),
                        error: log.bind(null, 4)
                    };
                    concreteProviders.push(pro);
                } else if (angular.isArray(value)) {
                    var pro = $injector.invoke(value, null, {
                        Levels: {
                            DEBUG: 1,
                            INFO: 2,
                            WARNING: 3,
                            ERROR: 4
                        }
                    });
                    concreteProviders.push(pro);
                } else if (typeof value === 'string') {
                    var pro = $injector.get(value);
                    concreteProviders.push(pro);
                }
            });

            function createLogger(catagory) {
                return new Logger(catagory, concreteProviders, loggingLevel, hiddenCatagorys);
            }

            return this;
        }
    });

    function Logger(catagory, providers, loggingLevel, hiddenCatagorys) {
        this.log = log;
        this.debug = debug;
        this.info = info;
        this.warning = warning;
        this.error = error;

        function log(level, msg) {
            if (level < loggingLevel || hiddenCatagorys.indexOf(catagory) != -1)
                return;
            
            var args = Array.prototype.slice.call(arguments, 2);
            angular.forEach(providers, function (provider) {
                provider.log(level, catagory, msg, args);
            });
        }

        function debug(msg) {
            if (1 < loggingLevel || hiddenCatagorys.indexOf(catagory) != -1)
                return;

            var args = Array.prototype.slice.call(arguments, 1);
            angular.forEach(providers, function (provider) {
                provider.debug(catagory, msg, args);
            });
        }

        function info(msg) {
            if (2 < loggingLevel || hiddenCatagorys.indexOf(catagory) != -1)
                return;

            var args = Array.prototype.slice.call(arguments, 1);
            angular.forEach(providers, function (provider) {
                provider.info(catagory, msg, args);
            });
        }

        function warning(msg) {
            if (3 < loggingLevel || hiddenCatagorys.indexOf(catagory) != -1)
                return;

            var args = Array.prototype.slice.call(arguments, 1);
            angular.forEach(providers, function (provider) {
                provider.warning(catagory, msg, args);
            });
        }

        function error(msg, exception) {
            if (4 < loggingLevel || hiddenCatagorys.indexOf(catagory) != -1)
                return;

            var args = Array.prototype.slice.call(arguments, 2);
            angular.forEach(providers, function (provider) {
                provider.error(catagory, msg, exception, args);
            });
        }
    }
})(window, angular);