Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to log data in files in Play framework?

I have a Play server (Play framework 2.3) implemented in Java. I want to keep logs of API calls in files limiting the maximum number of files and maximum size of each file. I have the below 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 have taken from here. I do not fully understand the above configuration. I use the following lines to log into 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, it doesn't work. The access.log file is empty while all logging goes to application.log file. I have commented out all logger settings in application.conf. When I looked into the file Logger.class, I found this line

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

Is this the reason why it's not working? Please suggest how do I fix it.

like image 311
ajay Avatar asked Feb 17 '15 10:02

ajay


1 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 pattern put the application home path.

like image 152
Rakesh Chouhan Avatar answered Oct 18 '22 02:10

Rakesh Chouhan