11import * as clc from 'cli-color' ;
22import { Injectable , Optional } from '../decorators' ;
3- import { NestEnvironment } from '../enums/nest-environment.enum' ;
43import { isObject } from '../utils/shared.utils' ;
54
65declare const process : any ;
6+ const yellow = clc . xterm ( 3 ) ;
77
88export interface LoggerService {
9- log ( message : any , context ?: string ) : any ;
10- error ( message : any , trace ?: string , context ?: string ) : any ;
11- warn ( message : any , context ?: string ) : any ;
9+ log ( message : any , context ?: string ) ;
10+ error ( message : any , trace ?: string , context ?: string ) ;
11+ warn ( message : any , context ?: string ) ;
1212}
1313
1414@Injectable ( )
1515export class Logger implements LoggerService {
16- private static prevTimestamp ?: number ;
17- private static contextEnvironment = NestEnvironment . RUN ;
18- private static logger ?: typeof Logger | LoggerService = Logger ;
19- private static readonly yellow = clc . xterm ( 3 ) ;
16+ private static lastTimestamp ?: number ;
17+ private static instance ?: typeof Logger | LoggerService = Logger ;
2018
2119 constructor (
2220 @Optional ( ) private readonly context ?: string ,
23- @Optional ( ) private readonly isTimeDiffEnabled = false ,
21+ @Optional ( ) private readonly isTimestampEnabled = false ,
2422 ) { }
2523
26- log ( message : any , context ?: string ) {
27- const { logger } = Logger ;
28- if ( logger === this ) {
29- Logger . log ( message , context || this . context , this . isTimeDiffEnabled ) ;
30- return ;
31- }
32- logger && logger . log . call ( logger , message , context || this . context ) ;
24+ error ( message : any , trace = '' , context ?: string ) {
25+ const instance = this . getInstance ( ) ;
26+ instance &&
27+ instance . error . call ( instance , message , trace , context || this . context ) ;
3328 }
3429
35- error ( message : any , trace = '' , context ?: string ) {
36- const { logger } = Logger ;
37- if ( logger === this ) {
38- Logger . error ( message , trace , context || this . context ) ;
39- return ;
40- }
41- logger &&
42- logger . error . call ( logger , message , trace , context || this . context ) ;
30+ log ( message : any , context ?: string ) {
31+ this . callFunction ( 'log' , message , context ) ;
4332 }
4433
4534 warn ( message : any , context ?: string ) {
46- const { logger } = Logger ;
47- if ( logger === this ) {
48- Logger . warn ( message , context || this . context , this . isTimeDiffEnabled ) ;
49- return ;
50- }
51- logger && logger . warn . call ( logger , message , context || this . context ) ;
35+ this . callFunction ( 'warn' , message , context ) ;
5236 }
5337
54- static overrideLogger ( logger : LoggerService | boolean ) {
55- this . logger = logger ? ( logger as LoggerService ) : undefined ;
38+ debug ( message : any , context ?: string ) {
39+ this . callFunction ( 'debug' , message , context ) ;
5640 }
5741
58- static setMode ( mode : NestEnvironment ) {
59- this . contextEnvironment = mode ;
42+ verbose ( message : any , context ?: string ) {
43+ this . callFunction ( 'verbose' , message , context ) ;
44+ }
45+
46+ static overrideLogger ( logger : LoggerService | boolean ) {
47+ this . instance = isObject ( logger ) ? ( logger as LoggerService ) : undefined ;
6048 }
6149
6250 static log ( message : any , context = '' , isTimeDiffEnabled = true ) {
@@ -77,38 +65,65 @@ export class Logger implements LoggerService {
7765 this . printMessage ( message , clc . yellow , context , isTimeDiffEnabled ) ;
7866 }
7967
68+ static debug ( message : any , context = '' , isTimeDiffEnabled = true ) {
69+ this . printMessage ( message , clc . magentaBright , context , isTimeDiffEnabled ) ;
70+ }
71+
72+ static verbose ( message : any , context = '' , isTimeDiffEnabled = true ) {
73+ this . printMessage ( message , clc . cyanBright , context , isTimeDiffEnabled ) ;
74+ }
75+
76+ private callFunction (
77+ name : 'log' | 'warn' | 'debug' | 'verbose' ,
78+ message : any ,
79+ context ?: string ,
80+ ) {
81+ const instance = this . getInstance ( ) ;
82+ const func = instance && ( instance as typeof Logger ) [ name ] ;
83+ func &&
84+ func . call (
85+ instance ,
86+ message ,
87+ context || this . context ,
88+ this . isTimestampEnabled ,
89+ ) ;
90+ }
91+
92+ private getInstance ( ) : typeof Logger | LoggerService {
93+ const { instance } = Logger ;
94+ return instance === this ? Logger : instance ;
95+ }
96+
8097 private static printMessage (
8198 message : any ,
8299 color : ( message : string ) => string ,
83100 context : string = '' ,
84101 isTimeDiffEnabled ?: boolean ,
85102 ) {
86- if ( Logger . contextEnvironment === NestEnvironment . TEST ) {
87- return ;
88- }
89- const output = isObject ( message ) ? JSON . stringify ( message , null , 2 ) : message ;
103+ const output = isObject ( message )
104+ ? ` ${ color ( 'Object:' ) } \n ${ JSON . stringify ( message , null , 2 ) } \n`
105+ : color ( message ) ;
106+
90107 process . stdout . write ( color ( `[Nest] ${ process . pid } - ` ) ) ;
91108 process . stdout . write ( `${ new Date ( Date . now ( ) ) . toLocaleString ( ) } ` ) ;
92109
93- context && process . stdout . write ( this . yellow ( `[${ context } ] ` ) ) ;
94- process . stdout . write ( color ( output ) ) ;
110+ context && process . stdout . write ( yellow ( `[${ context } ] ` ) ) ;
111+ process . stdout . write ( output ) ;
95112
96113 this . printTimestamp ( isTimeDiffEnabled ) ;
97114 process . stdout . write ( `\n` ) ;
98115 }
99116
100117 private static printTimestamp ( isTimeDiffEnabled ?: boolean ) {
101- const includeTimestamp = Logger . prevTimestamp && isTimeDiffEnabled ;
118+ const includeTimestamp = Logger . lastTimestamp && isTimeDiffEnabled ;
102119 if ( includeTimestamp ) {
103- process . stdout . write (
104- this . yellow ( ` +${ Date . now ( ) - Logger . prevTimestamp } ms` ) ,
105- ) ;
120+ process . stdout . write ( yellow ( ` +${ Date . now ( ) - Logger . lastTimestamp } ms` ) ) ;
106121 }
107- Logger . prevTimestamp = Date . now ( ) ;
122+ Logger . lastTimestamp = Date . now ( ) ;
108123 }
109124
110125 private static printStackTrace ( trace : string ) {
111- if ( this . contextEnvironment === NestEnvironment . TEST || ! trace ) {
126+ if ( ! trace ) {
112127 return ;
113128 }
114129 process . stdout . write ( trace ) ;
0 commit comments