I have a problem about generating log info when I work in Docker. There is no issue to write logs in log file in localhost.
I cannot see any new logs when I implement the CRUD process during docker.
How can I connect log file(Springboot-Elk.log) to Docker?
How can I fix it?
CodePudding user response:
When working with docker, its good to write all logs to the console. This will allow it to expose logs when run in Kubernetes or other ochestrators. On spring framework, you can achieve this by changing to ConsoleAppender. The example bellow shows how to achieve this in log4j.xml. Place the file to your resources folder and add the log4j dependencies(Ref: https://www.baeldung.com/spring-boot-logging ):
<configuration>
<appender name="consoleAppender" >
<encoder >
<timeZone>UTC</timeZone>
</encoder>
</appender>
<logger name="com.yourcompany.packagename" level="INFO">
<appender-ref ref="consoleAppender" />
</logger>
<root level="ERROR">
<appender-ref ref="consoleAppender" />
</root>
</configuration>
You can still configure log to disk by adding another appender in the configuration above, but you need to add a mount point to your docker-compose file to point to the logs directory on your application.
It is good to note that Docker is stateless and therefore logs are lost when you restart the container.
CodePudding user response:
For me it works. But my ELK Stack is running not in docker.
This is my logstash config (same config for TCP and UDP):
input {
tcp {
port => 5144
codec => "json"
type => "logback"
}
udp {
port => 5144
codec => "json"
type => "logback"
}
}
output {
if [type]=="logback" {
elasticsearch {
hosts => ["localhost:9200"]
index => "logback-%{ YYYY.MM.dd}"
}
}
}
And you have to setup the logback index in Kibana too.
This is my logback-spring.xml:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<springProperty name="mvnVersion" source="info.app.version"/>
<springProperty name="appName" source="info.app.name"/>
<springProperty name="tcpLogHost" source="logstash.tcpHost"/>
<springProperty name="udpLogHost" source="logstash.udpHost"/>
<springProperty name="udpLogPort" source="logstash.udpPort"/>
<appender name="stashUdp" >
<host>${udpLogHost}</host>
<port>${udpLogPort}</port>
<layout >
<customFields>{"appName":"${appName}","env":"${env}","mvnVersion":"${mvnVersion}"}</customFields>
</layout>
</appender>
<appender name="stdout" >
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{100} - %msg%n</pattern>
</encoder>
</appender>
<root level="debug">
<appender-ref ref="stashUdp" />
<appender-ref ref="stdout" />
</root>
</configuration>
I'm logging directly (UDP) to ELK Stack.
And you need
<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
<version>${logstash.logback.version}</version>
</dependency>