简体   繁体   中英

Spring boot logging problem (with logback)

I write spring boot app that logging to file using logback.

My logback-spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <property name="LOG_DIR" value="logs"/>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>
                %d{dd-MM-yyyy HH:mm:ss.SSS} %magenta([%thread]) %highlight(%-5level) %logger.%M - %msg%n
            </pattern>
        </encoder>
    </appender>
    <appender name="SAVE-TO-FILE"
              class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOG_PATH}/log.log</file>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <Pattern>
                %d{dd-MM-yyyy HH:mm:ss.SSS} [%thread] %-5level %logger{36}.%M - %msg%n
            </Pattern>
        </encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <!-- rollover daily & on size-->
            <fileNamePattern>
                ${LOG_PATH}/archived/log_%d{dd-MM-yyyy}_%i.log
            </fileNamePattern>
            <maxFileSize>200MB</maxFileSize>
            <maxHistory>10</maxHistory>
            <totalSizeCap>2GB</totalSizeCap>
        </rollingPolicy>
    </appender>
    <springProfile name="dev">
        <root level="INFO">
            <appender-ref ref="STDOUT"/>
            <appender-ref ref="SAVE-TO-FILE"/>
        </root>
        <logger name="com.elektrosoft.centralServer.telekomCentralServer" additivity="false" level="DEBUG">
            <appender-ref ref="STDOUT"/>
            <appender-ref ref="SAVE-TO-FILE"/>
        </logger>
    </springProfile>
    <springProfile name="prod">
        <root level="INFO">
            <appender-ref ref="SAVE-TO-FILE"/>
        </root>
        <logger name="com.elektrosoft.centralServer.telekomCentralServer" additivity="false" level="DEBUG">
            <appender-ref ref="SAVE-TO-FILE"/>
        </logger>
    </springProfile>
</configuration>

My application.properties

logging.level.root=info
logging.level.com.elektrosoft.centralServer.telekomCentralServer=debug
logging.level.org.hibernate.SQL=error
logging.file.path=/var/logs/spring-app-logs/
logging.file.name=log.log
logging.pattern.file=%d{dd-MM-yyyy HH:mm:ss.SSS} [%thread] %-5level %logger{36}.%M - %msg%n
logging.pattern.console=%d{dd-MM-yyyy HH:mm:ss.SSS} %magenta([%thread]) %highlight(%-5level) %logger.%M - %msg%n

This is working ok when I run app from intellij IDE and log is write to file in the location. I deploy this app to the aws beanstalk and all is working. The problem is when I run the app from terminal (windows or linux) app is not starting and in the folder where is the app is created folder LOG_PATH_IS_UNDEFINED.

I have define logging.file.path in the application.properties and I rename logback file to logback-spring and from the doc when logging.file.path is define LOG_PATH is define by spring.

I don't understand why this is working if I run app from IDE and in AWS but not working if I run from terminal with java -jar springAPP.jar.

Why is this happen? How to solve this problem?

Please help!

Thanks for all your help.

I am guessing that you forgot to export LOG_PATH variable before you run the jar file. You can create a script file for that. Like start.sh

#!/bin/bash
export LOG_PATH=/var/logs/spring-app-logs/
java -jar springAPP.jar

and run ./start.sh

You did not define LOG_PATH in your logback-spring.xml

Add the following line:

    <property name="LOG_PATH" value="./logsFolder"/>

after the following line

    <property name="LOG_DIR" value="logs"/>

OR

Just changed LOG_DIR into LOG_PATH

to make:

    <property name="LOG_PATH" value="./logsFolder"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM