We have several jobs that run concurrently that have to use the same config info for log4j. They are all dumping the logs into one file using the same appender. Is there a way to have each job dynamically name its log file so they stay seperate?
Thanks
Tom
Can you pass a Java system property for each job? If so, you can parameterize like this:
java -Dmy_var=somevalue my.job.Classname
And then in your log4j.properties:
log4j.appender.A.File=${my_var}/A.log
You could populate the Java system property with a value from the host's environment (for example) that would uniquely identify the instance of the job.
If the job names are known ahead of time, you could include the job name when you do the getLogger() call. You then can bind different appenders to different loggers, with separate file names (or other destinations).
If you cannot know the job name ahead of time, you could configure the logger at runtime instead of using a configuration file:
FileAppender appender = new FileAppender();
appender.setFileName(...);
appender.setLayout(...);
Logger logger = Logger.getLogger("com.company.job."+jobName);
logger.addAppender(appender);
We have something similar implemented in our system. We store the specific loggers in a HashMap and initialize appenders for each of them as needed.
Here's an example:
public class JobLogger {
private static Hashtable<String, Logger> m_loggers = new Hashtable<String, Logger>();
private static String m_filename = "..."; // Root log directory
public static synchronized void logMessage(String jobName, String message)
{
Logger l = getJobLogger(jobName);
l.info(message);
}
public static synchronized void logException(String jobName, Exception e)
{
Logger l = getJobLogger(partner);
l.info(e.getMessage(), e);
}
private static synchronized Logger getJobLogger(String jobName)
{
Logger logger = m_loggers.get(jobName);
if (logger == null) {
Layout layout = new PatternLayout("...");
logger = Logger.getLogger(jobName);
m_loggers.put(jobName, logger);
logger.setLevel(Level.INFO);
try {
File file = new File(m_filename);
file.mkdirs();
file = new File(m_filename + jobName + ".log");
FileAppender appender = new FileAppender(layout, file.getAbsolutePath(), false);
logger.removeAllAppenders();
logger.addAppender(appender);
}
catch (Exception e)
{ ... }
}
return logger;
}
}
Then to use this in your job you just have to use a one line entry like this:
JobLogger.logMessage(jobName, logMessage);
This will create one log file for each job name and drop it in its own file with that job name in whichever directory you specify.
You can fiddle with other types of appenders and such, as written it will continue appending until the JVM is restarted which may not work if you run the same job on a server that is always up, but this gives the general idea of how it can work.
You can have each job set NDC or MDC and then write an appender that varies the name based on the NDC or MDC value. Creating a new appender isn't too hard. There may also be a appender that will fit the bill in the log4j sandbox. Start looking in http://svn.apache.org/viewvc/logging/log4j/trunk/contribs/
You could write your own appender that makes up its own filename, perhaps using the [File.createTempFile](http://java.sun.com/j2se/1.5.0/docs/api/java/io/File.html#createTempFile(java.lang.String,%20java.lang.String)) method. If the FileAppender class was written correctly, you should be able to extend it—or RollingFileAppender—and override the getFile method to return one that you choose based on whatever new properties you would like to add.
Building on shadit's answer. If each job can be identified by which class' main method was started you can use the system property sun.java.command that contais the full name of the class started. For instance like this:
log4j.appender.LOGFILE.File=${sun.java.command}.log
I use it together with a TimestampFileAppender like this:
log4j.appender.LOGFILE=TimestampFileAppender
log4j.appender.LOGFILE.TimestampPattern=yyyy_MM_dd__HH_mm
log4j.appender.LOGFILE.File=${sun.java.command}_{timestamp}.log
This way when I'm developing in Eclipse I get a new log file for each new process that I run, identified by the classname of the class with the main method and the time it was started.
Tom you coud specify and appenders for each job. Let's that you have 2 jobs corresponding to two different java packages com.tom.firstbatch and com.tom.secondbatch, you would have something like this in log4j.xml :
<category name="com.tom.firstbatch">
<appender-ref ref="FIRST_APPENDER"/>
</category>
<category name="com.tom.secondtbatch">
<appender-ref ref="SECOND_APPENDER"/>
</category>
You could programmatically configure log4j when you initialize the job.
You can also set the log4j.properties file at runtime via a system property. From the manual:
Set the resource string variable to the value of the log4j.configuration system property. The preferred way to specify the default initialization file is through the log4j.configuration system property. In case the system property log4j.configuration is not defined, then set the string variable resource to its default value "log4j.properties".
Assuming you're running the jobs from different java commands, this will enable them to use different log4j.properties files and different filenames for each one.
Without specific knowledge of how your jobs are run it's difficult to say!
you may implement following:
A ThreadLocal holder for the identity of your job.
Extend FileAppender, your FileAppender has to keep a Map holding a QuietWriter for every job identity. In method subAppend, you get the identity of your job from the ThreadLocal, you look up (or create) the QuietWriter and write to it...
I may send you some code by mail if you wish...
log4j.logger.com.foo.admin=,AdminFileAppender
log4j.logger.com.foo.report=,ReportFileAppender
It's another way to do this task.. here com.foo.admin is the full package name
Related
I have created a wrapper for logging using log4j2. This is a customClass creating a object of :
logger = LogManager.getLogger(caller);
The problem is I am not getting correct class name in source when I am printing LogEvent.
For eg. If abc.java is logging log.info, I need to capture abc.java in source instead of customClass name.
You can't create the Logger just once in your wrapper object, because it is created with a specific class name (not the one you want).
So the quick and dirty way is to call LogManager.getLogger(caller).info(message) on every log call, and pass the calling class as the caller. Log4J keeps a hashtable of already created Loggers so it will not create a new Logger in case one already exists for the same caller class.
However, I don't think this is optimized enough for this use case, so I would probably roll my own caching in the wrapper object using a ConcurrentHashMap and computeIfAbsent( ), so that retrievals of existing Logger objects don't block others.
So my pattern would be:
Logger logger = loggersPerClass.get(callerClassName);
if (logger == null) {
logger = loggersPerClass.computeIfAbsent(callerClassName, k -> LogManager.getLogger(callerClass));
}
I'm using Hibernate and java.util.Logger class for logs in my project. I have a separate config file for both. I am able to switch between showing and not showing SQL queries by setting org.hibernate.SQL.level property to ALL in the log configuration file, but I can't figure out how to do it programatically (I want to handle this through run parameters but without having to use two seperate log configuration files).
So far I have tried setting this parameter in hibernate Configuration class, to no avail (properties are getting set, I double checked, but no queries show up).
Then I figured it must be handled by Logger class itself, but LogManager does not have any methods for setting a property. Browsing through the web guided me towards FileHandler class but I am able to set only the 'usual' log properties (like pattern, level, etc).
Does it mean I'm wrong in thinking I have to change the Logger class and it should in fact be set in hibernate's Configuration? If that's the case, why did it not work?
...I can't figure out how to do it programmatically (I want to handle this through run parameters but without having to use two separate log configuration files).
I would have assumed that the current situation solved the problem as log configurations are set using a run-time parameter. That said, the general approach is to get and store a strong reference to the logger and change the properties of that logger.
private static final Logger hardRef = Logger.getLogger("org.hibernate.SQL");
static {
if (traceSql()) {
hardRef.setLevel(Level.ALL);
}
}
private static boolean traceSql() {
return true; //#todo Add code.
}
In my project for every class having log messages like
Logger.getLogger("LoggingExample.class").info("Logging an INFO-level message");
how to specify common LogManager.getLogManager().readConfiguration at
one place instead of writing configuration for every file.
For java util logger it is default logging.properties taking from
java home directory i don't want to maintain properties file in java home directory i
want to maintain it with in project folder how to do this in java util logger
You do not need to create a logger every time. As java.util.Logger is thread safe, it is feasible to declare it only once and use it every time. Below is an example:
private static final Logger myLog = Logger.getLogger("loggerName");
myLog .warn("Text");
Best practice is to use one logger per class. And if it does not involve more efforts then, I would advise to use log4j framework.
You'll want to hold a strong reference to a logger so you don't lose your configuration changes. Here is a modified example taken from the Java Logging Overview:
public class Nose {
// Obtain a suitable logger.
private static final String CLASS_NAME = Nose.class.getName();
private static final Logger logger = Logger.getLogger(CLASS_NAME);
public static void main(String argv[]) {
// Log a FINE tracing message
logger.fine("doing stuff");
try{
Wombat.sneeze();
} catch (Exception ex) {
// Log the exception
logger.log(Level.WARNING, "trouble sneezing", ex);
}
logger.fine("done");
}
}
If you want to use a custom configuration file you can specify the 'java.util.logging.config.file' system property to point to path of your properties file. This is explained in detail in the LogManager documentation.
The Java logging configuration file lets me define the properties of a named logger, e.g.
name.heikoseeberger.heikotron.level = FINE
name.heikoseeberger.heikotron.handlers = java.util.logging.FileHandler
So far, so good. Now I would like to configure that particular FileHandler, e.g. with a specific output file. Unfortunately I only know how to configure the "global" FileHandler, which is already present in the configuration file:
java.util.logging.FileHandler.pattern = %h/java%u.log
java.util.logging.FileHandler.limit = 50000
java.util.logging.FileHandler.count = 1
java.util.logging.FileHandler.formatter = java.util.logging.XMLFormatter
I don't want to configure this one, but the instance which is associated with my custom Logger. I already tried the following, but without success:
name.heikoseeberger.heikotron.java.util.logging.FileHandler.pattern = %h/heikotron.log
name.heikoseeberger.heikotron.java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter
Is it possible at all to set the properties of specific FileHandler instances? If yes, how should these be identified/named?
This is done by using the config option described in the top level class documentation of the LogManger. Create a public named class with a public constructor and invoke all of the java calls you need to make to configure your handler. Then in your logging properties direct the LogManager to load your class you created to configure your handler. Otherwise you can to subclass file handler which will create a custom namespace to configure.
I do not think it possible. If you review the source code for the FileHandler you will soon determine that it uses the string "java.util.logging.FileHandler.pattern" to determine the pattern of the file to use for logging purposes
private void configure() {
LogManager manager = LogManager.getLogManager();
String cname = getClass().getName();
pattern = manager.getStringProperty(cname + ".pattern", "%h/java%u.log");
limit = manager.getIntProperty(cname + ".limit", 0);
//...
}
As such, the configuration that you are putting in the file is not even been taken into account by the Handler.
It appears to me that handlers are unaware of the existence of any particular logger (i.e. name.heikoseeberger.heikotron), they just know how to publish a given LogRecord.
As far as I can see, the handlers of a particular logger are created by the LogManager, by reflectively invoking their default constructor, as such, when a given handler is being created, it is unaware of for which particular logger it has been requested, that is why all their properties are set through their own class names and not through the logger's name.
If I understand right you are trying to write in different log files using java.util.logging package. This can't be done out of the box without extending it.
If you can't switch to another logging framework like Logback, check answer to
java util logging.properties: How to log to two different files and see if it fits your needs.
I'm using java.util.logging.Logger as the logging engine for my application. Each class uses it's own logger, i.e., each class has:
private final Logger logger = Logger.getLogger(this.getClass().getName());
I want to set a logging level for all my classes, and be able to change it (i.e., have the setting in one place). Is there a way to do this, other that using a global Level variable and manually set each logger to it?
One easy way is to use a logging properties file, by including this VM argument:
-Djava.util.logging.config.file="logging.properties"
where "logging.properties" is the path to a file containing logging configuration. For relative paths, the working directory of the process is significant.
In that file, include a line like this:
.level= INFO
This sets the global level, which can be overridden for specific handlers and loggers. For example, a specific logger's level can be overridden like this:
com.xyz.foo.level = SEVERE
You can get a template for a logging properties file from jre6\lib\logging.properties.
As Andy answered, in most cases you should use the property file and the VM argument, thus its independent from your code.
But if you want to go programatically for some reason (I myself had a good reason in one case) you can access the Handlers like this too:
Logger rootLogger = LogManager.getLogManager().getLogger("");
rootLogger.setLevel(Level.INFO);
for (Handler h : rootLogger.getHandlers()) {
h.setLevel(Level.INFO);
}
EDIT I added the setLevel to the root logger as searchengine27 pointed out in in his answer.
The Handlers are File or Console Handlers that you setup via the properties or programatically too.
Or change filters like this:
Logger rootLogger = LogManager.getLogManager().getLogger("");
rootLogger.setFilter(new Filter() {
#Override
public boolean isLoggable(LogRecord record) {
return "something".equals(record.getLoggerName());
}
});
So I don't entirely like all of the answers here, so I'm going to chime in.
Config file use
You're seeing a lot of answers in here telling you to use the config file because it is best practice. I want to explain better how to do this programatically, but before I do, I want to say that I can see where they are coming from, and in the mood of being objective, I will enlighten you a bit (especially because nobody says why its bad practice). I actually want to share what somebody said in a separate StackOverflow answer that is in relation to setting the logger level programatically (Why are the Level.FINE logging messages not showing?):
This is not recommended, for it would result in overriding the global configuration. Using this throughout your code base will result in a possibly unmanageable logger configuration.
On that note, I think Andy Thomas has a goodish answer related to not doing it non-programatically.
Programatically setting the Level
That being said, I want to go into a bit more detail about doing it programatically, because I think it has its uses.
Imagine a scenario where you are writing something with a command line interface and you have an option to specify the verbosity of your execution, or even where it goes to (as in dynamic log files). I may be mistaken, but you would probably not want to do this statically in a .conf file. Especially so if you don't want to make your userbase responsible for setting these things (for whatever arbitrary reason) in the config file. This comes at the expense of the above quote, however. Here is an example of how you can do it programatically, keeping all of the existing handlers to whatever level they are at already, and only FileHandler's assume the new level:
public static void setDebugLevel(Level newLvl) {
Logger rootLogger = LogManager.getLogManager().getLogger("");
Handler[] handlers = rootLogger.getHandlers();
rootLogger.setLevel(newLvl);
for (Handler h : handlers) {
if(h instanceof FileHandler)
h.setLevel(newLvl);
}
}
I wanted to expand on this, over the accepted answer for one reason in particular. When doing it programatically, you just want to make sure that you set the level for the logger and the handler(s). The way it works, is it will check to see if the request is too low for the logger, and if it is it will discard it. Then the handler(s) have the same check, so you will want to make sure both the loggers and handlers are set to the level you want it.
One-liner Java 8 approach to morja's answer:
Arrays.stream(LogManager.getLogManager().getLogger("").getHandlers()).forEach(h -> h.setLevel(Level.INFO));
JUL(java.util.logging) is java default logger within jvm.
Java Logging Technology--see Overview
So you may find that there is a default config file already in C:\Program Files\Java\jre1.8.0_221\lib\logging.properties
I recommend that you create a new config file under your project to override the default setting, do as following:
config file name, logging.properties, all log level can be found in Class Level
handlers= java.util.logging.ConsoleHandler
.level= INFO
java.util.logging.ConsoleHandler.level = ALL
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
java.util.logging.SimpleFormatter.format= [%1$tF %1$tT] [%4$-7s] %5$s %n
# your specific logger level
com.xyz.foo.level = SEVERE
Then add jvm option to enable the config
java -Djava.util.logging.config.file="logging.properties" -Duser.country=CN -Duser.language=en
user.country and user.language may have an effect on log message localization