19

I used tomcat and simply override default logging system. How to enable logging with logback on wildfly in my spring app?

My Logback.xml owrking on tomcat

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>ERROR</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
        <encoder>
            <pattern>%-4relative [%thread] %-5level %logger{35} - %msg %n</pattern>
        </encoder>
    </appender>

    <logger name="org.springframework" level="WARN" />
    <logger name="com.citronium.planstery" level="INFO" />
    <logger name="org.apache.http.impl.conn.tsccm" level="ERROR" />

    <root level="INFO">
        <appender-ref ref="STDOUT" />
    </root>
</configuration>
pbaris
  • 4,525
  • 5
  • 37
  • 61
Rinat Mukhamedgaliev
  • 5,401
  • 8
  • 41
  • 59
  • I can't exactly answer your questions but I can give you pointers. Please check this [thread](https://community.jboss.org/thread/222437?tstart=0). It is not pretty straigh forward to override JBossLogging anymore. With their new modular classloading architecture, it has become pretty much an overhead to override the logging framework. We had similar issues and decided to go with SLf4J+JBossLogging combination. – Vikram Feb 18 '14 at 17:56
  • Maxim see date. My question was previously – Rinat Mukhamedgaliev Jul 14 '14 at 07:44

2 Answers2

34

You can use logback to configure logging in your application. You can't use logback to configure logging for the server.

To use logback in your configuration you'll need to change the add-logging-api-dependencies to false or create a jboss-deployment-structure.xml that excludes the subsystem. You'll also need to include logback and slf4j in your deployment.

The first option of changing the add-logging-api-dependencies is a global setting for all deployments. The follow CLI command will change the value:

/subsystem=logging:write-attribute(name=add-logging-api-dependencies,value=false)

This option simply doesn't add any of the implicit logging dependencies to your deployment.

The second option of using a jboss-deployment-structure.xml will disable the logging subsystem for your deployment only. The following is an example file:

<jboss-deployment-structure>
  <deployment>
     <!-- exclude-subsystem prevents a subsystems deployment unit processors running on a deployment -->
     <!-- which gives basically the same effect as removing the subsystem, but it only affects single deployment -->
     <exclude-subsystems>
        <subsystem name="logging" />
    </exclude-subsystems>
  </deployment>
</jboss-deployment-structure>
Jens Piegsa
  • 7,399
  • 5
  • 58
  • 106
James R. Perkins
  • 16,800
  • 44
  • 60
  • After using the jboss-deployment-structure.xml I am getting this exception: Caused by: java.lang.NoClassDefFoundError: Lorg/apache/commons/logging/Log; – Sujoy Aug 14 '14 at 19:29
  • Make sure you include commons-logging as a library in your deployment or add an explicit org.apache.commons.logging dependency to the jboss-deployment-structure.xml. That said why exclude logging if you're using commons-logging? – James R. Perkins Aug 14 '14 at 21:13
  • I don't use commons logging. I use log4j. My logging was not at all working. After adding exclude subsystem and adding the commons-logging.jar in my war it started working. Thanks James. But I still don't understand this. Where can I get more info? Everywhere it's so convoluted. I get confused. – Sujoy Aug 14 '14 at 22:48
  • There are several reasons it may not be working. It all depends on your setup. Feel free to post a detailed question https://community.jboss.org/en/wildfly. WRT commons-logging it's likely one of your dependencies requires it. – James R. Perkins Aug 14 '14 at 22:54
  • ya may be Spring needs it. – Sujoy Aug 18 '14 at 00:22
  • Where is the /subsystem=logging:write-attribute(name=add-logging-api-dependencies,value=false) command placed? – John Jun 16 '17 at 19:49
  • Into CLI, e.g. `$JBOSS_HOME/bin/jboss-cli.sh`. – James R. Perkins Jun 16 '17 at 22:50
5

Here is how we do this, by the way, we are using wildfly-8.1.0-Final.

First, make a jar file containing this class: https://gist.github.com/xiaodong-xie/219491e0b433f8bd451e

Then put this jar file into "wildfly-8.1.0.Final/modules/system/layers/base/org/jboss/logmanager/main", and add a reference to this jar file in the module.xml file in the exact same folder.

Then put "logback-classic-1.1.2.jar" and "logback-core-1.1.2.jar" (You can use any version of logback you choose) into "wildfly-8.1.0.Final/modules/system/layers/base/org/jboss/logmanager/main", and reference those 2 jar files in the module.xml file.

Add the following to the "subsystem:logging" in the standalone.xml you are using:

<custom-handler name="logback" class="org.slf4j.bridge.SLF4JBridgeHandler" module="org.jboss.logmanager"></custom-handler>

And reference this handler in the root-logger element following:

        <root-logger>
            <level name="INFO"/>
            <handlers>
                <handler name="CONSOLE"/>
                <handler name="logback"/>
            </handlers>
        </root-logger>

Here is an example of logback.xml:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator"/>

<appender name="LOGFILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>${JBOSS_HOME}/standalone/log/server-logback.log</file>
    <append>true</append>
</appender>
<appender name="ASYNC" class="ch.qos.logback.classic.AsyncAppender">
    <appender-ref ref="LOGFILE"/>
</appender>
<root level="INFO">
    <appender-ref ref="ASYNC"/>
</root>
</configuration>

And put this logback.xml file into "wildfly-8.1.0.Final/standalone/configuration" folder.

Add the following to the "standalone.sh" or equivalent in the "wildfly-8.1.0.Final/bin" folder.

-Dlogback.configurationFile=file:$JBOSS_CONFIG_DIR/logback.xml

Just under "-Dlogging.configuration=file:$JBOSS_CONFIG_DIR/logging.properties" line. There are 2 places in "standalone.sh" file.

=================================================================================

Or you can do it in a simpler way. :)

Put the 2 logback jar files to the "jboss.logmanager" module, and add "-Dorg.jboss.logging.provider=slf4j" to the "standalone.sh" file, the same position.

I found there are some logging missing if going this by the way, since Wildfly itself still using its own logging facility if going this way.

Have fun. :-)

Xiaodong Xie
  • 149
  • 3
  • 5
  • 2
    Is it somehow possible to include the configuration in your application? Like placing it in the EAR/WAR-file on delivery? Seems to be the correct way to me, if you disable your logger via `jboss-deployment-structure.xml`. – Robert Heine Oct 22 '15 at 07:11