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

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

Closed
ergonmichelle opened this issue Nov 27, 2024 · 1 comment
Labels
type: bug A general bug
Milestone

Comments

@ergonmichelle
Copy link

ergonmichelle commented Nov 27, 2024

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
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Nov 27, 2024
@wilkinsona wilkinsona changed the title Overriding log level with an environment variable broken when using an environment prefix Overriding log level with an environment variable does not work when using an environment prefix Nov 27, 2024
@wilkinsona wilkinsona added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels Nov 27, 2024
@wilkinsona wilkinsona added this to the 3.3.x milestone Nov 27, 2024
@wilkinsona wilkinsona modified the milestones: 3.3.x, 3.3.7 Nov 27, 2024
@ergonmichelle
Copy link
Author

Thank you for the quick fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

No branches or pull requests

3 participants