UNPKG

8.97 kBJavaScriptView Raw
1"use strict";
2var Logger_1;
3Object.defineProperty(exports, "__esModule", { value: true });
4exports.Logger = void 0;
5const tslib_1 = require("tslib");
6const injectable_decorator_1 = require("../decorators/core/injectable.decorator");
7const optional_decorator_1 = require("../decorators/core/optional.decorator");
8const shared_utils_1 = require("../utils/shared.utils");
9const console_logger_service_1 = require("./console-logger.service");
10const utils_1 = require("./utils");
11const DEFAULT_LOGGER = new console_logger_service_1.ConsoleLogger();
12let Logger = Logger_1 = class Logger {
13 constructor(context, options = {}) {
14 this.context = context;
15 this.options = options;
16 }
17 get localInstance() {
18 if (Logger_1.staticInstanceRef === DEFAULT_LOGGER) {
19 return this.registerLocalInstanceRef();
20 }
21 else if (Logger_1.staticInstanceRef instanceof Logger_1) {
22 const prototype = Object.getPrototypeOf(Logger_1.staticInstanceRef);
23 if (prototype.constructor === Logger_1) {
24 return this.registerLocalInstanceRef();
25 }
26 }
27 return Logger_1.staticInstanceRef;
28 }
29 error(message, ...optionalParams) {
30 var _a;
31 optionalParams = this.context
32 ? optionalParams.concat(this.context)
33 : optionalParams;
34 (_a = this.localInstance) === null || _a === void 0 ? void 0 : _a.error(message, ...optionalParams);
35 }
36 log(message, ...optionalParams) {
37 var _a;
38 optionalParams = this.context
39 ? optionalParams.concat(this.context)
40 : optionalParams;
41 (_a = this.localInstance) === null || _a === void 0 ? void 0 : _a.log(message, ...optionalParams);
42 }
43 warn(message, ...optionalParams) {
44 var _a;
45 optionalParams = this.context
46 ? optionalParams.concat(this.context)
47 : optionalParams;
48 (_a = this.localInstance) === null || _a === void 0 ? void 0 : _a.warn(message, ...optionalParams);
49 }
50 debug(message, ...optionalParams) {
51 var _a;
52 optionalParams = this.context
53 ? optionalParams.concat(this.context)
54 : optionalParams;
55 (_a = this.localInstance) === null || _a === void 0 ? void 0 : _a.debug(message, ...optionalParams);
56 }
57 verbose(message, ...optionalParams) {
58 var _a;
59 optionalParams = this.context
60 ? optionalParams.concat(this.context)
61 : optionalParams;
62 (_a = this.localInstance) === null || _a === void 0 ? void 0 : _a.verbose(message, ...optionalParams);
63 }
64 static error(message, ...optionalParams) {
65 var _a;
66 (_a = this.staticInstanceRef) === null || _a === void 0 ? void 0 : _a.error(message, ...optionalParams);
67 }
68 static log(message, ...optionalParams) {
69 var _a;
70 (_a = this.staticInstanceRef) === null || _a === void 0 ? void 0 : _a.log(message, ...optionalParams);
71 }
72 static warn(message, ...optionalParams) {
73 var _a;
74 (_a = this.staticInstanceRef) === null || _a === void 0 ? void 0 : _a.warn(message, ...optionalParams);
75 }
76 static debug(message, ...optionalParams) {
77 var _a;
78 (_a = this.staticInstanceRef) === null || _a === void 0 ? void 0 : _a.debug(message, ...optionalParams);
79 }
80 static verbose(message, ...optionalParams) {
81 var _a;
82 (_a = this.staticInstanceRef) === null || _a === void 0 ? void 0 : _a.verbose(message, ...optionalParams);
83 }
84 /**
85 * Print buffered logs and detach buffer.
86 */
87 static flush() {
88 this.isBufferAttached = false;
89 this.logBuffer.forEach(item => item.methodRef(...item.arguments));
90 this.logBuffer = [];
91 }
92 /**
93 * Attach buffer.
94 * Turns on initialisation logs buffering.
95 */
96 static attachBuffer() {
97 this.isBufferAttached = true;
98 }
99 /**
100 * Detach buffer.
101 * Turns off initialisation logs buffering.
102 */
103 static detachBuffer() {
104 this.isBufferAttached = false;
105 }
106 static getTimestamp() {
107 const localeStringOptions = {
108 year: 'numeric',
109 hour: 'numeric',
110 minute: 'numeric',
111 second: 'numeric',
112 day: '2-digit',
113 month: '2-digit',
114 };
115 return new Date(Date.now()).toLocaleString(undefined, localeStringOptions);
116 }
117 static overrideLogger(logger) {
118 var _a;
119 if (Array.isArray(logger)) {
120 Logger_1.logLevels = logger;
121 return (_a = this.staticInstanceRef) === null || _a === void 0 ? void 0 : _a.setLogLevels(logger);
122 }
123 if (shared_utils_1.isObject(logger)) {
124 if (logger instanceof Logger_1 && logger.constructor !== Logger_1) {
125 const errorMessage = `Using the "extends Logger" instruction is not allowed in Nest v8. Please, use "extends ConsoleLogger" instead.`;
126 this.staticInstanceRef.error(errorMessage);
127 throw new Error(errorMessage);
128 }
129 this.staticInstanceRef = logger;
130 }
131 else {
132 this.staticInstanceRef = undefined;
133 }
134 }
135 static isLevelEnabled(level) {
136 const logLevels = Logger_1.logLevels;
137 return utils_1.isLogLevelEnabled(level, logLevels);
138 }
139 registerLocalInstanceRef() {
140 var _a;
141 if (this.localInstanceRef) {
142 return this.localInstanceRef;
143 }
144 this.localInstanceRef = new console_logger_service_1.ConsoleLogger(this.context, {
145 timestamp: (_a = this.options) === null || _a === void 0 ? void 0 : _a.timestamp,
146 logLevels: Logger_1.logLevels,
147 });
148 return this.localInstanceRef;
149 }
150};
151Logger.logBuffer = new Array();
152Logger.staticInstanceRef = DEFAULT_LOGGER;
153Logger.WrapBuffer = (target, propertyKey, descriptor) => {
154 const originalFn = descriptor.value;
155 descriptor.value = function (...args) {
156 if (Logger_1.isBufferAttached) {
157 Logger_1.logBuffer.push({
158 methodRef: originalFn.bind(this),
159 arguments: args,
160 });
161 return;
162 }
163 return originalFn.call(this, ...args);
164 };
165};
166tslib_1.__decorate([
167 Logger_1.WrapBuffer,
168 tslib_1.__metadata("design:type", Function),
169 tslib_1.__metadata("design:paramtypes", [Object, Object]),
170 tslib_1.__metadata("design:returntype", void 0)
171], Logger.prototype, "error", null);
172tslib_1.__decorate([
173 Logger_1.WrapBuffer,
174 tslib_1.__metadata("design:type", Function),
175 tslib_1.__metadata("design:paramtypes", [Object, Object]),
176 tslib_1.__metadata("design:returntype", void 0)
177], Logger.prototype, "log", null);
178tslib_1.__decorate([
179 Logger_1.WrapBuffer,
180 tslib_1.__metadata("design:type", Function),
181 tslib_1.__metadata("design:paramtypes", [Object, Object]),
182 tslib_1.__metadata("design:returntype", void 0)
183], Logger.prototype, "warn", null);
184tslib_1.__decorate([
185 Logger_1.WrapBuffer,
186 tslib_1.__metadata("design:type", Function),
187 tslib_1.__metadata("design:paramtypes", [Object, Object]),
188 tslib_1.__metadata("design:returntype", void 0)
189], Logger.prototype, "debug", null);
190tslib_1.__decorate([
191 Logger_1.WrapBuffer,
192 tslib_1.__metadata("design:type", Function),
193 tslib_1.__metadata("design:paramtypes", [Object, Object]),
194 tslib_1.__metadata("design:returntype", void 0)
195], Logger.prototype, "verbose", null);
196tslib_1.__decorate([
197 Logger_1.WrapBuffer,
198 tslib_1.__metadata("design:type", Function),
199 tslib_1.__metadata("design:paramtypes", [Object, Object]),
200 tslib_1.__metadata("design:returntype", void 0)
201], Logger, "error", null);
202tslib_1.__decorate([
203 Logger_1.WrapBuffer,
204 tslib_1.__metadata("design:type", Function),
205 tslib_1.__metadata("design:paramtypes", [Object, Object]),
206 tslib_1.__metadata("design:returntype", void 0)
207], Logger, "log", null);
208tslib_1.__decorate([
209 Logger_1.WrapBuffer,
210 tslib_1.__metadata("design:type", Function),
211 tslib_1.__metadata("design:paramtypes", [Object, Object]),
212 tslib_1.__metadata("design:returntype", void 0)
213], Logger, "warn", null);
214tslib_1.__decorate([
215 Logger_1.WrapBuffer,
216 tslib_1.__metadata("design:type", Function),
217 tslib_1.__metadata("design:paramtypes", [Object, Object]),
218 tslib_1.__metadata("design:returntype", void 0)
219], Logger, "debug", null);
220tslib_1.__decorate([
221 Logger_1.WrapBuffer,
222 tslib_1.__metadata("design:type", Function),
223 tslib_1.__metadata("design:paramtypes", [Object, Object]),
224 tslib_1.__metadata("design:returntype", void 0)
225], Logger, "verbose", null);
226Logger = Logger_1 = tslib_1.__decorate([
227 injectable_decorator_1.Injectable(),
228 tslib_1.__param(0, optional_decorator_1.Optional()),
229 tslib_1.__param(1, optional_decorator_1.Optional()),
230 tslib_1.__metadata("design:paramtypes", [String, Object])
231], Logger);
232exports.Logger = Logger;