Spring Log4j2 xml file location - spring

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.

Related

Spring cloud config server not honoring logging.file property and not logging external file

I'm using spring-boot-starter 2.3.3.RELEASE version. I'm running my spring cloud configuration server in native profile (looking for configuration files in file system). I added
logging.file = /var/log/config.log in application.properties file. But my application is not logging logs to this file.(All other microservices are logging to this location). Am I missing any additional settings for Spring cloud config server? Thank you so much for your help.
In the spring boot 2.3.3 RELEASE documentation the logging properties that specify where the location should be is indicated using the property:
logging.file.path={path}
The documentation:
https://docs.spring.io/spring-boot/docs/2.3.3.RELEASE/reference/htmlsingle/#boot-features-logging-file-output
This modification from logging.path to logging.file.path appears as a deprecation in Spring Boot 2.2:
https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.2-Release-Notes#deprecations-in-spring-boot-22
One way to do it with spring-boot application is, setting it from command line argument as default it will dump every thing on console.
logging.file=logs/test.log
Old school but far effective, save path in application.properties
logging.file=logs/test.log

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 - logging configuration from JBoss

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.

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, log4j and JBoss4

I'm running JBoss 4.2.3.GA, and have a web app that uses Spring and Log4j. I've set up Spring's Log4jConfigurer, but am having a problem since JBoss's log4j file is not on the classpath. It's at jboss.home/server/defaul/conf/jboss-log4j.xml. So, without hard-coding the path in Log4jConfigurer, how can I get Spring to use JBoss's log4j file?
You don't generally modify JBoss's log4j config via the applications, that's a route to madness. JBoss uses a shared configuration amongst all apps.
You can, however, programmatically alter log4j's runtime configuration, such as adding custom appenders or loggers. This isn't what Spring's Log4jConfigurer is for, however - you use that to configure a standalone log4j config.
Do answer your question: JBoss usually sets additional system properties that point to the server directory and configuration directories. You should be able to access these from your application or let them be resolved from the spring contexts.
In your case, it should be ok to access ${jboss.server.home.dir}/conf/jboss-log4j.xml
As skaffman correctly pointed out in a comment, there is no need to configure Spring to read JBoss' log4j configuration unless you have otherwise adapted something in the packaging. You will only have to take care that no log4j.[properties,xml] lies somewhere in the root of the classpath or that a component tries to configure Log4J "manually".
Hope this could help,
Kariem

Resources