1 | /* Copyright @ 2015-present 8x8, Inc.
|
2 | *
|
3 | * Licensed under the Apache License, Version 2.0 (the "License");
|
4 | * you may not use this file except in compliance with the License.
|
5 | * You may obtain a copy of the License at
|
6 | *
|
7 | * http://www.apache.org/licenses/LICENSE-2.0
|
8 | *
|
9 | * Unless required by applicable law or agreed to in writing, software
|
10 | * distributed under the License is distributed on an "AS IS" BASIS,
|
11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12 | * See the License for the specific language governing permissions and
|
13 | * limitations under the License.
|
14 | */
|
15 | var Logger = require("./Logger");
|
16 | var LogCollector = require("./LogCollector");
|
17 |
|
18 | /**
|
19 | * Definition of the log method
|
20 | * @name log_method
|
21 | * @function
|
22 | * @param {...*} log_args the arguments to be logged
|
23 | */
|
24 | /**
|
25 | * The logger's transport type definition.
|
26 | *
|
27 | * @typedef {object} LoggerTransport
|
28 | *
|
29 | * @property {log_method} trace method called to log on {@link Logger.levels.TRACE} logging level
|
30 | * @property {log_method} debug method called to log on {@link Logger.levels.DEBUG} logging level
|
31 | * @property {log_method} info method called to log on {@link Logger.levels.INFO} logging level
|
32 | * @property {log_method} log method called to log on {@link Logger.levels.LOG} logging level
|
33 | * @property {log_method} warn method called to log on {@link Logger.levels.WARN} logging level
|
34 | * @property {log_method} error method called to log on {@link Logger.levels.ERROR} logging level
|
35 | */
|
36 |
|
37 | /**
|
38 | * Map with the created loggers with ID.
|
39 | */
|
40 | var idLoggers = {};
|
41 |
|
42 | /**
|
43 | * Array with the loggers without id.
|
44 | */
|
45 | var loggers = [];
|
46 |
|
47 | /**
|
48 | * Log level for the lbrary.
|
49 | */
|
50 | var curLevel = Logger.levels.TRACE;
|
51 |
|
52 |
|
53 | module.exports = {
|
54 | /**
|
55 | * Adds given {@link LoggerTransport} instance to the list of global
|
56 | * transports which means that it'll be used by all {@link Logger}s
|
57 | * @param {LoggerTransport} transport
|
58 | */
|
59 | addGlobalTransport: function(transport) {
|
60 | Logger.addGlobalTransport(transport);
|
61 | },
|
62 | /**
|
63 | * Removes given {@link LoggerTransport} instance from the list of global
|
64 | * transports
|
65 | * @param {LoggerTransport} transport
|
66 | */
|
67 | removeGlobalTransport: function(transport) {
|
68 | Logger.removeGlobalTransport(transport);
|
69 | },
|
70 | /**
|
71 | * Sets global options which will be used by all loggers. Changing these
|
72 | * works even after other loggers are created.
|
73 | */
|
74 | setGlobalOptions: function(options) {
|
75 | Logger.setGlobalOptions(options);
|
76 | },
|
77 | /**
|
78 | * Creates new logger.
|
79 | * @arguments the same as Logger constructor
|
80 | */
|
81 | getLogger: function(id, transports, options) {
|
82 | var logger = new Logger(curLevel, id, transports, options);
|
83 | if(id) {
|
84 | idLoggers[id] = idLoggers[id] || [];
|
85 | idLoggers[id].push(logger);
|
86 | } else {
|
87 | loggers.push(logger);
|
88 | }
|
89 | return logger;
|
90 | },
|
91 | /**
|
92 | * Creates a new Logger, without keeping track of it in the loggers list
|
93 | * @arguments the same as Logger constructor
|
94 | */
|
95 | getUntrackedLogger: function(id, transports, options) {
|
96 | return new Logger(curLevel, id, transports, options);
|
97 | },
|
98 | /**
|
99 | * Changes the log level for the existing loggers by id.
|
100 | * @param level the new log level.
|
101 | * @param id if specified the level will be changed only for loggers with the
|
102 | * same id. Otherwise the operation will affect all loggers that don't
|
103 | * have id.
|
104 | */
|
105 | setLogLevelById: function(level, id) {
|
106 | var l = id? (idLoggers[id] || []) : loggers;
|
107 | for(var i = 0; i < l.length; i++) {
|
108 | l[i].setLevel(level);
|
109 | }
|
110 | },
|
111 | /**
|
112 | * Changes the log level for all existing loggers.
|
113 | * @param level the new log level.
|
114 | */
|
115 | setLogLevel: function (level) {
|
116 | curLevel = level;
|
117 | var i = 0;
|
118 | for(; i < loggers.length; i++) {
|
119 | loggers[i].setLevel(level);
|
120 | }
|
121 |
|
122 | for(var id in idLoggers) {
|
123 | var l = idLoggers[id] || [];
|
124 | for(i = 0; i < l.length; i++) {
|
125 | l[i].setLevel(level);
|
126 | }
|
127 | }
|
128 | },
|
129 | /**
|
130 | * The supported log levels.
|
131 | */
|
132 | levels: Logger.levels,
|
133 | /**
|
134 | * Exports the <tt>LogCollector</tt>.
|
135 | */
|
136 | LogCollector: LogCollector
|
137 | };
|