Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring boot - Docker logs

I have a spring boot java application running inside a docker container. I configured logback in the application. Where i can access the generated logs? The configured path is:

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

I am currently checking logs using: docker logs containerName --follow But this command return the current logs from the last time the application was started. How can i check the rolling logs or archived ones.

Is there a way to create a volume for logback logs?

like image 909
user666 Avatar asked Nov 09 '18 14:11

user666


2 Answers

I am using the following way: in logback.xml:

<?xml version="1.0" encoding="UTF-8"?>

<property name="LOG_PATH" value="logs"/>
<timestamp key="currentTimestamp" datePattern="yyyy-MM-dd'_'HH"/>

<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\(%line\) - %msg%n
        </pattern>
    </encoder>
</appender>

<appender name="SAVE-TO-FILE"
          class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>${LOG_PATH}/appName.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">
        <!-- the rollover occurs when the active log file reaches the maximum file size before the end of the time period. -->
        <!-- The rollover period is inferred from the smallest date unit value of fileNamePattern -->
        <!-- each file should be at most [maxFileSize], keep [maxHistory] days worth of history, but at most [totalSizeCap] -->
        <maxFileSize>100MB</maxFileSize>
        <fileNamePattern>
            ${LOG_PATH}/archived/appName.%d{yyyy-MM-dd}.%i.log
        </fileNamePattern>
        <maxHistory>30</maxHistory>
        <totalSizeCap>1GB</totalSizeCap>
    </rollingPolicy>
</appender>


<root level="INFO">
    <appender-ref ref="STDOUT"/>
    <appender-ref ref="SAVE-TO-FILE"/>
</root>

In docker run :

docker run --network name-network --restart=unless-stopped --name name-java-container 
--hostname valueOfHostname -d -p 8080:8080 \
-v /data/nameFolderForLogs:/logs \
imageName
like image 90
user666 Avatar answered Oct 09 '22 20:10

user666


Try to implement your file-appender with

<configuration>
    ...
    <property name="LOG_PATH" value=/var/app>
    ...
    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> 
        <file>${LOG_PATH}/spring.log</file>
        ...   
    </appender>
</configuration>

And after this you can run the next command:

docker exec -it containerName cat /var/app/spring.log > $HOME/spring.log

or make volume with (docker run ... -v "$HOME/app:/var/app")

like image 38
Valentyn Riabukhin Avatar answered Oct 09 '22 21:10

Valentyn Riabukhin