Is it possible to state something like - "override file = /log4j.properties"
From within the log4j.properties and to link to the actual log4j config?
Thanks!
It is not possible. However, you can configure the system property log4j.configuration specifying the path of a configuration file. Add the following parameter when you create the JVM:
java -Dlog4j.configuration=path/to/log4j-test.properties AwesomeApp
Related
My Java application uses SLF4J which I have configured to use the SimpleLogger implementation and redirect log messages to a file. That is working fine.
How do I subsequently change the name of the log file?
I have tried changing the LOG_FILE_KEY property but it seems to have no effect. The log messages continue to be output to the original log file.
This is what I've done:
System.setProperty(org.slf4j.impl.SimpleLogger.LOG_FILE_KEY, Paths.get("new-filename.txt").toString());
To set the file path to your liking, one simply has to set the following property, either in a file named simplelogger.properties on the classpath (e.g. by placing such a file under the resources directory) or through a -D JVM startup option.
The property name and syntax is simply:
org.slf4j.simpleLogger.logFile=your-file-path
See in this related answer for examples of providing a property through either of the two mentioned methods.
I think I figured out the answer with the help of this answer and looking at the source code for org.slf4j.impl.SimpleLogger.
The answer is you can't create a new log file as the logger properties are loaded only once - upon construction of the first logger instance. Subsequent loggers will use the same properties as the first one.
My code has a dependency on abc.jar file which internally uses log4j.jar and needs a configuration file. This configuration is used to set some of the required fields like ssl protocol, cert validate, port and also has a parameter, named debug, which is read by log4j.properties file (inside the abc.jar) to set the level of the logging.
The value of the debug parameter is restricted to all or none. Also, the abc.jar file has its own (custom) Logger class. So whenever I create an object of org.apache.log4j.Logger class, it creates object of the (custom) Logger class inside the abc.jar file. I guess because of this Log4j is unable to read my log4j.properties file.
I want to use my own log4j.properties file so that I can specify the level of logging. How can I do this?
You can set the property log4j.configurationFile to set the path of your config file you want to use.
On command line, you can write -Dlog4j.configurationFile=<path>
Doing that disables automatic configuration and use your manually set file for configuration
Have you seen this post? Uses PropertyConfigurator to set the log4j path in any init() like method.
Where to put the logback.xml file in Tomcat when we want to have it configurable?
And how to make it accessible for the Java application(s) running inside?
You typically want to have logback.xml on the classpath. Per the Logback FAQ:
For web-applications, configuration files can be placed directly under WEB-INF/classes/.
You therefore need to put it in:
/webapps/your-app/WEB-INF/classes/
Logback has some conventions for where it looks for it. Those are documented here.
Logback tries to find a file called logback.groovy in the classpath.
If no such file is found, logback tries to find a file called logback-test.xml in the classpath.
If no such file is found, it checks for the file logback.xml in the classpath..
If neither file is found, logback configures itself automatically using the BasicConfigurator which will cause logging output to be
directed to the console.
But you can also tell it where to find the file.
You may specify the location of the default configuration file with a
system property named "logback.configurationFile". The value of this
property can be a URL, a resource on the class path or a path to a
file external to the application.
java -Dlogback.configurationFile=/path/to/config.xml chapters.configuration.MyApp1
Note that the file extension must be ".xml" or ".groovy". Other
extensions are ignored. Explicitly registering a status listener may
help debugging issues locating the configuration file.
i would like to turn of my debug statements at runtime in log4j2. According to the documentation we can do this. I kept my log4j.xml file in default package and then made jar out of it. Since I cannot modify jar i specified xml file using Dlog4j.configuration=/path/to/log4j.xml. However,this is not working. I Tried everything using file:// uri to all the combination, still it is not picking the xml.
The system property to specify the config file location is different from log4j-1.x.
In log4j2, the property is "log4j.configurationFile".
So if the config is in a jar file you would use:
-Dlog4j.configurationFile=jar:file:///C:/path/to/jarfile.jar!/path/to/log4j2.xml
(I assume you have the monitorInterval set in your configuration as documented here: http://logging.apache.org/log4j/2.x/manual/configuration.html#AutomaticReconfiguration)
Following must be added to configuration file:
<Configuration monitorInterval="60" >
Is it possible to use something other than the file names log4j.xml or log4j.properties to configure log4j logging in a Java web application?
I want to load a log4j.xml file from a different location on the file path (not in my classpath). Is that possible in a web application using say, JBoss or Tomcat?
You can use PropertyConfigurator.Call configure with file you wanted
Use -Dlog4j.configuration=path/to/your/file.xml startup parameter to specify where your configuration file is. It's the recommended practice anyway:
The preferred way to specify the default initialization file is
through the log4j.configuration system property.
(log4j manual)