logger.js
7.31 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Logger = exports.LoggerLevel = void 0;
const util_1 = require("util");
const error_1 = require("./error");
const utils_1 = require("./utils");
// Filters for classes
const classFilters = {};
let filteredClasses = {};
let level;
// Save the process id
const pid = process.pid;
// current logger
// eslint-disable-next-line no-console
let currentLogger = console.warn;
/** @public */
exports.LoggerLevel = Object.freeze({
ERROR: 'error',
WARN: 'warn',
INFO: 'info',
DEBUG: 'debug',
error: 'error',
warn: 'warn',
info: 'info',
debug: 'debug'
});
/**
* @public
*/
class Logger {
/**
* Creates a new Logger instance
*
* @param className - The Class name associated with the logging instance
* @param options - Optional logging settings
*/
constructor(className, options) {
options = options !== null && options !== void 0 ? options : {};
// Current reference
this.className = className;
// Current logger
if (!(options.logger instanceof Logger) && typeof options.logger === 'function') {
currentLogger = options.logger;
}
// Set level of logging, default is error
if (options.loggerLevel) {
level = options.loggerLevel || exports.LoggerLevel.ERROR;
}
// Add all class names
if (filteredClasses[this.className] == null) {
classFilters[this.className] = true;
}
}
/**
* Log a message at the debug level
*
* @param message - The message to log
* @param object - Additional meta data to log
*/
debug(message, object) {
if (this.isDebug() &&
((Object.keys(filteredClasses).length > 0 && filteredClasses[this.className]) ||
(Object.keys(filteredClasses).length === 0 && classFilters[this.className]))) {
const dateTime = new Date().getTime();
const msg = (0, util_1.format)('[%s-%s:%s] %s %s', 'DEBUG', this.className, pid, dateTime, message);
const state = {
type: exports.LoggerLevel.DEBUG,
message,
className: this.className,
pid,
date: dateTime
};
if (object)
state.meta = object;
currentLogger(msg, state);
}
}
/**
* Log a message at the warn level
*
* @param message - The message to log
* @param object - Additional meta data to log
*/
warn(message, object) {
if (this.isWarn() &&
((Object.keys(filteredClasses).length > 0 && filteredClasses[this.className]) ||
(Object.keys(filteredClasses).length === 0 && classFilters[this.className]))) {
const dateTime = new Date().getTime();
const msg = (0, util_1.format)('[%s-%s:%s] %s %s', 'WARN', this.className, pid, dateTime, message);
const state = {
type: exports.LoggerLevel.WARN,
message,
className: this.className,
pid,
date: dateTime
};
if (object)
state.meta = object;
currentLogger(msg, state);
}
}
/**
* Log a message at the info level
*
* @param message - The message to log
* @param object - Additional meta data to log
*/
info(message, object) {
if (this.isInfo() &&
((Object.keys(filteredClasses).length > 0 && filteredClasses[this.className]) ||
(Object.keys(filteredClasses).length === 0 && classFilters[this.className]))) {
const dateTime = new Date().getTime();
const msg = (0, util_1.format)('[%s-%s:%s] %s %s', 'INFO', this.className, pid, dateTime, message);
const state = {
type: exports.LoggerLevel.INFO,
message,
className: this.className,
pid,
date: dateTime
};
if (object)
state.meta = object;
currentLogger(msg, state);
}
}
/**
* Log a message at the error level
*
* @param message - The message to log
* @param object - Additional meta data to log
*/
error(message, object) {
if (this.isError() &&
((Object.keys(filteredClasses).length > 0 && filteredClasses[this.className]) ||
(Object.keys(filteredClasses).length === 0 && classFilters[this.className]))) {
const dateTime = new Date().getTime();
const msg = (0, util_1.format)('[%s-%s:%s] %s %s', 'ERROR', this.className, pid, dateTime, message);
const state = {
type: exports.LoggerLevel.ERROR,
message,
className: this.className,
pid,
date: dateTime
};
if (object)
state.meta = object;
currentLogger(msg, state);
}
}
/** Is the logger set at info level */
isInfo() {
return level === exports.LoggerLevel.INFO || level === exports.LoggerLevel.DEBUG;
}
/** Is the logger set at error level */
isError() {
return level === exports.LoggerLevel.ERROR || level === exports.LoggerLevel.INFO || level === exports.LoggerLevel.DEBUG;
}
/** Is the logger set at error level */
isWarn() {
return (level === exports.LoggerLevel.ERROR ||
level === exports.LoggerLevel.WARN ||
level === exports.LoggerLevel.INFO ||
level === exports.LoggerLevel.DEBUG);
}
/** Is the logger set at debug level */
isDebug() {
return level === exports.LoggerLevel.DEBUG;
}
/** Resets the logger to default settings, error and no filtered classes */
static reset() {
level = exports.LoggerLevel.ERROR;
filteredClasses = {};
}
/** Get the current logger function */
static currentLogger() {
return currentLogger;
}
/**
* Set the current logger function
*
* @param logger - Custom logging function
*/
static setCurrentLogger(logger) {
if (typeof logger !== 'function') {
throw new error_1.MongoInvalidArgumentError('Current logger must be a function');
}
currentLogger = logger;
}
/**
* Filter log messages for a particular class
*
* @param type - The type of filter (currently only class)
* @param values - The filters to apply
*/
static filter(type, values) {
if (type === 'class' && Array.isArray(values)) {
filteredClasses = {};
values.forEach(x => (filteredClasses[x] = true));
}
}
/**
* Set the current log level
*
* @param newLevel - Set current log level (debug, warn, info, error)
*/
static setLevel(newLevel) {
if (newLevel !== exports.LoggerLevel.INFO &&
newLevel !== exports.LoggerLevel.ERROR &&
newLevel !== exports.LoggerLevel.DEBUG &&
newLevel !== exports.LoggerLevel.WARN) {
throw new error_1.MongoInvalidArgumentError(`Argument "newLevel" should be one of ${(0, utils_1.enumToString)(exports.LoggerLevel)}`);
}
level = newLevel;
}
}
exports.Logger = Logger;
//# sourceMappingURL=logger.js.map