Skip to content

Commit

Permalink
Enhance Logs with Color-Coding and Improved Readability (#2228)
Browse files Browse the repository at this point in the history
* Enhance log readability by adding color-coding and formatting

* Remove incorrect logLevel value from env sample
  • Loading branch information
PooyaRaki authored Jan 3, 2025
1 parent 0997223 commit a6226af
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@
# Log level for the service.
# LOG_LEVEL=

# Make logs prrety and colorize
# LOG_PRETTY_COLORIZE=

# Targeted Messaging
# TARGETED_MESSAGING_FILE_STORAGE_TYPE=

Expand Down
1 change: 1 addition & 0 deletions src/config/entities/__tests__/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ export default (): ReturnType<typeof configuration> => ({
log: {
level: 'debug',
silent: process.env.LOG_SILENT?.toLowerCase() === 'true',
prettyColorize: process.env.LOG_PRETTY_COLORIZE?.toLowerCase() === 'true',
},
mappings: {
imitation: {
Expand Down
1 change: 1 addition & 0 deletions src/config/entities/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ export default () => ({
log: {
level: process.env.LOG_LEVEL || 'debug',
silent: process.env.LOG_SILENT?.toLowerCase() === 'true',
prettyColorize: process.env.LOG_PRETTY_COLORIZE?.toLowerCase() === 'true',
},
owners: {
// There is no hook to invalidate the owners, so defaulting 0 disables the cache
Expand Down
6 changes: 5 additions & 1 deletion src/logging/logging.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,13 @@ const LoggerTransports = Symbol('LoggerTransports');
function winstonTransportsFactory(
configurationService: IConfigurationService,
): Array<Transport> | Transport {
const prettyColorize =
configurationService.getOrThrow<boolean>('log.prettyColorize');
return new winston.transports.Console({
level: configurationService.getOrThrow<string>('log.level'),
format: winston.format.json(),
format: prettyColorize
? winston.format.prettyPrint({ colorize: true })
: winston.format.json(),
});
}

Expand Down

0 comments on commit a6226af

Please sign in to comment.