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.
Related
I'm using the log4j2 library to manage the logging process.
I created a configuration file named log4j2.xml containing the Appenders and Loggers configurations. Then, I defined a Logger in each class
private static Logger my_logger = LogManager.getLogger(my_class);
I did not specify anywhere the name of the conf file, so I think that the library implicitly get and read it.
Now, I need to provide my application in the form of a jar file, so I need to make the config file available so that the user can modify and configure it.
In my case, I suggest to create a XXX folder at the level of the jar file, containing all the configuration files used by my app.
My question is how can I say to the app "get XXX/log4j2.xml" rather than the xml contained into the jar.
that config file must be located in the class path, if you want the app to read the configuration from any other location then you need to specify that using
PropertyConfigurator.configure("/myPath/log4j.properties");
Make any folder and put your property or xml file in that. In order to read the property file you can do something like this:
Properties objProperties = new Properties();
<your-class>.class.getClassLoader().getResource("folder/log4j.properties");
objProperties.load(isFile);
or, Also this:
InputStream ist = Thread.currentThread().getContextClassLoader().getResourceAsStream("folder/log4j.properties");
In case of java web application please use the link
I had a similar task a few weeks ago.
I solved it this way:
Store a template of your log4j2.xml inside your jar files resource folder
When running your application, check for a file named log4j2.xml in the jar files current directory
If there is one, use that to create your logger
If not, copy your template from within your jar to the jar files directory and then use that to create your logger.
Cheers
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.
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
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.
My procedure for using log4j is this:
put a .properties file somewhere in a project folder
in an initialization method, that runs only once, invoke PropertyConfigurator.configure("path_to_file")
in every method we need to use logger we define a static logger variable and simply invoke getLogger(class)
Is this correct?
What happens if the initialization module is not defined? Where can I put the "log4j.properties" file such that I don't have to invoke propertyconfigurator.configure at all? If that's not possible, is it OK to invoke PropertyConfigurator.configure("path_to_file") in every method that uses a logger?
If you put it somewhere in the classpath, Log4J will automatically load it. A typical place for it is the source (or resource) root directory. That way it can get copied into the root directory of your jar too (if you create one from your class files).
The exact details for this depend on what build system you use. E.g. in Maven the convention is to put it in src/main/resources.
The default initialization procedure for log4j is documented in this section of the log4j tutorial. This explains in detail the steps that log4j takes to locate the logging configuration.
The simplest way to configure log4j is to put a the properties file somewhere that allows it to be found by the class loader using the name "/log4j.properties". There are other approaches that you can use as well that involve setting system properties, loading the properties file programmatically, or even instantiating the Loggers, Appenders and so on via the log4j APIs. (Not that the latter is a good idea ...)
(Putting the log4j properties file in "src/main/resources" in a Maven project is just one of a number of possible ways to get the file into your application's classpath root.)