2 Replies Latest reply on Jan 23, 2015 4:26 PM by Andrew May

    Is it possible to use a log4j Layout as custom-formatter?

    Andrew May Newbie

      We're looking to put all of our WildFly (8.2) logs into a custom JSON format, including both server logs and application logs.

       

      From searching around it appears that it is possible to use a Log4j Appender using the <custom-handler> tag ([AS7-4925] Log4j appenders should also be allowed for custom handlers - JBoss Issue Tracker), but the only example I could find of using that appears to use a JBoss Logging formatter (Re: Log4jAppenderHandler usage).

       

      I've already got a custom log4j layout, and I'd like to use it either with the <periodic-rotating-file-handler> or a <custom-handler> using Log4j's RollingFileAppender, but I'm not sure if that's possible. I tried to specify a <custom-layout> (actually just trying one of the standard Log4j layouts to start with) but got an error:

       

      <formatter name="JSON">
          <custom-formatter class="org.apache.log4j.PatternLayout" module="org.apache.log4j">
              <properties>
                  <property name="ConversionPattern" value="{%m}"/>
              </properties>
          </custom-formatter>
      </formatter>
      

       

      13:01:24,313 ERROR [org.jboss.as.controller.management-operation] (Controller Boot Thread) JBAS014612: Operation ("add") failed - address: ([
          ("subsystem" => "logging"),
          ("custom-formatter" => "JSON")
      ]): java.lang.IllegalArgumentException: Failed to load class "org.apache.log4j.PatternLayout" for formatter "JSON"
              at org.jboss.logmanager.config.AbstractPropertyConfiguration.<init>(AbstractPropertyConfiguration.java:76) [jboss-logmanager-1.5.2.Final.jar:1.5.2.Final]
              at org.jboss.logmanager.config.FormatterConfigurationImpl.<init>(FormatterConfigurationImpl.java:33) [jboss-logmanager-1.5.2.Final.jar:1.5.2.Final]
              at org.jboss.logmanager.config.LogContextConfigurationImpl.addFormatterConfiguration(LogContextConfigurationImpl.java:174) [jboss-logmanager-1.5.2.Final.jar:1.5.2.Final]
              at org.jboss.as.logging.logmanager.ConfigurationPersistence.addFormatterConfiguration(ConfigurationPersistence.java:219)
              at org.jboss.as.logging.CustomFormatterResourceDefinition$2.performRuntime(CustomFormatterResourceDefinition.java:108)
              at org.jboss.as.logging.LoggingOperations$LoggingAddOperationStepHandler$1.execute(LoggingOperations.java:207)
              at org.jboss.as.controller.AbstractOperationContext.executeStep(AbstractOperationContext.java:660) [wildfly-controller-8.2.0.Final.jar:8.2.0.Final]
              at org.jboss.as.controller.AbstractOperationContext.doCompleteStep(AbstractOperationContext.java:501) [wildfly-controller-8.2.0.Final.jar:8.2.0.Final]
              at org.jboss.as.controller.AbstractOperationContext.completeStepInternal(AbstractOperationContext.java:298) [wildfly-controller-8.2.0.Final.jar:8.2.0.Final]
              at org.jboss.as.controller.AbstractOperationContext.executeOperation(AbstractOperationContext.java:293) [wildfly-controller-8.2.0.Final.jar:8.2.0.Final]
              at org.jboss.as.controller.ModelControllerImpl.boot(ModelControllerImpl.java:346) [wildfly-controller-8.2.0.Final.jar:8.2.0.Final]
              at org.jboss.as.controller.AbstractControllerService.boot(AbstractControllerService.java:297) [wildfly-controller-8.2.0.Final.jar:8.2.0.Final]
              at org.jboss.as.server.ServerService.boot(ServerService.java:356) [wildfly-server-8.2.0.Final.jar:8.2.0.Final]
              at org.jboss.as.server.ServerService.boot(ServerService.java:331) [wildfly-server-8.2.0.Final.jar:8.2.0.Final]
              at org.jboss.as.controller.AbstractControllerService$1.run(AbstractControllerService.java:259) [wildfly-controller-8.2.0.Final.jar:8.2.0.Final]
              at java.lang.Thread.run(Thread.java:745) [rt.jar:1.7.0_72]
      Caused by: java.lang.ClassCastException: class org.apache.log4j.PatternLayout
              at java.lang.Class.asSubclass(Class.java:3208) [rt.jar:1.7.0_72]
              at org.jboss.logmanager.config.AbstractPropertyConfiguration.<init>(AbstractPropertyConfiguration.java:74) [jboss-logmanager-1.5.2.Final.jar:1.5.2.Final]
              ... 15 more
      

       

      Does <custom-formatter> only support JUL Formatters? Is it possible to use Log4j Layouts if we use a Log4j Appender as a <custom-handler> and if so how is that configured?

       

      Thanks,

      Andrew