How to get each log in a separate log file?

There is a lot of EJB in my application. The current custom Logger implementation creates the following log:

private static Logger logger = Logger.getInstance ("SERVICE_NAME");

and the record will go to the file;

(path) /SERVICE_NAME/SERVICE_NAME.log

I want to replicate this behavior with logback, but having real problems grabbing the logger name in the logback.xml configuration. This can be seen in the log encoder.pattern, that is, "% d% -5level% logger {35} -% msg% n".

Any ideas how I can get this in a property / variable and then use it in an element?

+6
source share
2 answers

I have a partial solution. If I create my own Discriminator, I can use the Discriminator in the logback.xml file to implement seperate-log-files-per-EJB.

Discriminator;

public class LoggerNameBasedDiscriminator implements Discriminator<ILoggingEvent> { private static final String KEY = "loggerName"; private boolean started; @Override public String getDiscriminatingValue(ILoggingEvent iLoggingEvent) { return iLoggingEvent.getLoggerName(); } @Override public String getKey() { return KEY; } public void start() { started = true; } public void stop() { started = false; } public boolean isStarted() { return started; } 

}

Then my logback.xml;

  <configuration debug="true" scan="true" scanPeriod="30 seconds"> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg</pattern> </encoder> </appender> <appender name="SIFT" class="ch.qos.logback.classic.sift.SiftingAppender"> <discriminator class="package.to.LoggerNameBasedDiscriminator"/> <sift> <appender name="FILE-${loggerName}" class="ch.qos.logback.core.FileAppender"> <FILE>path/to/logs/${loggerName}/${loggerName}.log</FILE> <encoder> <pattern>%d{HH:mm:ss.SSS} %-50(%level %logger{35}) %msg%n</pattern> </encoder> </appender> </sift> </appender> <root level="debug"> <appender-ref ref="STDOUT" /> <appender-ref ref="SIFT" /> </root> </configuration> 

This solution seems to work, but now I don’t have time to rotate the log in time or size!

+6
source

Thanks to your example, I implemented a loggername based discriminator solution that directs different log output to different files. Although the logback documentation is so detailed, I could not find this important information. You must have found the solution mentioned by yayitswei already.

logback.xml:

 [...] <timestamp key="startTimestamp" datePattern="yyyy-MM-dd"/> <timestamp key="folderTimestamp" datePattern="MM-yyyy"/> <property name="LOGDIR" value="/var/log/spock" /> <appender name="SIFT" class="ch.qos.logback.classic.sift.SiftingAppender"> <discriminator class="com.enterprise.spock.LoggerNameBasedDiscriminator" /> <sift> <appender name="FILE-${loggerName}" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${LOGDIR}/${loggerName}-${startTimestamp}.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"> <fileNamePattern>${LOGDIR}/${folderTimestamp}/${loggerName}-%d{yyyy-MM-dd}-%i.log.gz</fileNamePattern> <maxFileSize>500KB</maxFileSize> <maxHistory>100</maxHistory> <totalSizeCap>50MB</totalSizeCap> </rollingPolicy> <encoder> <charset>UTF-8</charset> <pattern>%level %date{HH:mm:ss.SSS}: %msg %n</pattern> </encoder> </appender> </sift> </appender> [...] 

Edit: I replaced TimeBasedRollingPolicy with the SizeAndTimeBasedRollingPolicy parameter suggested here . For this you need at least logback 1.1.7.

What you get with this is a log file for every Logger created. Each log file will look like this: /var/log/loggername-2017-08-03.log.

When about 500 KB has been written to the file, it will be archived as gz-zipfile in / var / log / loggername / 08-2017 / loggername-2017-08-03-0.log.gz.

0 at the end of gz-zipfile-name is %i from <fileNamePattern> above. Without %i this will not work. Remember to use <configuration debug=true> in logback.xml if something doesn't work.

+1
source

Source: https://habr.com/ru/post/951555/


All Articles