11import * as clc from 'cli-color' ;
2+ import { Injectable , Optional } from '../decorators' ;
23import { NestEnvironment } from '../enums/nest-environment.enum' ;
3- import { Constructor } from '../utils/merge-with-values.util ' ;
4+ import { isObject } from '../utils/shared.utils ' ;
45
56declare const process ;
67
78export interface LoggerService {
8- log ( message : string ) ;
9- error ( message : string , trace : string ) ;
10- warn ( message : string ) ;
9+ log ( message : any , context ?: string ) ;
10+ error ( message : any , trace ?: string , context ? : string ) ;
11+ warn ( message : any , context ?: string ) ;
1112}
1213
14+ @Injectable ( )
1315export class Logger implements LoggerService {
1416 private static prevTimestamp = null ;
15- private static contextEnv = NestEnvironment . RUN ;
17+ private static contextEnvironment = NestEnvironment . RUN ;
1618 private static logger : typeof Logger | LoggerService = Logger ;
17-
1819 private static readonly yellow = clc . xterm ( 3 ) ;
1920
2021 constructor (
21- private readonly context : string ,
22- private readonly isTimeDiffEnabled = false ,
22+ @ Optional ( ) private readonly context : string ,
23+ @ Optional ( ) private readonly isTimeDiffEnabled = false ,
2324 ) { }
2425
25- log ( message : string ) {
26+ log ( message : any , context ?: string ) {
2627 const { logger } = Logger ;
28+ if ( logger === this ) {
29+ Logger . log ( message , context || this . context , this . isTimeDiffEnabled ) ;
30+ return ;
31+ }
2732 logger &&
28- logger . log . call ( logger , message , this . context , this . isTimeDiffEnabled ) ;
33+ logger . log . call (
34+ logger ,
35+ message ,
36+ context || this . context ,
37+ this . isTimeDiffEnabled ,
38+ ) ;
2939 }
3040
31- error ( message : string , trace = '' ) {
41+ error ( message : any , trace = '' , context ?: string ) {
3242 const { logger } = Logger ;
43+ if ( logger === this ) {
44+ Logger . error (
45+ message ,
46+ trace ,
47+ context || this . context ,
48+ this . isTimeDiffEnabled ,
49+ ) ;
50+ return ;
51+ }
3352 logger &&
3453 logger . error . call (
3554 logger ,
3655 message ,
3756 trace ,
38- this . context ,
57+ context || this . context ,
3958 this . isTimeDiffEnabled ,
4059 ) ;
4160 }
4261
43- warn ( message : string ) {
62+ warn ( message : any , context ?: string ) {
4463 const { logger } = Logger ;
64+ if ( logger === this ) {
65+ Logger . warn ( message , context || this . context , this . isTimeDiffEnabled ) ;
66+ return ;
67+ }
4568 logger &&
46- logger . warn . call ( logger , message , this . context , this . isTimeDiffEnabled ) ;
69+ logger . warn . call (
70+ logger ,
71+ message ,
72+ context || this . context ,
73+ this . isTimeDiffEnabled ,
74+ ) ;
4775 }
4876
4977 static overrideLogger ( logger : LoggerService | boolean ) {
5078 this . logger = logger ? ( logger as LoggerService ) : null ;
5179 }
5280
5381 static setMode ( mode : NestEnvironment ) {
54- this . contextEnv = mode ;
82+ this . contextEnvironment = mode ;
5583 }
5684
57- static log ( message : string , context = '' , isTimeDiffEnabled = true ) {
85+ static log ( message : any , context = '' , isTimeDiffEnabled = true ) {
5886 this . printMessage ( message , clc . green , context , isTimeDiffEnabled ) ;
5987 }
6088
6189 static error (
62- message : string ,
90+ message : any ,
6391 trace = '' ,
6492 context = '' ,
6593 isTimeDiffEnabled = true ,
@@ -68,22 +96,26 @@ export class Logger implements LoggerService {
6896 this . printStackTrace ( trace ) ;
6997 }
7098
71- static warn ( message : string , context = '' , isTimeDiffEnabled = true ) {
99+ static warn ( message : any , context = '' , isTimeDiffEnabled = true ) {
72100 this . printMessage ( message , clc . yellow , context , isTimeDiffEnabled ) ;
73101 }
74102
75103 private static printMessage (
76- message : string ,
77- color : ( msg : string ) => string ,
104+ message : any ,
105+ color : ( message : string ) => string ,
78106 context : string = '' ,
79107 isTimeDiffEnabled ?: boolean ,
80108 ) {
81- if ( Logger . contextEnv === NestEnvironment . TEST ) return ;
82-
109+ if ( Logger . contextEnvironment === NestEnvironment . TEST ) {
110+ return void 0 ;
111+ }
112+ const output =
113+ message && isObject ( message ) ? JSON . stringify ( message , null , 2 ) : message ;
83114 process . stdout . write ( color ( `[Nest] ${ process . pid } - ` ) ) ;
84115 process . stdout . write ( `${ new Date ( Date . now ( ) ) . toLocaleString ( ) } ` ) ;
85- process . stdout . write ( this . yellow ( `[${ context } ] ` ) ) ;
86- process . stdout . write ( color ( message ) ) ;
116+
117+ context && process . stdout . write ( this . yellow ( `[${ context } ] ` ) ) ;
118+ process . stdout . write ( color ( output ) ) ;
87119
88120 this . printTimestamp ( isTimeDiffEnabled ) ;
89121 process . stdout . write ( `\n` ) ;
@@ -100,8 +132,9 @@ export class Logger implements LoggerService {
100132 }
101133
102134 private static printStackTrace ( trace : string ) {
103- if ( this . contextEnv === NestEnvironment . TEST || ! trace ) return ;
104-
135+ if ( this . contextEnvironment === NestEnvironment . TEST || ! trace ) {
136+ return void 0 ;
137+ }
105138 process . stdout . write ( trace ) ;
106139 process . stdout . write ( `\n` ) ;
107140 }
0 commit comments