Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dynamic log level configuration with environment variable #354

Merged
merged 1 commit into from
Dec 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()],
})


Loading