|
1 | 1 | import { Injectable, LoggerService } from '@nestjs/common';
|
| 2 | +import 'moment-timezone'; |
2 | 3 | import * as moment from 'moment';
|
| 4 | +import { existsSync, mkdirSync, appendFileSync } from 'fs'; |
3 | 5 |
|
4 | 6 | @Injectable()
|
5 | 7 | export class NestResponseLoggerService implements LoggerService {
|
6 |
| - private now: string; |
| 8 | + private timezone: string; |
| 9 | + private readonly logFilePath: string; |
| 10 | + private readonly logFileEnabled: string; |
| 11 | + |
7 | 12 | constructor() {
|
8 |
| - this.now = moment(new Date()).format('DD/MM/YYYY HH:mm:ss'); |
| 13 | + this.timezone = process.env.TZ || 'UTC'; |
| 14 | + this.logFilePath = process.env.LOG_FILE_PATH || './logs/app.log'; |
| 15 | + this.logFileEnabled = process.env.LOG_FILE_ENABLED || 'false'; |
| 16 | + this.ensureLogDirectoryExists(); |
9 | 17 | }
|
10 | 18 |
|
11 | 19 | log(message: any, ...optionalParams: any[]) {
|
12 |
| - console.log(`[${this.now}] [LOG] ${message}`, optionalParams); |
| 20 | + const now = this.getCurrentTime(); |
| 21 | + console.log(`[${now}] [LOG] ${message}`, optionalParams); |
| 22 | + this.writeToFile(`[${now}] [LOG] ${message} ${optionalParams}`); |
13 | 23 | }
|
14 | 24 |
|
15 | 25 | error(message: any, ...optionalParams: any[]) {
|
16 |
| - console.error(`[${this.now}] [ERROR] ${message}`, optionalParams); |
| 26 | + const now = this.getCurrentTime(); |
| 27 | + console.error(`[${now}] [ERROR] ${message}`, optionalParams); |
| 28 | + this.writeToFile(`[${now}] [LOG] ${message} ${optionalParams}`); |
17 | 29 | }
|
18 | 30 |
|
19 | 31 | warn(message: any, ...optionalParams: any[]) {
|
20 |
| - console.warn(`[${this.now}] [WARN] ${message}`, optionalParams); |
| 32 | + const now = this.getCurrentTime(); |
| 33 | + console.warn(`[${now}] [WARN] ${message}`, optionalParams); |
| 34 | + this.writeToFile(`[${now}] [LOG] ${message} ${optionalParams}`); |
21 | 35 | }
|
22 | 36 |
|
23 | 37 | debug?(message: any, ...optionalParams: any[]) {
|
24 |
| - console.debug(`[${this.now}] [DEBUG] ${message}`, optionalParams); |
| 38 | + const now = this.getCurrentTime(); |
| 39 | + console.debug(`[${now}] [DEBUG] ${message}`, optionalParams); |
| 40 | + this.writeToFile(`[${now}] [LOG] ${message} ${optionalParams}`); |
25 | 41 | }
|
26 | 42 |
|
27 | 43 | verbose?(message: any, ...optionalParams: any[]) {
|
28 |
| - console.info(`[${this.now}] [VERBOSE] ${message}`, optionalParams); |
| 44 | + const now = this.getCurrentTime(); |
| 45 | + console.info(`[${now}] [VERBOSE] ${message}`, optionalParams); |
| 46 | + this.writeToFile(`[${now}] [LOG] ${message} ${optionalParams}`); |
| 47 | + } |
| 48 | + |
| 49 | + private getCurrentTime(): string { |
| 50 | + return moment().tz(this.timezone).format('DD/MM/YYYY HH:mm:ss'); |
| 51 | + } |
| 52 | + |
| 53 | + private ensureLogDirectoryExists(): void { |
| 54 | + const logDirectory = this.logFilePath.substring( |
| 55 | + 0, |
| 56 | + this.logFilePath.lastIndexOf('/'), |
| 57 | + ); |
| 58 | + if (!existsSync(logDirectory)) { |
| 59 | + mkdirSync(logDirectory, { recursive: true }); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + private writeToFile(log: string): void { |
| 64 | + if (this.logFileEnabled === 'true') { |
| 65 | + appendFileSync(this.logFilePath, log + '\n', 'utf8'); |
| 66 | + } |
29 | 67 | }
|
30 | 68 | }
|
0 commit comments