Skip to content

Overriding log level with an environment variable does not work when using an environment prefix #43304

Closed
@ergonmichelle

Description

@ergonmichelle

Overriding the log level for a specific package with an environment variable does not work as expected when using an environment prefix.

Files to reproduce the problem

Application.kt:

package com.example.springboot

import com.example.springboot.a.A
import com.example.springboot.b.B
import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication

@SpringBootApplication
class Application

fun main(args: Array<String>) {
	val application = SpringApplication(Application::class.java)
	application.environmentPrefix = "prefix"
	application.run(*args)

	A().log()
	B().log()
}

A.kt in package com.example.springboot.a:

package com.example.springboot.a

import org.slf4j.LoggerFactory

class A {
    private val logger = LoggerFactory.getLogger(A::class.java)

    fun log() {
        logger.atInfo().log("Some info log")
        logger.atDebug().log("Some debug log")
    }
}

B.kt in package com.example.springboot.b:

package com.example.springboot.b

import org.slf4j.LoggerFactory

class B {
    private val logger = LoggerFactory.getLogger(B::class.java)

    fun log() {
        logger.atInfo().log("Some info log")
        logger.atDebug().log("Some debug log")
    }
}

application.yaml:

logging:
  level:
    root: INFO

This zip file contains the sample project.

Expected behaviour
If the application is started with the environment variable PREFIX_LOGGING_LEVEL_COM_EXAMPLE_SPRINGBOOT_B=DEBUG the debug statement from class B should be logged.

Actual behaviour
No debug statement is logged.

Further observations

  • If the application is started with two environment variables, one using the prefix and the other not, the debug statement is logged (i.e., started with LOGGING_LEVEL_COM_EXAMPLE_SPRINGBOOT_B=DEBUG;PREFIX_LOGGING_LEVEL_COM_EXAMPLE_SPRINGBOOT_B=DEBUG).
    However, if started with only one of the two environment variables, no debug statement is logged.
  • The log level can be adjusted as execpted by setting the environment variable LOGGING_LEVEL_COM_EXAMPLE_SPRINGBOOT_B=DEBUG if no environment prefix is set in Application.kt.
  • The debug level can be adjusted as expected in the application.yaml file:
logging:
  level:
    root: INFO
    com.example.springboot.b: DEBUG

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions