Set Logging Level in Spring Boot via Environment Variable

后端 未结 10 1014
刺人心
刺人心 2020-12-23 08:57

Should it be possible to set logging levels through environment variables alone in a Spring Boot application?

I don\'t want to use application.properties

相关标签:
10条回答
  • 2020-12-23 09:37

    Starting with Spring Boot 2.0.x this works again. Tested with Spring Boot v2.0.9.RELEASE. E.g. enable connection pool debug log:

    LOGGING_LEVEL_COM_ZAXXER=DEBUG java -jar myApp.jar
    

    or Spring framework debug log:

    LOGGING_LEVEL_ORG_SPRINGFRAMEWORK=DEBUG java -jar myApp.jar
    

    or both:

    LOGGING_LEVEL_ORG_SPRINGFRAMEWORK=DEBUG LOGGING_LEVEL_COM_ZAXXER=DEBUG java -jar myApp.jar
    

    See "Application Poperties" in Spring Boot Reference Documentation for more application properties.

    0 讨论(0)
  • 2020-12-23 09:39

    Yes, you can control logging level using environment variable. Here is how I have implemented for my Spring Boot application, deployed on Cloud Foundry platform.

    In you log configuration file provide placeholder for logging level to read value from environment variable. Default is INFO.

        <logger name="com.mycompany.apps.cf" level="${APP_LOGGING_LEVEL:-INFO}">
          <appender-ref ref="CONSOLE"/>
        </logger>
    

    And then, in CF deployment manifest file provide environment variable.

        applications:
        - name: my-app-name
          memory: 2048
          env:
            APP_LOGGING_LEVEL: DEBUG
    

    I hope this will help.

    0 讨论(0)
  • 2020-12-23 09:39

    In spring-boot 2.0.0, adding --trace works. For instance java -jar myapp.jar --debug or java -jar myapp.jar --trace

    https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-logging.html#boot-features-logging-console-output

    0 讨论(0)
  • 2020-12-23 09:42

    Here's an example using Logback with Janino to conditionally include different logging configs via properties or environmental variables... The base config, logback.xml is using conditionals for development console logging or production file logging... just drop the following files in /resources/


    logback.xml


    <?xml version="1.0" encoding="UTF-8"?>
    <configuration scan="true">
        <if condition='property("spring.profiles.active").contains("dev")'>
            <then>
                <include resource="org/springframework/boot/logging/logback/base.xml"/>
                <include resource="dev.xml" optional="true"/>
            </then>
        </if>
        <if condition='property("spring.profiles.active").contains("pro")'>
            <then>
                <include resource="org/springframework/boot/logging/logback/base.xml"/>
                <include resource="pro.xml" optional="true"/>
            </then>
        </if>
    </configuration>
    

    dev.xml

    <included>
        <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
            <encoder>
                <charset>utf-8</charset>
                <Pattern>%-30([%p] [%c:%L]) » %m%n%rEx</Pattern>
            </encoder>
        </appender>
    
        <!-- CHATTY LOGGERS HERE.-->
        <logger name="org.springframework" level="DEBUG"/>
    
        <contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator">
            <resetJUL>true</resetJUL>
        </contextListener>
    
        <root level="${logback.loglevel}">
            <appender-ref ref="CONSOLE"/>
        </root>
    
    </included>
    

    pro.xml

    <included>
        <conversionRule conversionWord="wex"
                        converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter"/>
        <property name="FILE_LOG_PATTERN"
                  value="%d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${PID:- } --- [%t] %-40.40logger{39} : %m%n%wex"/>
        <property name="FILE_NAME_PATTERN" value="./logs/%d{yyyy-MM-dd}-exec.log"/>
    
        <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <fileNamePattern>FILE_NAME_PATTERN</fileNamePattern>
                <maxHistory>7</maxHistory>
            </rollingPolicy>
            <encoder>
                <pattern>${FILE_LOG_PATTERN}</pattern>
            </encoder>
        </appender>
    
        <appender name="ASYNC" class="ch.qos.logback.classic.AsyncAppender">
            <queueSize>512</queueSize>
            <appender-ref ref="FILE"/>
        </appender>
    
        <!-- APP SPECIFIC LOGGERS HERE.-->
        <logger name="org.springframework.boot.SpringApplication" level="INFO"/>
    
        <root level="INFO">
            <appender-ref ref="FILE"/>
        </root>
    
    </included>
    
    0 讨论(0)
  • 2020-12-23 09:53

    This is just an idea, but did you try setting

    _JAVA_OPTIONS=-Dlogging.level.org.springframework=TRACE?

    Theoretically, this way -Dlogging.level.org.springframework=TRACE will be passed as default JVM argument and should affect every JVM instance in your environment.

    0 讨论(0)
  • 2020-12-23 09:54

    Setting log levels via environment variables can only be done for Packages but not for Classes

    I had the same problem as the OP. And I was wondering why some of the users here reported that the proposed solutions worked well while others responded they didn't.
    I'm on Spring Boot 2.1 and the problem obviously changed a bit over the last years, but the current situation is as follows:

    TL;DR

    Setting the log level for a package works:

    LOGGING_LEVEL_COM_ACME_PACKAGE=DEBUG
    

    While setting the log level for a specific class has no effect:

    LOGGING_LEVEL_COM_ACME_PACKAGE_CLASS=DEBUG
    

    How can that be?

    Have a look at Spring Boot's LoggingApplicationListener.
    If you'd debug it and set a breakpoint in the highlighted code block, you'd see that the log level definition for a class com.acme.mypackage.MyClass becomes com.acme.mypackage.myclass.
    So a log level definition for a class looks exactly like a log level definition for a package.

    This is related to Spring's Relaxed Binding, which proposes an upper case notation for environment variables. Thus the typical camel case notation of a class is not available for the LoggingApplicationListener. On the other hand, I think one can't check whether the given fully-qualified path matches a class. The classloader won't find anything as long as the notation doesn't match exactly.

    Thus log definitions in environment variables don't work for classes but only for packages.

    0 讨论(0)
提交回复
热议问题