Override logging in WildFly

2019-01-10 23:49发布

问题:

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>

回答1:

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>


回答2:

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