-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(server): setup winston logger (#9561)
There is no impact on the existing logger, as the current logger is used with `new Logger(Context)` and does not utilize dependency injection. In the next phase, gradually replace and supplement the existing `Logger`.
- Loading branch information
1 parent
5e51018
commit 482b534
Showing
10 changed files
with
518 additions
and
16 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Global, Module } from '@nestjs/common'; | ||
|
||
import { ConfigModule } from '../config'; | ||
import { loggerProvider } from './service'; | ||
|
||
@Global() | ||
@Module({ | ||
imports: [ConfigModule], | ||
providers: [loggerProvider], | ||
exports: [loggerProvider], | ||
}) | ||
export class LoggerModule {} | ||
|
||
export { AFFiNELogger } from './logger'; |
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,17 @@ | ||
import { WinstonLogger } from 'nest-winston'; | ||
|
||
export class AFFiNELogger extends WinstonLogger { | ||
override error( | ||
message: any, | ||
trace?: Error | string | unknown, | ||
context?: string | ||
) { | ||
if (trace && trace instanceof Error) { | ||
super.error(message, trace.stack, context); | ||
} else if (typeof trace === 'string' || trace === undefined) { | ||
super.error(message, trace, context); | ||
} else { | ||
super.error(message, undefined, context); | ||
} | ||
} | ||
} |
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,30 @@ | ||
import { LoggingWinston } from '@google-cloud/logging-winston'; | ||
import { ConsoleLogger, LoggerService, Provider, Scope } from '@nestjs/common'; | ||
import { createLogger, transports } from 'winston'; | ||
|
||
import { Config } from '../config'; | ||
import { AFFiNELogger } from './logger'; | ||
|
||
export const loggerProvider: Provider<LoggerService> = { | ||
provide: AFFiNELogger, | ||
useFactory: (config: Config) => { | ||
if (config.NODE_ENV !== 'production') { | ||
return new ConsoleLogger(); | ||
} | ||
const loggingWinston = new LoggingWinston(); | ||
// Create a Winston logger that streams to Cloud Logging | ||
const instance = createLogger({ | ||
level: config.affine.stable ? 'log' : 'verbose', | ||
transports: [ | ||
new transports.Console(), | ||
// Add Cloud Logging | ||
loggingWinston, | ||
], | ||
}); | ||
return new AFFiNELogger(instance); | ||
}, | ||
inject: [Config], | ||
// use transient to make sure the logger is created for each di context | ||
// to make the `setContext` method works as expected | ||
scope: Scope.TRANSIENT, | ||
}; |
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
Oops, something went wrong.