UNPKG

8.93 kBJavaScriptView Raw
1"use strict";
2var Logger_1;
3Object.defineProperty(exports, "__esModule", { value: true });
4exports.Logger = void 0;
5const tslib_1 = require("tslib");
6const core_1 = require("../decorators/core");
7const shared_utils_1 = require("../utils/shared.utils");
8const console_logger_service_1 = require("./console-logger.service");
9const utils_1 = require("./utils");
10const DEFAULT_LOGGER = new console_logger_service_1.ConsoleLogger();
11const dateTimeFormatter = new Intl.DateTimeFormat(undefined, {
12 year: 'numeric',
13 hour: 'numeric',
14 minute: 'numeric',
15 second: 'numeric',
16 day: '2-digit',
17 month: '2-digit',
18});
19/**
20 * @publicApi
21 */
22let Logger = Logger_1 = class Logger {
23 constructor(context, options = {}) {
24 this.context = context;
25 this.options = options;
26 }
27 get localInstance() {
28 if (Logger_1.staticInstanceRef === DEFAULT_LOGGER) {
29 return this.registerLocalInstanceRef();
30 }
31 else if (Logger_1.staticInstanceRef instanceof Logger_1) {
32 const prototype = Object.getPrototypeOf(Logger_1.staticInstanceRef);
33 if (prototype.constructor === Logger_1) {
34 return this.registerLocalInstanceRef();
35 }
36 }
37 return Logger_1.staticInstanceRef;
38 }
39 error(message, ...optionalParams) {
40 optionalParams = this.context
41 ? (optionalParams.length ? optionalParams : [undefined]).concat(this.context)
42 : optionalParams;
43 this.localInstance?.error(message, ...optionalParams);
44 }
45 log(message, ...optionalParams) {
46 optionalParams = this.context
47 ? optionalParams.concat(this.context)
48 : optionalParams;
49 this.localInstance?.log(message, ...optionalParams);
50 }
51 warn(message, ...optionalParams) {
52 optionalParams = this.context
53 ? optionalParams.concat(this.context)
54 : optionalParams;
55 this.localInstance?.warn(message, ...optionalParams);
56 }
57 debug(message, ...optionalParams) {
58 optionalParams = this.context
59 ? optionalParams.concat(this.context)
60 : optionalParams;
61 this.localInstance?.debug?.(message, ...optionalParams);
62 }
63 verbose(message, ...optionalParams) {
64 optionalParams = this.context
65 ? optionalParams.concat(this.context)
66 : optionalParams;
67 this.localInstance?.verbose?.(message, ...optionalParams);
68 }
69 fatal(message, ...optionalParams) {
70 optionalParams = this.context
71 ? optionalParams.concat(this.context)
72 : optionalParams;
73 this.localInstance?.fatal?.(message, ...optionalParams);
74 }
75 static error(message, ...optionalParams) {
76 this.staticInstanceRef?.error(message, ...optionalParams);
77 }
78 static log(message, ...optionalParams) {
79 this.staticInstanceRef?.log(message, ...optionalParams);
80 }
81 static warn(message, ...optionalParams) {
82 this.staticInstanceRef?.warn(message, ...optionalParams);
83 }
84 static debug(message, ...optionalParams) {
85 this.staticInstanceRef?.debug?.(message, ...optionalParams);
86 }
87 static verbose(message, ...optionalParams) {
88 this.staticInstanceRef?.verbose?.(message, ...optionalParams);
89 }
90 static fatal(message, ...optionalParams) {
91 this.staticInstanceRef?.fatal?.(message, ...optionalParams);
92 }
93 /**
94 * Print buffered logs and detach buffer.
95 */
96 static flush() {
97 this.isBufferAttached = false;
98 this.logBuffer.forEach(item => item.methodRef(...item.arguments));
99 this.logBuffer = [];
100 }
101 /**
102 * Attach buffer.
103 * Turns on initialization logs buffering.
104 */
105 static attachBuffer() {
106 this.isBufferAttached = true;
107 }
108 /**
109 * Detach buffer.
110 * Turns off initialization logs buffering.
111 */
112 static detachBuffer() {
113 this.isBufferAttached = false;
114 }
115 static getTimestamp() {
116 return dateTimeFormatter.format(Date.now());
117 }
118 static overrideLogger(logger) {
119 if (Array.isArray(logger)) {
120 Logger_1.logLevels = logger;
121 return this.staticInstanceRef?.setLogLevels(logger);
122 }
123 if ((0, 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 v9. 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 (0, utils_1.isLogLevelEnabled)(level, logLevels);
138 }
139 registerLocalInstanceRef() {
140 if (this.localInstanceRef) {
141 return this.localInstanceRef;
142 }
143 this.localInstanceRef = new console_logger_service_1.ConsoleLogger(this.context, {
144 timestamp: this.options?.timestamp,
145 logLevels: Logger_1.logLevels,
146 });
147 return this.localInstanceRef;
148 }
149};
150exports.Logger = Logger;
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.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.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.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.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.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.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.prototype, "fatal", null);
202tslib_1.__decorate([
203 Logger.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, "error", null);
208tslib_1.__decorate([
209 Logger.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, "log", null);
214tslib_1.__decorate([
215 Logger.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, "warn", null);
220tslib_1.__decorate([
221 Logger.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, "debug", null);
226tslib_1.__decorate([
227 Logger.WrapBuffer,
228 tslib_1.__metadata("design:type", Function),
229 tslib_1.__metadata("design:paramtypes", [Object, Object]),
230 tslib_1.__metadata("design:returntype", void 0)
231], Logger, "verbose", null);
232tslib_1.__decorate([
233 Logger.WrapBuffer,
234 tslib_1.__metadata("design:type", Function),
235 tslib_1.__metadata("design:paramtypes", [Object, Object]),
236 tslib_1.__metadata("design:returntype", void 0)
237], Logger, "fatal", null);
238exports.Logger = Logger = Logger_1 = tslib_1.__decorate([
239 (0, core_1.Injectable)(),
240 tslib_1.__param(0, (0, core_1.Optional)()),
241 tslib_1.__param(1, (0, core_1.Optional)()),
242 tslib_1.__metadata("design:paramtypes", [String, Object])
243], Logger);