How to register data in files on the Play platform?

I have a Play server (Play Framework 2.3) implemented in Java. I want to keep API call logs in files limiting the maximum number of files and the maximum size of each file. I have the following application-logger.xml file

 <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>/home/ajay/projects/application.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!-- Daily rollover with compression --> <fileNamePattern>application-log-%d{yyyy-MM-dd}.gz</fileNamePattern> <!-- keep 30 days worth of history --> <maxHistory>30</maxHistory> </rollingPolicy> <encoder> <pattern>%date{yyyy-MM-dd HH:mm:ss ZZZZ} - [%level] - from %logger in %thread %n%message%n%xException%n</pattern> </encoder> </appender> <appender name="ACCESS_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>/home/ajay/projects/access.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!-- daily rollover with compression --> <fileNamePattern>access-log-%d{yyyy-MM-dd}.gz</fileNamePattern> <!-- keep 1 week worth of history --> <maxHistory>7</maxHistory> </rollingPolicy> <encoder> <pattern>%date{yyyy-MM-dd HH:mm:ss ZZZZ} %message%n</pattern> <!-- this quadruples logging throughput --> <immediateFlush>false</immediateFlush> </encoder> </appender> <!-- additivity=false ensures access log data only goes to the access log --> <logger name="access" level="INFO" additivity="false"> <appender-ref ref="ACCESS_FILE" /> </logger> <root level="INFO"> <appender-ref ref="FILE"/> </root> 

which I took from here . I do not fully understand the above configuration . I use the following lines to enter the access.log file

 import play.Logger; // log in the access.log file final Logger.ALogger accessLogger = Logger.of("access"); accessLogger.info("Logging api call from the client"); 

However, this will not work. The access.log file is empty, and all entries go to the application.log file. I commented on all the registrar settings in application.conf . When I looked at the Logger.class file, I found this line

 private static final ALogger logger = of("application"); 

Is this the reason why it does not work? Please suggest how to fix this.

+6
source share
2 answers

Try this appender:

  <appender name="CUSTOM" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${application.home}/logs/agent.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${application.home}/logs/old/myLog-%d{yyyy-MM-dd}.log.%i</fileNamePattern> <maxHistory>30</maxHistory> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <maxFileSize>100MB</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> </rollingPolicy> <encoder> <pattern>%date %M %logger{0} %message%n</pattern> </encoder> </appender> 

In the file name template, put the source path of the application.

+1
source

immediateFlush property of your appender is set to false, so you do not see any changes to the access.log file. Just uninstall

 <immediateFlush>false</immediateFlush> 

from your xml configuration.

From the protocol documentation: Immediately flushing the output stream ensures that logging events are immediately written to disk and will not be lost if your application exits without properly closing applications.

0
source

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


All Articles