-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Mario Moreno
committed
Aug 27, 2023
1 parent
4955b8a
commit 9ee6b88
Showing
10 changed files
with
101 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
TZ=America/Buenos_Aires | ||
|
||
LOG_FILE_PATH=./logs/app.log | ||
LOG_FILE_ENABLED=false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
TZ=UTC | ||
|
||
LOG_FILE_PATH=./logs/app.log | ||
LOG_FILE_ENABLED=false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 45 additions & 7 deletions
52
libs/nest-response-logger/src/nest-response-logger.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,68 @@ | ||
import { Injectable, LoggerService } from '@nestjs/common'; | ||
import 'moment-timezone'; | ||
import * as moment from 'moment'; | ||
import { existsSync, mkdirSync, appendFileSync } from 'fs'; | ||
|
||
@Injectable() | ||
export class NestResponseLoggerService implements LoggerService { | ||
private now: string; | ||
private timezone: string; | ||
private readonly logFilePath: string; | ||
private readonly logFileEnabled: string; | ||
|
||
constructor() { | ||
this.now = moment(new Date()).format('DD/MM/YYYY HH:mm:ss'); | ||
this.timezone = process.env.TZ || 'UTC'; | ||
this.logFilePath = process.env.LOG_FILE_PATH || './logs/app.log'; | ||
this.logFileEnabled = process.env.LOG_FILE_ENABLED || 'false'; | ||
this.ensureLogDirectoryExists(); | ||
} | ||
|
||
log(message: any, ...optionalParams: any[]) { | ||
console.log(`[${this.now}] [LOG] ${message}`, optionalParams); | ||
const now = this.getCurrentTime(); | ||
console.log(`[${now}] [LOG] ${message}`, optionalParams); | ||
this.writeToFile(`[${now}] [LOG] ${message} ${optionalParams}`); | ||
} | ||
|
||
error(message: any, ...optionalParams: any[]) { | ||
console.error(`[${this.now}] [ERROR] ${message}`, optionalParams); | ||
const now = this.getCurrentTime(); | ||
console.error(`[${now}] [ERROR] ${message}`, optionalParams); | ||
this.writeToFile(`[${now}] [LOG] ${message} ${optionalParams}`); | ||
} | ||
|
||
warn(message: any, ...optionalParams: any[]) { | ||
console.warn(`[${this.now}] [WARN] ${message}`, optionalParams); | ||
const now = this.getCurrentTime(); | ||
console.warn(`[${now}] [WARN] ${message}`, optionalParams); | ||
this.writeToFile(`[${now}] [LOG] ${message} ${optionalParams}`); | ||
} | ||
|
||
debug?(message: any, ...optionalParams: any[]) { | ||
console.debug(`[${this.now}] [DEBUG] ${message}`, optionalParams); | ||
const now = this.getCurrentTime(); | ||
console.debug(`[${now}] [DEBUG] ${message}`, optionalParams); | ||
this.writeToFile(`[${now}] [LOG] ${message} ${optionalParams}`); | ||
} | ||
|
||
verbose?(message: any, ...optionalParams: any[]) { | ||
console.info(`[${this.now}] [VERBOSE] ${message}`, optionalParams); | ||
const now = this.getCurrentTime(); | ||
console.info(`[${now}] [VERBOSE] ${message}`, optionalParams); | ||
this.writeToFile(`[${now}] [LOG] ${message} ${optionalParams}`); | ||
} | ||
|
||
private getCurrentTime(): string { | ||
return moment().tz(this.timezone).format('DD/MM/YYYY HH:mm:ss'); | ||
} | ||
|
||
private ensureLogDirectoryExists(): void { | ||
const logDirectory = this.logFilePath.substring( | ||
0, | ||
this.logFilePath.lastIndexOf('/'), | ||
); | ||
if (!existsSync(logDirectory)) { | ||
mkdirSync(logDirectory, { recursive: true }); | ||
} | ||
} | ||
|
||
private writeToFile(log: string): void { | ||
if (this.logFileEnabled === 'true') { | ||
appendFileSync(this.logFilePath, log + '\n', 'utf8'); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters