logback xml configuration for web application - java

I am using logback SLF4J for logging the debug/error statements. Could you please let me know how to use single logback.xml configuration file for multiple environments (dev/qa/prod)? Right now, i am editing xml file for each environment to specify dbname...I appreciate your help.

Couple of options (most of them documented here)
Use properties in the log configuration which are set externally (either java properties or OS environment variables)
Use JNDI settings (creating db datasources is pretty common)
Generate a logback.xml file as part of the deployment process
JMX configurator which allows you to reload the configuration from a named file
Package a WAR file for each environment (don't really recommend this included for completeness)

Related

How to read log4j properties file out of classpath

I am facing a task to put log4j.properties configuration file to the JBoss server instead of putting this configuration to the project as stated in official JBoss documentation here: https://developer.jboss.org/thread/231514
If I got this right, log4j logger finds configuration within classpath automatically, but what is the way (if there is any) of loading this file from server directory (e.g. wildfly/modules/x/y/log4j.properties)?
You can use -Dlog4j.configuration=file:[path-to-your-external-file] while starting the server.
This is the answer I was looking for.

get location for log4j config stored outside application

We are doing a maven project which is using datanucleus which logs with log4j so can't use logback. Where can I put the log4j config file outside the application so that the different VM environments have access to it. How can I get the location of the config file without hard coding the classpath?
You could specify the path in log4j.properties.
log4j.appender.file.File=path_to_log_file
For details, please check this example.

How to read Logback configuration file from path outside the war file?

I have a requirement where I want to place all logback configuration outside the war file.
Presently I have placed my configuration file (logback.xml) on the classpath.
I am using the JBOSS EAP web application server, kindly suggest how to achieve the same.
Go into the jboss startup/run script and add this option:
java -Dlogback.configurationFile=/path/to/config.xml chapters.configuration.MyApp1
see more at http://logback.qos.ch/manual/configuration.html
You could also use -Dlogging.config=/path/to/logback.xml
This is really handy with java based microservices to provide logging XML externally.

logback configuration files per jar

I would like to deliver jar files with its own logback logging configuration.
The common way to configure logback is with the default file logback.xml that logback library reads from the classpath's root (works for application servers or not).
You could include another files from the main logback config file (didn't try it), but I don't know which jars will be in the classpath and which ones require the log configuration.
Plus, the jars could be used in a command line application or application servers (shared library or not).
I thought that maybe I could get the filepath to the jar and check if there is a config file there, and try to read the configuration programmatically and load it with JoranConfigurator.
// This is the way I find to get the path to the jar
String path = ClassThatWantsALogger.class.getProtectionDomain().getCodeSource()
.getLocation().getPath();
String decodedPath = URLDecoder.decode(path, "UTF-8");// the path to the jar
But this approach could fail because depends on the security restrictions, maybe fails in Linux or in application servers. It's a hard approach for a problem that probably has a better solution.
Do you imagine a way to manage that jars could have its own logback config file that works for any environment?
I will cite myself to answer your question: Logging configuration is the concern (separation of concerns) of the client application. It's his decision to make where, how and what will be logged. You shall not impose anything on it by our library.

Can I use log4j in a web app with a file other than log4j.xml or log4j.properties?

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)

Categories