i have a java application. I export my project as a .war file, build a docker container and run it.
In my application i define my variable:
private static final Logger logger = Logger.getLogger(BusController.class.getName());
And for the output i use for example:
logger.warning("User "+XYZ+" not found!");
Then i have created a logback.xml, that the logs will be saved at my HDD with a timestemp. I found the solution for this also here.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<logger name="org.springframework.web" level="INFO"/>
<timestamp key="Timestamp" timeReference="contextBirth" datePattern="yyyy-MM-dd'_'HH-mm-ss"/>
<!-- Send debug messages to System.out -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- By default, encoders are assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder -->
<encoder>
<pattern>%d{HH:mm:ss.SSS} - %msg%n</pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>/opt/docker-busapi/data/logs/busicroservice-${myTimestamp}.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%d{yyyy-MM-dd_HH:mm:ss} - %msg%n</Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<FileNamePattern>busmicroservice.%i{yyyy-MM-dd_HH:mm:ss.SSS}}.log</FileNamePattern>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>30MB</MaxFileSize>
</triggeringPolicy>
</appender>
<logger name="mypackage" level="INFO" additivity="false">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
</logger>
That works, all my logs will be saved in my windows environment under "/opt/docker-busapi/data/logs" (i created the folder structure)
I also could mount my extra volume now via the -v parameter in my dockerfile:
REGISTRY=xxxxx.net
VERSION=latest
IMAGE=busMicroservice
SERVICE=busmicroservice
LOCAL_DATA_PATH=/opt/docker-busapi/data
docker run -p xxxx:xxxxx -d -v $LOCAL_DATA_PATH:/logs --name $SERVICE --hostname $SERVICE $REGISTRY/$IMAGE:$VERSION
Because if i check my docker container, the volume is mounted. Via "docker inspect busmicroservice" i get:
"Mounts": [
{
"Type": "bind",
"Source": "/opt/docker-busapi/data",
"Destination": "/logs",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
}
But my logs won't be saved in my logs folder. I guess there is still something wrong with the path. Which Path do i have to put in my "docker run" command?
Looks like you're not consistent on your file path names, in some places you have this defined:
While in others, you have:
And then in the Docker container, you're saying that this should be used in the container:
If you want all your files to go to the host directory of
/opt/docker-busapi/data/logs/
then, first be sure that directory exists and you have the correct permissions set on it, and then ensure that your application uses/logs
as it's directory to save in.Then when you run:
Then you should be good to go.