Skip to content

Commit 0e1164c

Browse files
committed
refactor(logger): move everything to the logger class and document methods
1 parent cd975cd commit 0e1164c

File tree

1 file changed

+86
-39
lines changed

1 file changed

+86
-39
lines changed

pkg/api/src/loaders/logger.ts

+86-39
Original file line numberDiff line numberDiff line change
@@ -6,90 +6,137 @@ import PinoPretty from 'pino-pretty';
66

77
import pathsConfig from '@/config/env/paths';
88

9-
const logsFolder = path.join(pathsConfig.dataDir, './logs');
10-
11-
function createLogger(level: string): PinoLogger<string> {
12-
return pino(
13-
{
14-
level,
15-
},
16-
pino.multistream([
17-
{
18-
level,
19-
stream: PinoPretty({
20-
translateTime: 'SYS:standard',
21-
ignore: 'pid,hostname',
22-
singleLine: true,
23-
sync: true,
24-
}),
25-
},
26-
{
27-
level,
28-
stream: FileStreamRotator.getStream({
29-
filename: path.join(logsFolder, 'petio-%DATE%.log'),
30-
frequency: 'daily',
31-
verbose: false,
32-
create_symlink: true,
33-
audit_file: path.join(logsFolder, 'audit.json'),
34-
date_format: 'YYYY-MM-DD',
35-
max_logs: '7d',
36-
}),
37-
},
38-
]),
39-
);
40-
}
41-
9+
/**
10+
* The Logger class provides logging functionality for the application.
11+
*/
4212
export class Logger {
4313
private static instance: Logger;
4414

4515
private logger: PinoLogger;
4616

47-
private children: pino.Logger<string>[] = [];
48-
4917
private constructor() {
5018
const level = process.env.LOG_LEVEL || 'info';
51-
this.logger = createLogger(
19+
this.logger = this.createLogger(
5220
process.env.NODE_ENV === 'development' ? 'debug' : level,
5321
);
5422
}
5523

24+
/**
25+
* Creates a new instance of the logger with the specified log level.
26+
* @param level - The log level to use.
27+
* @returns The created logger instance.
28+
*/
29+
private createLogger(level: string): PinoLogger<string> {
30+
const logsFolder = path.join(pathsConfig.dataDir, './logs');
31+
return pino(
32+
{
33+
level,
34+
},
35+
pino.multistream([
36+
{
37+
level,
38+
stream: PinoPretty({
39+
translateTime: 'SYS:standard',
40+
ignore: 'pid,hostname',
41+
singleLine: true,
42+
sync: true,
43+
}),
44+
},
45+
{
46+
level,
47+
stream: FileStreamRotator.getStream({
48+
filename: path.join(logsFolder, 'petio-%DATE%.log'),
49+
frequency: 'daily',
50+
verbose: false,
51+
create_symlink: true,
52+
audit_file: path.join(logsFolder, 'audit.json'),
53+
date_format: 'YYYY-MM-DD',
54+
max_logs: '7d',
55+
}),
56+
},
57+
]),
58+
);
59+
}
60+
61+
/**
62+
* Gets the singleton instance of the Logger class.
63+
* @returns The Logger instance.
64+
*/
5665
public static getInstance(): Logger {
5766
if (!Logger.instance) {
5867
Logger.instance = new Logger();
5968
}
6069
return Logger.instance;
6170
}
6271

72+
/**
73+
* Logs an info message.
74+
* @param message - The message to log.
75+
* @param obj - Additional data to log.
76+
*/
6377
public info(message: string, obj = {}) {
6478
this.logger.info(obj, message);
6579
}
6680

81+
/**
82+
* Logs a warning message.
83+
* @param message - The message to log.
84+
* @param obj - Additional data to log.
85+
*/
6786
public warn(message: string, obj = {}) {
6887
this.logger.warn(obj, message);
6988
}
7089

90+
/**
91+
* Logs an error message.
92+
* @param message - The message to log.
93+
* @param obj - Additional data to log.
94+
*/
7195
public error(message: string, obj = {}) {
7296
this.logger.error(obj, message);
7397
}
7498

99+
/**
100+
* Logs a debug message.
101+
* @param message - The message to log.
102+
* @param obj - Additional data to log.
103+
*/
75104
public debug(message: string, obj = {}) {
76105
this.logger.debug(obj, message);
77106
}
78107

108+
/**
109+
* Logs a trace message.
110+
* @param message - The message to log.
111+
* @param obj - Additional data to log.
112+
*/
79113
public trace(message: string, obj = {}) {
80114
this.logger.trace(obj, message);
81115
}
82116

117+
/**
118+
* Logs a message with the specified log level.
119+
* @param level - The log level to use.
120+
* @param message - The message to log.
121+
* @param obj - Additional data to log.
122+
*/
83123
public log(level: string, message: string, obj = {}) {
84124
this.logger[level](obj, message);
85125
}
86126

127+
/**
128+
* Creates a child logger with additional properties.
129+
* @param obj - The additional properties for the child logger.
130+
* @returns The child logger instance.
131+
*/
87132
public child(obj: object) {
88-
const child = this.logger.child<string>(obj);
89-
this.children.push(child);
90-
return child;
133+
return this.logger.child<string>(obj);
91134
}
92135

136+
/**
137+
* Gets the underlying logger instance.
138+
* @returns The underlying logger instance.
139+
*/
93140
public core() {
94141
return this.logger;
95142
}

0 commit comments

Comments
 (0)