Skip to content

Commit cac152e

Browse files
committed
Update logging contracts and introduce configuration
Signed-off-by: Shubham Sharma <[email protected]>
1 parent f2e4210 commit cac152e

File tree

6 files changed

+117
-48
lines changed

6 files changed

+117
-48
lines changed

src/logger/Logger.ts

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
}

src/logger/LoggerExtensions.ts

Lines changed: 0 additions & 47 deletions
This file was deleted.

src/types/logger/LogLevel.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
/**
15+
* LogLevel sets the level of logging that should be used.
16+
*/
17+
export enum LogLevel {
18+
error = 0,
19+
warn = 1,
20+
info = 2,
21+
verbose = 3,
22+
debug = 4,
23+
}

src/types/logger/LoggerOptions.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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 { LogLevel } from "./LogLevel"
15+
16+
/**
17+
* LoggerOptions provides options for configuring the logger.
18+
*/
19+
export type LoggerOptions = {
20+
logLevel?: LogLevel
21+
logService?: LoggerService
22+
}
File renamed without changes.

src/utils/Logger.util.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const CURRENT_LOGGER_LEVEL = process.env.LOGGER_LEVEL || LoggerLevel.VERBOSE;
3131

3232
console.log(`CURRENT LOG LEVEL: ${CURRENT_LOGGER_LEVEL}`)
3333

34-
export class Logger {
34+
export class LoggerOld {
3535
static print(level: LoggerLevel, message: string, category = 'Server') {
3636
if (level > CURRENT_LOGGER_LEVEL) {
3737
return;

0 commit comments

Comments
 (0)