Search code examples
javajbosswildfly

Redirect all Wildfly logs to Log4j2 set by application OR use only one log manager


I'm migrating an old Web Application (war) written in Primefaces 8 using Apache and Weld dependencies to Jakarta EE 10 and Primefaces 13.0.4 using WildFly 31.0.1.Final.

So far it was successfully, all beans changed to use new annotations and a little interface changes were required. But I see the logs are a mess.

I have successfully configured Log4j2 within my WEB application (a .war file). All application logs are written to a specific file as the old application, but I see in WildFly standalone logs and in Eclipse console that my Log4j2 logs are being writeen "inside" WildFly logs.

Since I want to use only one log manager (not one for application and another for the application server), I have 3 questions:

  1. Is it possible to redirect all WildFly Application Server logs to my application Log4j2 configured logs, bypassing or ignoring the default WildFly log manager? Is it possible by Java code or with an extra XML/properties file?
  2. If 1 above is not possible, how can I use the default WildFly Application Server logs? I've tried to use java.util.logging clases but they don't write anything in console neither in standalone log files.
  3. Just for the sake of my sanity: what is the best practice to log application messages when using WildFly?

This is an example of the "log inside log" that I mentioned:

WildFly standalone logs (console and file)

15:39:34,029 INFO  [org.primefaces.webapp.PostConstructApplicationEventListener] (ServerService Thread Pool -- 96) Running on PrimeFaces 13.0.4
15:39:34,344 INFO  [org.jboss.resteasy.resteasy_jaxrs.i18n] (ServerService Thread Pool -- 96) RESTEASY002225: Deploying jakarta.ws.rs.core.Application: class br.com.powerdba.r2.JakartaRestConfiguration$Proxy$_$$_WeldClientProxy
15:39:34,373 INFO  [org.wildfly.extension.undertow] (ServerService Thread Pool -- 96) WFLYUT0021: Registered web context: '/r2' for server 'default-server'
15:39:34,416 INFO  [org.jboss.as.server] (ServerService Thread Pool -- 45) WFLYSRV0010: Deployed "r2-1.0.0.war" (runtime-name : "r2-1.0.0.war")
15:39:34,457 INFO  [org.jboss.as.server] (Controller Boot Thread) WFLYSRV0212: Resuming server
15:39:34,461 INFO  [org.jboss.as] (Controller Boot Thread) WFLYSRV0060: Http management interface listening on http://127.0.0.1:9990/management
15:39:34,461 INFO  [org.jboss.as] (Controller Boot Thread) WFLYSRV0051: Admin console listening on http://127.0.0.1:9990
15:39:34,464 INFO  [org.jboss.as] (Controller Boot Thread) WFLYSRV0025: WildFly Full 31.0.1.Final (WildFly Core 23.0.3.Final) started in 7537ms - Started 463 of 683 services (322 services are lazy, passive or on-demand) - Server configuration file in use: standalone.xml
15:39:35,498 INFO  [stdout] (default task-1) [INFO ] 2024-04-18 15:39:35.497 [default task-1] ValidateLoginFilter - uri = /r2/ 
15:39:35,500 INFO  [stdout] (default task-1) [WARN ] 2024-04-18 15:39:35.500 [default task-1] ValidateLoginFilter - Page needs authenticated users and user is not logged in. Calling Login page. 
15:39:35,506 INFO  [stdout] (default task-1) [INFO ] 2024-04-18 15:39:35.506 [default task-1] ValidateLoginFilter - uri = /r2/login.xhtml 
15:39:35,506 INFO  [stdout] (default task-1) [INFO ] 2024-04-18 15:39:35.506 [default task-1] ValidateLoginFilter - Calling doFilter for login page 
15:39:35,753 INFO  [stdout] (default task-1) [INFO ] 2024-04-18 15:39:35.753 [default task-1] LoginBean - ---/nLoginBean/n---

My application log on custom file

[INFO ] 2024-04-18 15:39:35.497 [default task-1] ValidateLoginFilter - uri = /r2/ 
[WARN ] 2024-04-18 15:39:35.500 [default task-1] ValidateLoginFilter - Page needs authenticated users and user is not logged in. Calling Login page. 
[INFO ] 2024-04-18 15:39:35.506 [default task-1] ValidateLoginFilter - uri = /r2/login.xhtml 
[INFO ] 2024-04-18 15:39:35.506 [default task-1] ValidateLoginFilter - Calling doFilter for 
[INFO ] 2024-04-18 15:39:35.753 [default task-1] LoginBean - ---/nLoginBean/n---


Solution

    1. Is it possible to redirect all WildFly Application Server logs to my application Log4j2 configured logs, bypassing or ignoring the default WildFly log manager? Is it possible by Java code or with an extra XML/properties file?

    No, you cannot redirect log messages from the server into your own logging configuration. Log messages from modules outside of your deployment will always use the logging configuration defined in the logging subsystem.

    1. If 1 above is not possible, how can I use the default WildFly Application Server logs? I've tried to use java.util.logging clases but they don't write anything in console neither in standalone log files.

    You can continue to use the log4j-api for your loggers inside your application. You just need to remove the library from your deployment as well as remove the log4j-core library. Then you can configure the logging subsystem or use a logging-profile or a logging configuration (logging.properties) in your deployment.

    1. Just for the sake of my sanity: what is the best practice to log application messages when using WildFly?

    It really depends. Really you can use any logging facade; log4j-api (log4j2), slf4j, jboss-logging, commons-logging or JUL. From an application perspective all you should do is log messages. The configuration would happen in the container.

    For the "inside" log message, e.g. [stdout] out you're seeing. WildFly wraps System.out and System.err in JUL loggers. This is done for cases where you may not have access to the console, but you don't want to miss some log from some random library that used System.out instead of a real logger. The reason you're seeing these is because you have the ConsoleAppender defined in your logging configuration within your deployment.

    You can also continue just use log4j-core and your own logging configuration in your deployment. The only thing you won't get is logging from server modules. In most cases this isn't a huge deal, but I can see where having logs from say Hibernate is desired. See the documentation for details there.