Skip to content

Commit

Permalink
Add dynamic log level configuration with environment variable (#354)
Browse files Browse the repository at this point in the history
  • Loading branch information
ludeknovy authored Dec 11, 2024
1 parent 5cd23f3 commit d60d8b2
Showing 1 changed file with 28 additions and 6 deletions.
34 changes: 28 additions & 6 deletions src/logger.ts
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()],
})


0 comments on commit d60d8b2

Please sign in to comment.