Logback as logger implementation
Native microservice
At first change dependencies for your microservice implementation from:
<dependency>
<groupId>com.jlupin</groupId>
<artifactId>jlupin-platform-native</artifactId>
</dependency>
to:
<dependency>
<groupId>com.jlupin</groupId>
<artifactId>jlupin-platform-native</artifactId>
<exclusions>
<exclusion>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-jcl</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
</exclusion>
</exclusions>
</dependency>
And then add proper dependencies to your microservice implementation.
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>${logback.version}</version>
</dependency>
To enable external file support for logger add this option to JVM parameters (replace [microservice-name]
with name of your microservice):
[..]
PROPERTIES:
jvmOptions1: '[...] -Dlogback.configurationFile=../application/[microservice-name]/logback.xml'
jvmOptions2: '[...] -Dlogback.configurationFile=../application/[microservice-name]/logback.xml'
[...]
Recommended logback.xml
file to put in microservice directory or additional-files (replace [microservice-name]
with name of your microservice):
<configuration scan="true">
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d %-5p [%c] \(%t\) %m%n</pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${microservice.dir}/../../logs/microservice/[microservice-name]/microservice.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${microservice.dir}/../../logs/microservice/[microservice-name]/microservice.%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%d %-5p [%c] \(%t\) %m%n</pattern>
</encoder>
</appender>
<!-- <logger name="org.springframework" level="DEBUG"/> -->
<root level="INFO">
<appender-ref ref="FILE"/>
</root>
</configuration>
From now you can log as follows:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
final Logger logger = LoggerFactory.getLogger(SomeService.class);
logger.info("some info from native");
Servlet microservice
There is no need to add any dependencies. By default Spring BOOT is using Logback for logging. If you are using our recommended configuration you should remove starter to log4j and remove exclusion of default logger.
JLupin by itself is internally using Log4j2 as a logging provider. Because of this it needs to be configured by user even when Logback is used as your application's logger through SLF4J. You must create log4j2.xml
file for JLupin logs and logback.xml
for your logs.
To enable external file support for logger add this option to JVM parameters:
[..]
PROPERTIES:
jvmOptions1: '[...] -Dlog4j.configurationFile=${sys:microservice.dir}/log4j2.xml -Dlogging.config=${microservice.dir}/logback.xml'
jvmOptions2: '[...] -Dlog4j.configurationFile=${sys:microservice.dir}/log4j2.xml -Dlogging.config=${microservice.dir}/logback.xml'
[...]
Recommended log4j2.xml
file to put in microservice directory or additional-files (replace [microservice-name]
with name of your microservice):
<?xml version="1.0" encoding="UTF-8"?>
<!-- ===================================================================== -->
<!-- -->
<!-- Log4j2 Configuration -->
<!-- -->
<!-- ===================================================================== -->
<!--
| For more configuration information and examples see the Apache Log4j2
| website: https://logging.apache.org/log4j/2.x/index.html
-->
<Configuration status="WARN" dest="errors/[microservice-name]_log4j2_status.log">
<!-- Extract log directory and file name into variables -->
<Properties>
<Property name="logDirectory">${sys:microservice.dir}/../../logs/microservice/[microservice-name]</Property>
<Property name="logFileName">jlupin</Property>
</Properties>
<Appenders>
<!-- RollingFileAppender configured to role every day -->
<RollingFile name="FILE">
<FileName>${logDirectory}/${logFileName}.log</FileName>
<FilePattern>${logDirectory}/${logFileName}.%d{yyyy-MM-dd}.log</FilePattern>
<!-- Compress log files to gzip -->
<!-- More configuration https://logging.apache.org/log4j/2.x/manual/appenders.html#DefaultRolloverStrategy -->
<!-- <FilePattern>/.%d{yyyy-MM-dd}.log.gz</FilePattern> -->
<!-- Do not truncate file -->
<Append>true</Append>
<!-- The default pattern: Date Priority [Category] (Thread) Message\n -->
<PatternLayout pattern="%d %-5p [%c] (%t) %m%n"/>
<Policies>
<!-- Rollover every microservice start - very useful for debugging -->
<!-- <OnStartupTriggeringPolicy /> -->
<!-- Rollover at the top of each day -->
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
<!-- Rollover if file size is greater than 200 MB -->
<!-- <SizeBasedTriggeringPolicy size="200 MB"/> -->
</Policies>
<CreateOnDemand>true</CreateOnDemand>
<!-- Keep last 10 log files -->
<!-- More configuration https://logging.apache.org/log4j/2.x/manual/appenders.html#DefaultRolloverStrategy -->
<!-- <DefaultRolloverStrategy max="10" /> -->
</RollingFile>
<!-- AsyncAppender for high performance -->
<Async name="ASYNC_FILE">
<BufferSize>1000</BufferSize>
<AppenderRef ref="FILE"/>
</Async>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%d %-5p [%c] (%t) %m%n"/>
</Console>
<Async name="ASYNC_STDOUT">
<BufferSize>1000</BufferSize>
<AppenderRef ref="STDOUT"/>
</Async>
</Appenders>
<Loggers>
<!-- Setup for root logger with AsyncAppender -->
<Root level="info">
<AppenderRef ref="ASYNC_FILE"/>
</Root>
</Loggers>
</Configuration>
Recommended logback.xml
file to put in microservice directory or additional-files (replace [microservice-name]
with name of your microservice):
<configuration scan="true">
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d %-5p [%c] \(%t\) %m%n</pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${microservice.dir}/../../logs/microservice/[microservice-name]/microservice.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${microservice.dir}/../../logs/microservice/[microservice-name]/microservice.%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%d %-5p [%c] \(%t\) %m%n</pattern>
</encoder>
</appender>
<!-- <logger name="org.springframework" level="DEBUG"/> -->
<root level="INFO">
<appender-ref ref="FILE"/>
</root>
</configuration>
From now you can log as follows:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
final Logger logger = LoggerFactory.getLogger(SomeService.class);
logger.info("some info from servlet");