Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log4j - Invalid element or attribute in RollingRandomAccessFile

Tags:

java

log4j

I'm kind of new to Log4J (and Java in general). I'm trying to output some logs to logstash. I ran into some issues with the logstash's built-in log4j input type. For that reason, I wanted to use the json_event layout approach. While I believe I have everything setup properly, I'm getting an error when I start my app that says:

ERROR: RollingRandomAccessFile contains an invalid element or attribute "layout".

I do not understand why I'm getting this, or how to resolve this. I'm more interested in getting my logs in the json_event format than anything else. Currently, my log4j2.xml file looks like this:

<Configuration status="WARN" monitorInterval="30">
  <Appenders>
    <RollingRandomAccessFile name="RollingFile" fileName="/logs/recent.log"
      filePattern="/logs/$${date:yyyy-MM}/server-%d{yyyy-MM-dd-HH}-%i.log.gz">
      <layout class="net.logstash.log4j.JSONEventLayoutV1" />
      <Policies>
        <TimeBasedTriggeringPolicy interval="4" modulate="true"/>
        <SizeBasedTriggeringPolicy size="100 MB"/>
      </Policies>
    </RollingRandomAccessFile>
    <Async name="AsyncFile">
      <AppenderRef ref="RollingFile"/>
    </Async>
  </Appenders>
  <Loggers>
    <Logger name="com.myApp" level="trace" additivity="false">
      <AppenderRef ref="AsyncFile"/>
    </Logger>
    <Root level="trace">
      <AppenderRef ref="AsyncFile"/>
    </Root>
  </Loggers>
</Configuration>

To start my app, I'm running the following at the command-line:

java -Dlog4j.configurationFile=log4j2.xml -cp "/home/ubuntu/jsonevent-layout-1.6.jar" -Xms256m -Xmx1024m -jar myApp.jar

I've confirmed that the path to jsonevent-layout-1.6.jar is correct. My app successfully logs if I remove the new layout. So, I know that log4j is running properly. I know my app runs just fine. This problem is isolated to using the json_event layout. Can someone please help me get this issue resolved? I'm totally stumped as to what I'm doing wrong.

Thank you

like image 359
user3517343 Avatar asked Apr 09 '14 22:04

user3517343


1 Answers

The net.logstash.log4j.JSONEventLayoutV1 from github.com/logstash/log4j-jsonevent-layout does not work with Log4J2. You will find a fork at github.com/maartenbosteels/log4j-jsonevent-layout which is compatible with Log4J2. Unfortunately, it is not in a maven central. So you'll have to solve that. However, once you have it that your log4j2.xml will look something like.

<RollingRandomAccessFile name="RollingFile" fileName="/logs/recent.log"
   filePattern="/logs/$${date:yyyy-MM}/server-%d{yyyy-MM-dd-HH}-%i.log.gz">
   <JSONEventLayoutV1>
   <Policies>
     <TimeBasedTriggeringPolicy interval="4" modulate="true"/>
     <SizeBasedTriggeringPolicy size="100 MB"/>
   </Policies>
</RollingRandomAccessFile>
like image 169
juice Avatar answered Nov 16 '22 16:11

juice