Spring Boot - logging configuration from JBoss - spring

I'd like to know if there is a way to configure Spring Boot logging using JBoss standalone.xml or domain.xml configuration. For now I ended up with two empty files log4j.properties and log4j-file.properties under org\springframework\boot\logging\log4j package in mvn resources in WAR file and it looks like now my logging configuration in domain.xml works because I've overriden default spring-boot log4j configuration.
But there must be a better way to do this instead of this stupid hack. Keep in mind that I don't want to provide external log4j properties file and set it for one of the property in application.properties but I want to use jboss logging system configuration.
EDIT
Strange thing but when I updated Spring Boot to version 1.4.0 then JBoss intercepts the logs and log them only to the file and I don't have logs on the stdout anymore even without custom log4j configuration.

Related

log4j2 behaviour different between tomcat 8.5.83 and 8.5.39

We have our web app running on tomcat 8.5.39. This app is using log4j2 correctly, applying a system property before log4j2 starts to indicate the log4j.xml configuration file. Something like:
System.setProperty("log4j.configurationFile", "/opt/ventusproxy/logs");
Now we have migrated to tomcat 8.5.83, and after this our webapp is not able to find the configuration file. Asking the log4j team, and after checking log4j debug file, it seems that, for any reason, log4j2 is starting before we set the previous system property.
We also tried 8.5.79 with the same result.
Is there any configuration change that we are missing between 8.5.39 and a later tomcat version that breaks the way log4j2 was working into our webapp until now?
Thanks,
Joan.

Spring Log4j2 xml file location

using Spring 2.0.5 with Log4j2
Have found works as expected if I place the file log4j2.xml in the resources folder.
However, have found the log4j2 option of monitorInterval="60" to be super useful as if some production issue can increase logging on some class without a restart. However if log4j2.xml is embedded in the jar of course it cannot be modified. so far the best I have been able to do is use 2 copies of log4j2.xml one in the resources and the other in the folder running the jar from.
I can then run:
java -Dlog4j.configurationFile=log4j2.xml -jar myapp.jar
it seems to work the same if started from the maven spring-boot plugin or from Eclipse.
Have tried a number of things such as setting the classpath, the absolute file name etc. but no luck
My question is how can I remove the log4j2.xml file from the resources folder and only specify it on the command line when starting spring?
I am not entirely clear on your question but I can provide the following information which I hope addresses it:
Log4j 2.12.0 added support for Spring Cloud Config and enhanced the support for Spring Boot. As of that version you can include a file named log4j2.system.properties and place any system properties you want defined there. They will be set before Log4j initializes. So you can specify the full URL to the configuration there if you want instead of the command line. You can also add the definition to a file named log4j2.component.properties.
The support for Spring Cloud Config allows you to place the configuration in your Spring Cloud Config server. See Spring CLoud Config support for more details.
Spring Boot initializes logging at least 3 times. The first is usually because the SpringApplication class declares a Logger so logging is initialized before anything else happens. The configuration for that will use "normal" log4j 2 initialization. After that Spring influences how logging initialization occurs primarily because Spring Boot sets the class path to include the jars inside BOOT-INF/lib directory inside your Spring Boot jar.
By using one of the configuration options I outlined above you can move the logging configuration outside of your application and you should not require a logging configuration in the resources directory. In fact, if you look at the sample Spring Cloud Config Application in Log4j you will see it does not include a configuration file in it.

How to configure log4j write to file only in spring boot?

Is that possible with one config?
log4j write log to file only when we run project as:java -jar project.jar &
log4j write log to file and print on console when we run project in eclipse
Maybe you can have a look at this answer, which seems to describe the same problem.
By using different Spring's profiles, you can switch from one configuration to another very easily, with multiple application.properties files which contains the property logging.config pointing on different log4j config files.
An other option is to use the default Spring Boot's logging mechanism, Logback which allows you to use the <springProfile> tag. See here for more details.

Spring Boot environment aware log4j properties

I try to use environment aware log4j properties files in Spring Boot application, for example, log4j.properties is used for development and log4j-uat.properties for UAT environment as showed in picture.
following Extern log4.properties file with Spring Boot, I added logging.config=log4j-uat.properties to application-uat.properties and get exception said cannot find that properties file.
following https://github.com/spring-projects/spring-boot/issues/1348, I added parameter when starting the application as java -jar logger-server.jar --spring.profiles.active=uat -Dlogging.config=classpath:log4j-uat.properties. However, it seems that the uat log4j properties is always overwritten by the default log4j properties.
Could anyone please advise me how to make Spring Boot pick up the log4j properties files for different environments?
Thanks,
Sean

spring restful web service logging

I implemented Spring RESTful Web Service using this tutorial: https://spring.io/guides/gs/rest-service/. I added dependencies for log4j in pom.xml and log4j.properties file in src/main/resources folder. Now I expect logging to be done into log file as stated inthe log4j.properties file instead of to STDOUT. However, logging goes only to STDOUT. How can I fix tis to print log messages to log file?
By default Spring Boot uses LogBack as logging framework.
If you want to configure Log4J, follow this configuration.
You need to exclude Spring Boot's logging module (containing LogBack dependency) and include Spring boot started for log4j.
BTW, log4j is ancient technology, consider moving to logback or log4j2

Resources