Logging

Appkit provides support for tracking the activity of specific events and logging them either to the console or a file depending on how your application is set up.

Log activity events

To log activity events, perform the steps in this section.

1: Update the POM

To log activity events, first add these dependencies to your pom.xml file:

<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.1.1</version>
</dependency>
<dependency>
    <groupId>twigkit</groupId>
    <artifactId>twigkit.activity.logger</artifactId>
    <version>${project.parent.version}</version>
</dependency>

2: Create an activity logging configuration file

In conf/activity/tracking, place a logger.conf file. In the file, set the property logging-enabled equal to true:

logging-enabled: true

If logging-enabled is set to false, then all logging will be disabled.

3: Create a logback.xml file

Create a logback.xml file in /resources and configure this to either output activity data to the console or a file (or both).

For example, to output logging activity to the console:

<configuration debug="true" scan="true">
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <withJansi>false</withJansi>
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} %highlight(%-5level) %cyan(%logger{36}) - %msg %n</pattern>
        </encoder>
    </appender>

    <logger name="twigkit" level="INFO"/>
    <logger name="twigkit.activity.subscribers" level="TRACE"/>

    <root level="ERROR">
        <appender-ref ref="STDOUT"/>
    </root>
</configuration>

To output logging activity to a file, while all other logging is redirected to the console:

<configuration debug="true" scan="true">
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <withJansi>false</withJansi>
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} %highlight(%-5level) %cyan(%logger{36}) - %msg %n</pattern>
        </encoder>
    </appender>

    <appender name="activity-log" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>./logs/activity.log</file>
        <append>true</append>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">

            <!-- Daily rollover. Make sure the path matches the one in the file element or else
             the rollover logs are placed in the working directory. -->
            <fileNamePattern>./logs/activity_%d{yyyy-MM-dd}.%i.log</fileNamePattern>

            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>5MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>

            <!-- keep 30 days' worth of history -->
            <maxHistory>30</maxHistory>

        </rollingPolicy>
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} - %msg %n</pattern>
        </encoder>
    </appender>

    <logger name="twigkit" level="INFO"/>

    <logger name="twigkit.activity.subscribers" level="TRACE" additivity="false">
        <appender-ref ref="activity-log"/>
    </logger>

    <root level="ERROR">
        <appender-ref ref="STDOUT"/>
    </root>
</configuration>

The location of the file is set in the file tag and is relative to the root of the application.

Example

A typical example of what you might see in the log is:

16:33:51.711 - type:response,user:user,query:US,hits:2437,page:1,time:104
16:33:52.907 - type:response,user:user,query:US,hits:2437,page:1,time:106
16:33:54.191 - type:click,username:user,URL:http://twigkit.com/kapps_kyc/customers/519/,host:twigkit.com

Where, for example, the type refers to whether the event is a click or response, the user is the name of the user, query shows the query terms, hits is the number of hits returned in the response, page is the page number, and URL is the URL associated with the click.