UNPKG

4.63 kBJavaScriptView Raw
1"use strict";
2var Logger_1;
3Object.defineProperty(exports, "__esModule", { value: true });
4const tslib_1 = require("tslib");
5const clc = require("cli-color");
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 yellow = clc.xterm(3);
10let Logger = Logger_1 = class Logger {
11 constructor(context, isTimestampEnabled = false) {
12 this.context = context;
13 this.isTimestampEnabled = isTimestampEnabled;
14 }
15 error(message, trace = '', context) {
16 const instance = this.getInstance();
17 if (!this.isLogLevelEnabled('error')) {
18 return;
19 }
20 instance &&
21 instance.error.call(instance, message, trace, context || this.context);
22 }
23 log(message, context) {
24 this.callFunction('log', message, context);
25 }
26 warn(message, context) {
27 this.callFunction('warn', message, context);
28 }
29 debug(message, context) {
30 this.callFunction('debug', message, context);
31 }
32 verbose(message, context) {
33 this.callFunction('verbose', message, context);
34 }
35 setContext(context) {
36 this.context = context;
37 }
38 static overrideLogger(logger) {
39 if (Array.isArray(logger)) {
40 this.logLevels = logger;
41 return;
42 }
43 this.instance = shared_utils_1.isObject(logger) ? logger : undefined;
44 }
45 static log(message, context = '', isTimeDiffEnabled = true) {
46 this.printMessage(message, clc.green, context, isTimeDiffEnabled);
47 }
48 static error(message, trace = '', context = '', isTimeDiffEnabled = true) {
49 this.printMessage(message, clc.red, context, isTimeDiffEnabled);
50 this.printStackTrace(trace);
51 }
52 static warn(message, context = '', isTimeDiffEnabled = true) {
53 this.printMessage(message, clc.yellow, context, isTimeDiffEnabled);
54 }
55 static debug(message, context = '', isTimeDiffEnabled = true) {
56 this.printMessage(message, clc.magentaBright, context, isTimeDiffEnabled);
57 }
58 static verbose(message, context = '', isTimeDiffEnabled = true) {
59 this.printMessage(message, clc.cyanBright, context, isTimeDiffEnabled);
60 }
61 callFunction(name, message, context) {
62 if (!this.isLogLevelEnabled(name)) {
63 return;
64 }
65 const instance = this.getInstance();
66 const func = instance && instance[name];
67 func &&
68 func.call(instance, message, context || this.context, this.isTimestampEnabled);
69 }
70 getInstance() {
71 const { instance } = Logger_1;
72 return instance === this ? Logger_1 : instance;
73 }
74 isLogLevelEnabled(level) {
75 return Logger_1.logLevels.includes(level);
76 }
77 static printMessage(message, color, context = '', isTimeDiffEnabled) {
78 const output = shared_utils_1.isObject(message)
79 ? `${color('Object:')}\n${JSON.stringify(message, null, 2)}\n`
80 : color(message);
81 const localeStringOptions = {
82 year: 'numeric',
83 hour: 'numeric',
84 minute: 'numeric',
85 second: 'numeric',
86 day: '2-digit',
87 month: '2-digit',
88 };
89 const timestamp = new Date(Date.now()).toLocaleString(undefined, localeStringOptions);
90 const pidMessage = color(`[Nest] ${process.pid} - `);
91 const contextMessage = context ? yellow(`[${context}] `) : '';
92 const timestampDiff = this.updateAndGetTimestampDiff(isTimeDiffEnabled);
93 process.stdout.write(`${pidMessage}${timestamp} ${contextMessage}${output}${timestampDiff}\n`);
94 }
95 static updateAndGetTimestampDiff(isTimeDiffEnabled) {
96 const includeTimestamp = Logger_1.lastTimestamp && isTimeDiffEnabled;
97 const result = includeTimestamp
98 ? yellow(` +${Date.now() - Logger_1.lastTimestamp}ms`)
99 : '';
100 Logger_1.lastTimestamp = Date.now();
101 return result;
102 }
103 static printStackTrace(trace) {
104 if (!trace) {
105 return;
106 }
107 process.stdout.write(`${trace}\n`);
108 }
109};
110Logger.logLevels = [
111 'log',
112 'error',
113 'warn',
114 'debug',
115 'verbose',
116];
117Logger.instance = Logger_1;
118Logger = Logger_1 = tslib_1.__decorate([
119 injectable_decorator_1.Injectable(),
120 tslib_1.__param(0, optional_decorator_1.Optional()),
121 tslib_1.__param(1, optional_decorator_1.Optional()),
122 tslib_1.__metadata("design:paramtypes", [String, Object])
123], Logger);
124exports.Logger = Logger;