How do I set a logging level in a Kitura project generated using the project template?

88 Views Asked by At

I want to set the logging level of my Kitura project. The project is created with the kitura init command, which generates the project using a template. In the template, there is an initializeLogging method that sets the logging level of HeliumLogger:

func initializeLogging() {
    let manager = ConfigurationManager()
    manager.load(.environmentVariables)

    let logLevel: LoggerMessageType
    switch (manager["LOG_LEVEL"] as? String)?.lowercased() {
    case "error":
        logLevel = .error
    case "warning":
        logLevel = .warning
    case "info":
        logLevel = .info
    case "verbose":
        logLevel = .verbose
    case "debug":
        logLevel = .debug
    case "exit":
        logLevel = .exit
    case "entry":
        logLevel = .entry
    case "none", "false", "off", "disabled":
        return
    default:
        logLevel = .info
    }
    HeliumLogger.use(logLevel)
}

It seems to get an environment variable called "LOG_LEVEL". Upon further inspection of what manager.load does, I worked out that it accesses ProcessInfo.processInfo.environment, which I know I can set in the "Edit Schemes" screen in Xcode:

enter image description here

However, this only works if I run the app with Xcode. If I use swift build, then run the executable in the console. The logging level is back to "info". Clearly, setting it in "Edit Schemes" is not how I am supposed to set it...

What is the intended approach of setting the logging level?


I know I can just delete everything and write HeliumLogger.use(.verbose), but I would like some late binding :) And I feel like the code in the code in the template is there for a reason...

0

There are 0 best solutions below