Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting multiple logs on one line with log4j

Tags:

java

log4j

I'm using org.apache.log4j.Logger and I frequently see lines in my log which look like this:

2013-06-09T00:06:00,092 data data data dat2013-06-09T00:06:10,253 data data2013-06-09T00:06:16,202 data d2013-06-09T00:06:20,750 data data data data

What I expect to see is:

2013-06-09T00:06:00,092 data data data data
2013-06-09T00:06:10,253 data data data data
2013-06-09T00:06:16,202 data data data data
2013-06-09T00:06:20,750 data data data data

That is, with multiple log-lines crammed together on one line, and log information being overwritten. I've no idea what I'm doing wrong, so I'd like to know (A) if it's something obvious or (B) any pointers that can help me solve this.

My logger class looks like this:

public class MyLogger {
    private final org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger("com.example.mylog");

    public void info(String info){
        synchronized (log){
            log.info(info);
        }
    }
}

Which I use like this:

private CallsLogger callsLogger;
/* ... */
myLogger.info(String.format("%s\t%s\t%s\t%s", "data", "data", "data", "data"));

I'm logging ~40.000 events per day, and about 200 of these have malformed lines, with anything from 2 to 11 separate events per line.

My log4j.xml looks liks this:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "http://logging.apache.org/log4j/docs/api/org/apache/log4j/xml/log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
    <appender name="mylog" class="org.apache.log4j.rolling.RollingFileAppender">
        <rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
            <param name="FileNamePattern" value="logs/mylog.log.%d.gz"/>
        </rollingPolicy>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern"
                   value="%d{yyyy-MM-dd'T'HH:mm:ss,SSS}\t%m%n"/>
        </layout>
    </appender>
    <logger name="com.example.mylog" additivity="false">
        <level value="INFO"/>
        <appender-ref ref="mylog"/>
    </logger>
    <root>
        <priority value="ERROR"/>
        <appender-ref ref="appender"/>
    </root>
</log4j:configuration>

Any help greatly appreciated.

like image 749
Adam Avatar asked Jun 11 '13 11:06

Adam


2 Answers

<param name="ConversionPattern"
               value="%d{yyyy-MM-dd'T'HH:mm:ss,SSS} - %m%n"/>

Try the above pattern without \t

OR

like image 77
Pratik Shelar Avatar answered Oct 19 '22 08:10

Pratik Shelar


Use a class like

public class MyPatternLayout extends PatternLayout {
...
public String format(LoggingEvent pEvent) {
    return super.format(pEvent).replace('\n', ' ') + '\n';
}
...

This will remove all \n from the string to be logged and then add one at the end of the line.

And in the log4j config xml use:

<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
...
<layout class="....MyPatternLayout">
...
</layout>
</appender>
like image 25
danidacila Avatar answered Oct 19 '22 06:10

danidacila