Trying to figure out activeweb logging to a separate file

60 Views Asked by At

I am trying to use the default active web library to log everything to a separate log file. Right now I'm running everything under IntelliJ (via mvn jetty:run) and all the logging is coming out to the console only.

I tried added a log4j.properties file in the WEB-INF directory; didn't work (I have not added log4j dependency to my pom as I don't want it in there).

Looking a slf4j, I cannot find any properties or config file that let's me define how I would log to a specific log file. And, I'm not sure what logging AW uses, so it's hard to see what I need to configure.

Stuck at this point, and just googling and reading thru the slf4j site to try to get this working.

1

There are 1 best solutions below

6
On BEST ANSWER

I general, if you want logging done by Log4j and Slf4j, you will need to add appropriate dependencies. Here is the configuration from one of our projects:

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

this will bring appropriate deps.

Here is the contents of the log4j.properties file.

log4j.rootLogger=INFO, ${logger-name}, SPLUNK

log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Target=System.out
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout

log4j.appender.SPLUNK=org.apache.log4j.DailyRollingFileAppender
log4j.appender.SPLUNK.File=${catalina.home}/logs/worker-splunk.log
log4j.appender.SPLUNK.Append=true
log4j.appender.SPLUNK.Encoding=UTF-8

# This is a filter that will filter out junk we do not want to sent to Splunk
log4j.appender.SPLUNK.filter.1=app.utils.SplunkLogFilter
log4j.appender.SPLUNK.layout=org.javalite.logging.JsonLog4jLayout


log4j.appender.FILE=org.apache.log4j.DailyRollingFileAppender
log4j.appender.FILE.File=${catalina.home}/logs/worker.log
log4j.appender.FILE.Append=true
log4j.appender.FILE.Encoding=UTF-8
log4j.appender.FILE.layout=org.javalite.logging.JsonLog4jLayout

The logger-name is a Maven- filtered property. Locally it is resolved to CONSOLE and when the app is built, it resolves to FILE. This way, we can observe log on the console during development.

The class SplunkLogFilter looks like this:

import org.apache.log4j.spi.Filter;
import org.apache.log4j.spi.LoggingEvent;

public class SplunkLogFilter extends Filter {

    private static final String[] EXCLUDED_LOGGERS = new String[]{"***ServiceImpl", "app.utils.ProcessUtil"};
    private static final String[] EXCLUDED_MESSAGES = new String[]{"****Command"};

    @Override
    public int decide(LoggingEvent event) {
        String loggerName = event.getLoggerName();
        for (String excludedLogger : EXCLUDED_LOGGERS) {
            if(loggerName.equals(excludedLogger)){
                return Filter.DENY;
            }
        }
        String message = event.getMessage().toString();
        for (String excludedMessage : EXCLUDED_MESSAGES) {
            if(message.contains(excludedMessage)){
                return Filter.DENY;
            }
        }
        return Filter.NEUTRAL;
    }
}

So, we are logging into two files in parallel, where one is shipped into Splunk. The Splunk file is smaller, so we pay less for Splunk, but we retain full files just in case.