Search code examples
log4jactivejdbcjavalite

log4j.properties does not have effects on logging in activeJDBC


I am currently working on a simple web application using javalite webactive and activeJDBC. To start with javalite I simply used https://github.com/javalite/activeweb-simple/ and expanded from there.

One thing which I would like to change, now that I have already implemented a few controllers and models I want to customize the logger:

[qtp1442407170-13] INFO org.javalite.activeweb.RequestDispatcher - {"controller":"app.controllers.HomeController","duration_millis":685,"remote_ip":"0:0:0:0:0:0:0:1","method":"GET","action":"index","url":"http://localhost:8080/home/index","status":200}.

I don't really get what the prefix represents hence I would like to change that part to show date and timestamps.

Following http://javalite.io/logging#log4j-configuration I should be able to customize the logging via log4j.properties in src/main/resources, but this does not seem to make any difference.

The files content:

log4j.rootLogger=INFO, stdout

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %4p %c{1} - %m%n

I suspect that I have somehow overwritten the default logger, although I did not change anything related in the pom.xml or Main.java.

Am I missing something or could it be that the changes somehow get overwritten?


Solution

  • here is a method I have used on all my JavaLite ActiveWeb projects. The requirements are:

    1. I want to log to a console while development - for convenience.
    2. I want to log to a file in any other environment
    3. I want my log file to have a JSON format because I can analyze it in Splunk, ElasticSearch or any other similar tool.

    So, I augmented the ActiveWeb with this commit.

    Let's do it step by step:

    1. Add a property to the pom file <logger-name>CONSOLE</logger-name>
    2. Add a file log4j.properties with this content:

    ```

    log4j.rootLogger=INFO, ${logger-name}
    log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
    log4j.appender.CONSOLE.Target=System.out
    log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
    log4j.appender.CONSOLE.layout.ConversionPattern=%d %4p [%t]: %c{1} - %m%n
    
    log4j.appender.FILE=org.apache.log4j.DailyRollingFileAppender
    log4j.appender.FILE.File=logs/activeweb-simple.log
    log4j.appender.FILE.Append=true
    log4j.appender.FILE.Encoding=UTF-8
    log4j.appender.FILE.layout=org.javalite.logging.JsonLog4jLayout
    

    ```

    1. Add an SLF4J dependency to integrate it with Log4J:

    ```

    <dependency>
       <groupId>org.slf4j</groupId>
       <artifactId>slf4j-log4j12</artifactId>
       <version>1.7.5</version>
    </dependency>
    

    ```

    1. Add a profile to the pom:

    <profiles> <profile> <id>file_log</id> <properties> <logger-name>FILE</logger-name> </properties> </profile> </profiles>

    1. Add property filtering to pom:

    <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources>

    Lets see how this is working.

    When you run the program in a development environment without a profile, maven replaces ${logger-name} in your log4j.properties file for CONSOLE. This way, you use the CONSOLE logger, and everything is printed to your console. If you run your program with profile -Pfile_log, then you Maven will replace ${logger-name} with FILE, so Log4J will log to a corresponding file.

    Additionally, see that the console layout is org.apache.log4j.ConsoleAppender, but the file layout is org.javalite.logging.JsonLog4jLayout. The org.javalite.logging.JsonLog4jLayout is a JavaLite integration with Log4J built specifically to integrate with Log4J and produce a JSON output.

    So, how do you use it? In a development environment you do not need to do anything special, it just works. When you need to build it, run mvn package -Pfile_log -