1
+ /*
2
+ Copyright 2022 The Dapr Authors
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+ http://www.apache.org/licenses/LICENSE-2.0
7
+ Unless required by applicable law or agreed to in writing, software
8
+ distributed under the License is distributed on an "AS IS" BASIS,
9
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ See the License for the specific language governing permissions and
11
+ limitations under the License.
12
+ */
13
+
14
+ import { LoggerOptions } from "../types/logger/LoggerOptions" ;
15
+ import { LogLevel } from "../types/logger/LogLevel" ;
16
+
17
+ export class Logger {
18
+ private readonly logLevel : LogLevel ;
19
+ private readonly logService : LoggerService ;
20
+
21
+ /**
22
+ * Creates a new instance of the Logger class.
23
+ * If an options is missing, it falls back to the default values.
24
+ * The default log level is 'info'.
25
+ * The default log service is the ConsoleLogger.
26
+ * @param options Logger options
27
+ */
28
+ constructor ( options ?: LoggerOptions ) {
29
+ if ( options && options . logLevel ) {
30
+ this . logLevel = options . logLevel ;
31
+ } else {
32
+ this . logLevel = LogLevel . info ;
33
+ }
34
+
35
+ if ( options && options . logService ) {
36
+ this . logService = options . logService ;
37
+ } else {
38
+ this . logService = new ConsoleLogger ( ) ;
39
+ }
40
+ }
41
+
42
+ error ( component : string , area : string , message : any , ...optionalParams : any [ ] ) : void {
43
+ if ( this . logLevel >= LogLevel . error ) {
44
+ this . logService . error ( `[${ component } ] ${ area } : ${ message } ` , ...optionalParams ) ;
45
+ }
46
+ }
47
+
48
+ warn ( component : string , area : string , message : any , ...optionalParams : any [ ] ) : void {
49
+ if ( this . logLevel >= LogLevel . warn ) {
50
+ this . logService . warn ( `[${ component } ] ${ area } : ${ message } ` , ...optionalParams ) ;
51
+ }
52
+ }
53
+
54
+ info ( component : string , area : string , message : any , ...optionalParams : any [ ] ) : void {
55
+ if ( this . logLevel >= LogLevel . info ) {
56
+ this . logService . info ( `[${ component } ] ${ area } : ${ message } ` , ...optionalParams ) ;
57
+ }
58
+ }
59
+
60
+ verbose ( component : string , area : string , message : any , ...optionalParams : any [ ] ) : void {
61
+ if ( this . logLevel >= LogLevel . verbose ) {
62
+ this . logService . verbose ( `[${ component } ] ${ area } : ${ message } ` , ...optionalParams ) ;
63
+ }
64
+ }
65
+
66
+ debug ( component : string , area : string , message : any , ...optionalParams : any [ ] ) : void {
67
+ if ( this . logLevel >= LogLevel . debug ) {
68
+ this . logService . debug ( `[${ component } ] ${ area } : ${ message } ` , ...optionalParams ) ;
69
+ }
70
+ }
71
+ }
0 commit comments