-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add dynamic log level configuration with environment variable (#354)
- Loading branch information
Showing
1 changed file
with
28 additions
and
6 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 |
---|---|---|
@@ -1,10 +1,32 @@ | ||
import * as winston from "winston" | ||
|
||
const LOG_LEVELS = winston.config.syslog.levels | ||
const DEFAULT_LOG_LEVEL = "debug" | ||
|
||
const getLogLevel = (): string => { | ||
const logLevelEnvVar = process.env.LOG_LEVEL | ||
if (logLevelEnvVar) { | ||
const isAllowedLogLevel = Object.keys(LOG_LEVELS) | ||
.find(level => logLevelEnvVar.toLowerCase() === level.toLowerCase()) | ||
if (!isAllowedLogLevel) { | ||
console.log("Unsupported log level: ", logLevelEnvVar) | ||
} else { | ||
return isAllowedLogLevel | ||
} | ||
} | ||
return DEFAULT_LOG_LEVEL | ||
|
||
|
||
} | ||
|
||
export const logger = winston.createLogger({ | ||
format: winston.format.combine( | ||
winston.format.timestamp({ format: "YYYY-MM-DD HH:mm:ss" }), | ||
winston.format.json() | ||
), | ||
levels: winston.config.syslog.levels, | ||
transports: [ new winston.transports.Console() ], | ||
format: winston.format.combine( | ||
winston.format.timestamp({ format: "YYYY-MM-DD HH:mm:ss" }), | ||
winston.format.json() | ||
), | ||
level: getLogLevel(), | ||
levels: LOG_LEVELS, | ||
transports: [new winston.transports.Console()], | ||
}) | ||
|
||
|