@@ -30,20 +30,20 @@ export interface LoggerConfig {
30
30
}
31
31
32
32
export class Logger {
33
- _namespace : string ;
34
- _enabled : boolean ;
33
+ private readonly namespace : string ;
34
+ private readonly enabled : boolean ;
35
35
36
36
constructor ( namespace : string , enabled : boolean ) {
37
- this . _namespace = namespace ;
38
- this . _enabled = enabled ;
37
+ this . namespace = namespace ;
38
+ this . enabled = enabled ;
39
39
}
40
40
41
41
static configure ( options : Partial < LoggerConfig > ) {
42
- this . _validate ( options ) ;
42
+ this . validate ( options ) ;
43
43
Object . assign ( Logger . config , options ) ;
44
44
}
45
45
46
- static _validate ( options : Partial < LoggerConfig > ) {
46
+ private static validate ( options : Partial < LoggerConfig > ) {
47
47
Object . keys ( options ) . forEach ( ( key ) => {
48
48
if ( ! allowedKeys . includes ( key ) ) {
49
49
throw new Error ( 'Only the following keys are allowed: formatter, output' ) ;
@@ -58,41 +58,57 @@ export class Logger {
58
58
} ;
59
59
60
60
isEnabled ( ) {
61
- return this . _enabled ;
61
+ return this . enabled ;
62
62
}
63
63
64
64
trace ( action : string , data : unknown = { } ) {
65
- this . _log ( 'trace' , action , data ) ;
65
+ this . log ( 'trace' , action , data ) ;
66
66
}
67
67
68
68
debug ( action : string , data : unknown = { } ) {
69
- this . _log ( 'debug' , action , data ) ;
69
+ this . log ( 'debug' , action , data ) ;
70
70
}
71
71
72
72
info ( action : string , data : unknown = { } ) {
73
- this . _log ( 'info' , action , data ) ;
73
+ this . log ( 'info' , action , data ) ;
74
74
}
75
75
76
76
warn ( action : string , data : unknown = { } ) {
77
- this . _log ( 'warn' , action , data ) ;
77
+ this . log ( 'warn' , action , data ) ;
78
78
}
79
79
80
80
error ( action : string , data : unknown = { } ) {
81
- this . _log ( 'error' , action , data ) ;
81
+ this . log ( 'error' , action , data ) ;
82
82
}
83
83
84
84
fatal ( action : string , data : unknown = { } ) {
85
- this . _log ( 'fatal' , action , data ) ;
85
+ this . log ( 'fatal' , action , data ) ;
86
86
}
87
87
88
- _log ( level : string , action : string , data : unknown ) {
89
- if ( ! this . _enabled ) {
88
+ customError ( severity : string , action : string , error : Error , data : unknown = { } ) {
89
+ this . log ( severity , action , Object . assign ( this . getErrorDetails ( error ) , data ) ) ;
90
+ }
91
+
92
+ fromError ( action : string , error : unknown , data : unknown = { } ) {
93
+ this . customError ( 'error' , action , error as Error , data ) ;
94
+ }
95
+
96
+ warnFromError ( action : string , error : unknown , data : unknown = { } ) {
97
+ this . customError ( 'warn' , action , error as Error , data ) ;
98
+ }
99
+
100
+ timer ( ) {
101
+ return new Timer ( this ) ;
102
+ }
103
+
104
+ private log ( level : string , action : string , data : unknown ) {
105
+ if ( ! this . enabled ) {
90
106
return ;
91
107
}
92
108
93
109
let dataToLog = Object . assign (
94
110
{
95
- name : this . _namespace ,
111
+ name : this . namespace ,
96
112
action : action ,
97
113
level : config . levels [ level ] . number ,
98
114
time : new Date ( ) . toISOString ( ) ,
@@ -107,31 +123,15 @@ export class Logger {
107
123
Logger . config . output ( Logger . config . formatter ( dataToLog ) ) ;
108
124
}
109
125
110
- customError ( severity : string , action : string , error : Error , data : unknown = { } ) {
111
- this . _log ( severity , action , Object . assign ( this . _getErrorDetails ( error ) , data ) ) ;
112
- }
113
-
114
- fromError ( action : string , error : unknown , data : unknown = { } ) {
115
- this . customError ( 'error' , action , error as Error , data ) ;
116
- }
117
-
118
- warnFromError ( action : string , error : unknown , data : unknown = { } ) {
119
- this . customError ( 'warn' , action , error as Error , data ) ;
120
- }
121
-
122
- timer ( ) {
123
- return new Timer ( this ) ;
124
- }
125
-
126
- _shortenStackTrace ( stack : string ) {
126
+ private shortenStackTrace ( stack : string ) {
127
127
if ( ! stack ) {
128
128
return ;
129
129
}
130
130
131
131
return stack . length > STACK_TRACE_LIMIT ? stack . substring ( 0 , STACK_TRACE_LIMIT ) + ' ...' : stack ;
132
132
}
133
133
134
- _shortenData ( data : unknown ) {
134
+ private shortenData ( data : unknown ) {
135
135
if ( typeof data === 'undefined' ) {
136
136
return ;
137
137
}
@@ -141,22 +141,22 @@ export class Logger {
141
141
return stringifiedData . length > DATA_LIMIT ? stringifiedData . substring ( 0 , DATA_LIMIT ) + ' ...' : stringifiedData ;
142
142
}
143
143
144
- _getErrorDetails ( error : Error ) {
144
+ private getErrorDetails ( error : Error ) {
145
145
if ( ! ( error instanceof Object ) ) {
146
146
return { } ;
147
147
}
148
148
149
149
const baseDetails = {
150
150
error_name : error . name ,
151
- error_stack : this . _shortenStackTrace ( error . stack || '' ) ,
151
+ error_stack : this . shortenStackTrace ( error . stack || '' ) ,
152
152
error_message : error . message ,
153
- error_data : this . _shortenData ( ( error as ErrorWithData ) . data ) ,
153
+ error_data : this . shortenData ( ( error as ErrorWithData ) . data ) ,
154
154
} ;
155
155
156
- return Object . assign ( baseDetails , this . _getAxiosErrorDetails ( error as AxiosError ) ) ;
156
+ return Object . assign ( baseDetails , this . getAxiosErrorDetails ( error as AxiosError ) ) ;
157
157
}
158
158
159
- _getAxiosErrorDetails ( error : AxiosError ) {
159
+ private getAxiosErrorDetails ( error : AxiosError ) {
160
160
if ( ! error . isAxiosError ) {
161
161
return { } ;
162
162
}
@@ -166,7 +166,7 @@ export class Logger {
166
166
request_url : error . config . url ,
167
167
response_status : error . response ? error . response . status : undefined ,
168
168
response_status_text : error . response ? error . response . statusText : undefined ,
169
- response_data : error . response ? this . _shortenData ( error . response . data ) : undefined ,
169
+ response_data : error . response ? this . shortenData ( error . response . data ) : undefined ,
170
170
} ;
171
171
}
172
172
}
0 commit comments