Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override logging in WildFly

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>
like image 272
Rinat Mukhamedgaliev Avatar asked Feb 18 '14 06:02

Rinat Mukhamedgaliev


2 Answers

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>
like image 63
James R. Perkins Avatar answered Nov 06 '22 07:11

James R. Perkins


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. :-)

like image 22
Xiaodong Xie Avatar answered Nov 06 '22 06:11

Xiaodong Xie