How to reconfigure log4j2 at runtime with new xml file - java

I'm using log4j2 in a Web Application (servlet 3.0 API)
Log4j2 is configuring itself automatically when the application is started using a log4j2.xml file inside the WEB-INF directory.
When the context is started I execute some logic that produce some logs, and all the log produced are working accordingly to the rules defined in the log4j2.xml file.
At the end of my initialization logic I call a service that gives to me one or more log4j2 xml configuration files (for example, config1.xml, config2.xml and config3.xml).
Now I want to 'reconfigure' log4j using my initial log4j2.xml file together with all the new configuration files.
I cannot find a way to do this; I can find only a way to add programmatically new Appenders, new Loggers and so on;
I'd like to tell log4j2 "hey, here are some new configuration files; please, parse them and update your configuration accordingly"
There is a way to do this?
thanks

Thanks to Vionixt comment to my question, I solved the problem.
The code I used was:
System.setProperty("log4j.configurationFile", "<configFile1>,<configFile2>,<configFile3>");
LoggerContext.getContext(false).reconfigure();
where LoggerContext is
org.apache.logging.log4j.core.LoggingContext
After reconfiguration, all the new Logging rules were available and also used!
thank you so much,
cheers

Related

Externalized configuration setting out of fat-JAR file

I am looking for a way to purely externalize some configuration settings in Spring boot application. For example: when double clicked on the fat-JAR file then it loads configuration from that, say myConfig.config, file which is in the same folder in which the fat-JAR file is. Then read the configuration from there and deploy the web-app. One use case is reading the port number from the config file and start the web-app on port number specified in the config file. If port number needs to be changed then only config file needs to be updated and restart the web-app.
I know that it is possible in .NET. I tried this link[1], but it is specifying config file in command line. Also, the #PropertySource can be used but again it winds up being in fat-JAR. There is Spring Cloud Config as well but I think that it would be overkill for small application. There are lots of tutorials available but they use one of the above mentioned method.
So, Is there any way to achieve that?
If yes, then what are the steps/link for that?
[1] Springboot externalizing log4j configuration
All you need to do is place an application.properties file at the same level as your jar. Spring Boot will find and use the application.properties w/o anything extra.

Is there a way to externalize Spring [Boot] app's log4j2 config on Tomcat?

Our Spring Boot application uses log4j2 for logging, but the admins of the server we are going to deploy it to require that:
the logging be fully configurable on their end, i.e., there must be a log4j2.xml file that they can edit to adjust logging formats, files, levels etc., and
The configuration must not be lost or overridden when a new version of the app is deployed (the upload war -> stop tomcat -> delete webapp folder -> start Tomcat) process will be automated.
Ideally, the path to log4j2.xml should be set in the webapps context .xml file — since that's where the DB connection config is. But I can't seem to find a way to make this work. All search results talk about using META-INF/web.xml or application.properties files (not an option, since they get overwritten on deploy), and the only SO question I could find (Spring application without web.xml log4j configuration) did not work for me ("No Configuration was provided") exception on startup.
Set the environment variable logging.config to be the location of your log4j2.xml file.
It's also recommended that you call it log4j2-spring.xml
See https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-logging.html
section "Custom log configuration"

Log4j2 Configuration Logs to File

I am new to Log4j2. I am trying to configure a logging system using slf4j and log4j2.
I have few doubts.
While configuring log4j2, I noticed configuration logs form log4j2 is printed in console. I want to print those logs in file instead of console.
Is there any way to use log4j2's appender .
PS : I noted that log4j2 uses StatusLogger to print its configuration logs. Is there any way to make it use a file.
The simplest way to do this is to redirect the process output to a file:
java -cp lib/*.jar com.mypackage.MyClass > output.txt
There are some properties you may be able to use to tell Log4j to write status logs to a file (probably org.apache.logging.logj.simplelog.logFile), but at best this will work after Log4j 2 is fully initialized. I assume you are interested in the internal status log statements that are emitted during Log4j 2 startup and initialization. For that, I'd recommend redirecting to a file.

Grails log configuration is hijacking the external log configuration, how to stop it?

I have a web application consisting of severals modules. All the modules are packaged together in one single ear.
One of them is a brand new groovy app, while others are more old school. This new grails app is not under my responsibility.
Notice that grails is not using any log4j.[properties|xml] file, but it as its own DSL which interact directly with log4j at runtime. This configuration is located inside a config.groovy script, packaged with the application.
Log4j is configured using an external file and the -Dlog4j.configuration option for the JVM.
The problem is the grail configuration is containing a very liberal config:
- set the root level to info
- add a console appender
The result is that the external configuration is hijacked by grails:
now there are two console appender (logging twice the same info) and lots of useless info data are logged.
Is there another solution than a programmatic approach, to tell grails to stop being rude ?
You could just turn off the grails logging so it uses the external logger
http://blog.saddey.net/2010/02/07/grails-how-to-use-native-server-logging-configuration-eg-tomcat-glassfish-jboss/

log4j configuration with Java Web Start

Our Java WebStart application does not include a log4j configuration file; it simply sets up some hardcoded loggers and appenders.
I would like individual clients to be able to drop a log4j.properties file in somewhere and set up their own custom logging in troubleshooting situations. I could bundle a log4j.properties file into one of the jars of our application somewhere and that would allow configuration, but then the configuration would be the same for each client instead of only affecting the client that I want to troubleshoot. Plus, I wouldn't be able to change settings on the fly.
Is there a way I can hijack the log4j initialization procedure to use a per-client configuration file?
The basic problem here, is that Java Web Start severely restricts the access to the machine.
You should be able to do this, if you are running a signed application AND the user allows you full access to the machine. If not, you cannot do this with log4j with the default mechanism.
You may want to write your own configurator which reads from the file system using the Java WebStart API an then feeds that to log4j, but it will require some elbow grease.
You could use the PersistenceService to store the log4j configuration on the local user's machine (works without signing), or at least store a flag on whether to load a special config or not at startup of your web start application.
There is also a FileOpenService with which the end-user could open a local log4j.xml file to re-configure the logging facility on the fly. That way, the user has the control over the configuration and he has the control when and where to apply it.
Your app code which uses the FileOpenService to get the stream to the log4j configuration file can then use the DOMConfigurator.configure(InputStream) to reconfigure log4j.

Categories

Resources