2

I have configured my WildFly 18 server in domain mode format and we use log4j logger. When I checked in the server.log file, All logs from log4j are getting logged after wildfly's own logger format. Like below line 1st date and log level is from server logs and next date format and log level and log message is comming from log4j.

2021-03-19 00:13:06,623 INFO  2021-03-19 00:13:06,601 INFO  [com.app.connection.service] CurrentThreadID=436 On

I search a lot...I found so many configurations related to standalone mode and some said these configurations can be done in domain.xml but nothing worked

I have the following configuration in domain.xml

          <subsystem xmlns="urn:jboss:domain:logging:8.0">
                <custom-handler name="CUSTOM-FILE" class="org.jboss.logmanager.handlers.PeriodicSizeRotatingFileHandler" module="org.jboss.logmanager">
                    <formatter>
                        <named-formatter name="MY-PATTERN"/>
                    </formatter>
                    <properties>
                        <property name="maxBackupIndex" value="24"/>
                        <property name="rotateSize" value="10000000"/>
                        <property name="suffix" value=".yyyy-MM-dd-HH"/>
                        <property name="append" value="true"/>
                        <property name="fileName" value="${jboss.server.log.dir}/server.log"/>
                    </properties>
                </custom-handler>
                <logger category="com.arjuna">
                    <level name="WARN"/>
                </logger>
                <logger category="io.jaegertracing.Configuration">
                    <level name="WARN"/>
                </logger>
                <logger category="org.jboss.as.config">
                    <level name="DEBUG"/>
                </logger>
                <logger category="sun.rmi">
                    <level name="WARN"/>
                </logger>
                <root-logger>
                    <level name="INFO"/>
                    <handlers>
                        <handler name="CUSTOM-FILE"/>
                    </handlers>
                </root-logger>
                <formatter name="PATTERN">
                    <pattern-formatter pattern="%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p %s%e%n"/>
                </formatter>
                <formatter name="MY-PATTERN">
                    <pattern-formatter pattern="%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p %s%e%n"/>
                </formatter>
                <formatter name="COLOR-PATTERN">
                    <pattern-formatter pattern="%K{level}%d{HH:mm:ss,SSS} %-5p %s%e%n"/>
                </formatter>
            </subsystem>

In log4j.xml, I have a console appender like below, and this log4j.xml present inside modules directory at path wildfly18.0.1\modules\org\apache\logging\log4j\main\log4j2-props

<Console name="Console">
    <PatternLayout pattern="%d %-5p [%c] %m\n" />
</Console>

Following things I tried :

  1. Added tags inside logging subsystem

    <add-logging-api-dependencies value="false"/>
    <use-deployment-logging-config value="false"/>
    
  2. Added loggers

 <logger category="stdout" use-parent-handlers="false">
        <level name="INFO"/>
        <handlers>
             <handler name="CUSTOM-FILE"/>
         </handlers>
  </logger>
  1. Excluded subsystems and modules from deployments. Added following in META-INF\jboss-deployment-structure.xml

We use EAR deployment file

<deployment>
        <exclude-subsystems>
           <subsystem name="logging"/>
        </exclude-subsystems>
        <exclusions>
            <module name="org.apache.commons.logging"/>
            <module name="org.apache.log4j"/>
            <module name="org.jboss.logging"/>
            <module name="org.jboss.logging.jul-to-slf4j-stub"/>
            <module name="org.jboss.logmanager"/>
            <module name="org.jboss.logmanager.log4j"/>
            <module name="org.slf4j"/>
            <module name="org.slf4j.impl"/>
        </exclusions>
    </deployment>

So, please can somebody help me with the right configuration for domain mode setup?

Thanks in advance...

Pmestry
  • 21
  • 3

2 Answers2

0

I'm not sure it's an option, but WildFly 22 has log4j2 support so you can configure logging with WildFly and don't need to include a log4j2.xml.

It looks like you want to redirect stdout to a file. If you want your log4j2 configuration to control the format, you need to change the format for your MY-FORMATTER. Below are some CLI commands to configure the server.

# Add the pattern for the stdout logger
/subsystem=logging/pattern-formatter=MY-PATTERN:add(pattern="%s%n")
# Create a file to direct stdout to
/subsystem=logging/periodic-size-rotating-file-handler=CUSTOM-FILE:add(named-formatter=MY-PATTERN, max-backup-index=24, rotate-size="10m", suffix=".yyyy-MM-dd-HH", append=true, file={relative-to=jboss.server.log.dir, path=server-stdout.log})
# Create the stdout logger, direct it to the file only
/subsystem=logging/logger=stdout:add(handlers=[CUSTOM-FILE], use-parent-handlers=false)

Note that I renamed the log file to server-stdout.log as to not override the default server.log. The reason for this is the server itself will log to this file and if you override it with that pattern then the logs from the server itself will not be formatted.

Also note that anything that is written to System.out will end up in the server-stout.log as well.

James R. Perkins
  • 16,800
  • 44
  • 60
  • Thanks for the answer...But we have a requirement to write everything in the server.log file itself as we are using this file in our application to fetch entire server logs. – Pmestry Mar 23 '21 at 05:55
  • Is there any way where I can add console-handler in domain.xml by using following configuration commands and redirecting console loggers to server.log file bcoz I tired using this console-handler but log4j2 logs disappeared from my server.log file ```/subsystem=logging/console-handler=JUST-PRINT:add(formatter="%s%E%n") /subsystem=logging/logger=stderr:add(use-parent-handlers="false", handlers=[JUST-PRINT]) /subsystem=logging/logger=stdout:add(use-parent-handlers="false", handlers=[JUST-PRINT])``` – Pmestry Mar 23 '21 at 06:06
  • That will print messages from WildFly itself with just the message. There is no way to get WildFly to log to a log4j2 configuration. One option is you could use https://search.maven.org/artifact/org.jboss.logmanager/log4j2-jboss-logmanager/1.0.0.Final/jar which is what WildFly 22+ uses to write log4j2 messages to the jboss-logmanager. – James R. Perkins Mar 23 '21 at 15:05
0

As written in many other comments, Jboss adds it's own pattern and treats pattern of application log4j2.xml as the "message" to its own pattern. To resolve this add following to the logging subsystem of your Jboss Standalone.xml

   <subsystem xmlns="urn:jboss:domain:logging:8.0">
    ....
    <console-handler name="stdout-console" autoflush="true">
        <level name="ALL"/>
        <formatter>
            <pattern-formatter pattern="%s%n"/>
        </formatter>
    </console-handler>
    <logger category="stdout" use-parent-handlers="false">
        <handlers>
            <handler name="stdout-console"/>
        </handlers>
    </logger>
    ....
   </subsystem>

This will cause the default log pattern of Jboss to be overwritten and you log will have what you have configured in log4j2.xml console appender.

Nilesh Kumar
  • 109
  • 1
  • 5