How to read level changes from log4j.xml when application is running - log4j

I am running a executable jar file, which logs using log4j.xml(version 1.2).
Whenever i change the log level in the xml file, i have to restart the java application for the new log level to reflect.
Can i add some configuration in the log4j.xml so that new log level will be taken into account without application restart?
Thanks,
-Venkat

Log4j 1.x has reached end of life on August 5, 2015. So I would like to recommend you to use log4j 2.x which supports this with the monitorInterval configuration.
However according to the log4j 1.x docs here it does support log level changes runtime.
How can I change log behavior at runtime?
Log behavior can be set using configuration files which are parsed at
runtime. Using configuration files the programmer can define loggers
and set their levels.
The PropertyConfigurator defines a particular format of a
configuration file. See also the examples/Sort.java example and
associated configuration files.
Configuration files can be specified in XML. See log4j.dtd and
org.log4j.xml.DOMConfigurator for more details.
See the various Layout and Appender components for specific
configuration options.
In addition to configuration files, the user may disable all messages
belonging to a set of levels. See next item.

Related

Hi,I am using log4j in my system.Is there a tag which automatically recognises change of level,so that i dont require to start stop my server

I am using log4j.xml to store my logs.
At runtime I need to change the level at production, and want to execute it by making changes in log4j.xml
I have used maxinterval but it does not reflect once I change my log levels.
I need to find a way to change my logger level at runtime at log4j.aml level.
Is it possible?

How to get velocity.log file using log4j.properties file. i don't want to involve velocity.properties file

Now my velocity.log and velocity.log.1 are at :installed software eclipse Kepler\eclipse-64\eclipse
i don't know how these files are created here.
By default, Velocity log files are created in the current working directory.
To change this behavior, you'll have to somehow configure Velocity. If you don't want to mess with velocity.properties, then you can configure Velocity using Java, as described in this documentation section which explains how to have Velocity log to an existing Log4j logger.

How do I override default nlog configuration?

We ship a framework assembly for logging which internally uses nlog. We also ship an embedded nlog configuration as a resource in our assembly and read this on startup (a static constructor within our wrapper which wraps over the nlog wrapper using the XmlLoggingConfiguration class).
We would now like developers to be able to specify/override additional configuration via an external file for their own loggers (using the same nlog configuration file format).
Can we:
Refer include files in our embedded nlog configuration file? How is the path of the include file resolved? If we do this, does the content of the include file override the content of the embedded nlog configuration we read on startup?
Can developers use the nlog configuration section? Does the content of this section override the default configuration we have specified pro grammatically?
Any other options?
How is the path of the include file resolved?
You can use in the included file name:
layout renderers such as ${baseDir},
nlog variables.
If the resulting filename is not absolute, it is combined with the directory, where the original NLog configuration was located, but this may be null in your case, so you are limited to first 2 options.
does the content of the include file override the content of the embedded nlog configuration?
No, this content is added to the content of the embedded configuration.
Does the content of nlog configuration section override the default configuration we have specified programmatically?
When you set the configuration programatically, you override any configuration that was set before and also disable the automatic configuration discovery, including one from application configuration file or NLog.config.
But you can check whether the configuration is not null first time before you set the embedded value. This will trigger the automatic discovery, and in case if any logging configuration is found, the returned value would be non-null.

logging 2 web apps to 2 different files

I have 2 web applications built from the same source tree within tomcat, which each use a PropertyConfigurator loading their respective /webapp/WEB-INF/classes/log4j.properties.
Currently tomcat is configured to use one console appender for the whole container, and both app's log4js just write to the console:
log4j.rootCategory=info, A1
log4j.appender.A1=org.apache.log4j.ConsoleAppender
This means I end up with one big catalina.out for the whole container.
I would like to configure each of the applications log4js so that they append to their own separate rolling files.
I'm aware I can use system properties substitution within log4j.properties however isn't system properties shared across the VM and therefore not threadsafe between the 2 applications within the container?
Can someone suggest a tidy solution which allows me to configure the 2 applications to log to separate files, preferably with the application's context name within the log's file name.
thanks, p.
Where is your log4j.jar and commons-logging.jar placed in tomcat directory? And which version of tomcat you are using?
If you have one copy of these jar inside common/lib than surely catalina.log would be used by all logging since tomcat's log4j.properties file is only the one getting configured for both server and webapps under it.
Assuming this scenario holds valid in your case, copy log4j.jar and common-logging.jar under WEB-INF/lib for both web applications.
For using application name within log's file name, since separate log4j.properties file is used for each application, name your logfiles as you like in log4j.properties file against FileAppender or RollingFileAppender.

Log4J and Java Web Start, how to alternate between different configurations?

i'm starting with Log4J and i want to have a default log4j.properties in our Java Web Start distributed application, which only logs errors and important events.
But if something was wrong in one client i want to have a more detailed log, the way to do this is to define an alternate log4j configuration file in this client. This can be done by specifiying the alternate config file with the log4j.configuration system property.
but... How can i define an system property for this particular client in a java web start launched application? (i know that i can define theese propeties in the .jnlp file, but this affects all clients).
Our users work in windows environment but they often have a restricted permissions computer and they can't acces My Pc->Properties-->Advanced Options-->Enviroment Variables (i'm in a spanish configured computer i don't know the exact names in english).
Can you access to a defined directory on the client disk ?
If you can, you can define a convention : if no configuration file is found in the directory, the default config is used. Else, the specific configuration file is loaded.
You can do that with the PropertyConfigurator class of Log4J :
File log4jConfigFile == new File(conventionLocation);
if(log4jConfigFile.isFile() && log4jConfigFile.exists()) {
PropertyConfigurator.configure(conventionLocation);
} else {
PropertyConfigurator.configure(defaultEmbeddedJarLocation);
}
First off if you are using applets you should use an appender that can write to a remote location so you can actually see the errors without being physically on the local machine that the applet is running. Appender Types. Next, you need to create an appender with a threshold of whatever level you are logging the normal "access" type messages. Set the layout to whatever you desire. Then create another appender with a level of at least the level you log "errors" at as well as its own format that suits your needs as being "more detailed". That way when your code calls an error message it will use that different layout. Log4j is fairly complex but not impossible to understand. Look at the documentation at The log4j site and get your feet wet on some simple logging. After that you should be able to modify the code to get what you desire.

Resources