Java Logging - how to redirect output to a custom log file for a logger?
Asked Answered
C

3

22

I have a question about the jdk logging configuration. I have a EJB (deployed into glassfish) which uses JDK Logging to output messages. Therefor I use a named logger with a code like this:

private static Logger logger = Logger.getLogger("org.imixs.workflow");
.....
       logger.fine(" some info...");
....

I know that I can configure the loglevel for my logger by adding the following line into the logging.properties File from Glassfish:

.....
org.imixs.workflow.level=FINE

But how can I specify the output file for my logger? I would like to put all messages from the logger named 'org.imixs.workflow' into a separate file. Is this possible?

Thanks for any help

Curtate answered 23/11, 2011 at 20:52 Comment(0)
R
34

the slightly confusing pattern property of FileHandler can be used for this

handlers=java.util.logging.FileHandler
# Default global logging level. 
.level=INFO

#logging level for the foo.bar package
foo.bar.level=CONFIG 
java.util.logging.FileHandler.pattern=%h/java%u.log

A pattern consists of a string that includes the following special components that will be replaced at runtime:

"/" the local pathname separator

"%t" the system temporary directory

"%h" the value of the "user.home" system property

"%g" the generation number to distinguish rotated logs

"%u" a unique number to resolve conflicts

"%%" translates to a single percent sign "%"

If you want to log to multiple files then you can do it by set up multiple handlers for multiple named loggers

#FileHandler for file1    
java.util.logging.FileHandler.pattern = logging_property_test.log
java.util.logging.FileHandler.limit = 50000
java.util.logging.FileHandler.count = 1
java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter

#FileHandler for file2
logging.FileHandler2.pattern = logging_property_test2.log
logging.FileHandler2.limit = 50000
FileHandler.count = 1
logging.FileHandler2.formatter = java.util.logging.SimpleFormatter


#setting handler for logger1
logging.PropertyTestingLogger.handlers=java.util.logging.FileHandler

#setting handler for logger2
logging.PropertyTestingLogger2.handlers=logging.FileHandler2

as you can see the trick is that there is a logging.FileHandler2 that is a custom class and does nothing but extends the FileHandler

package logging;

import java.io.IOException;
import java.util.logging.FileHandler;

public class FileHandler2 extends FileHandler {

    public FileHandler2() throws IOException, SecurityException {
        super();
    }

}

Background : unfortunately the creators of Java were not expecting anyone to log into multiple files. If you look at the source of java.util.logging.FileHandler you will find, that the pattern property is loaded by the class name :

public class FileHandler extends StreamHandler {

   private String pattern;

   private void configure() {

        String cname = getClass().getName();
        pattern = manager.getStringProperty(cname + ".pattern", "%h/java%u.log");
Rousing answered 23/11, 2011 at 21:28 Comment(12)
But how can I use this patterns to specify that it only takes affect for my named logger (org.imixs.workflow)? I don't want to redirect general server messages into my application specific log file.Curtate
I updated my post accordingly. In the example the default log level is set to info, but the foo.bar package to configRousing
yes - but my question is: is it possible to set a separate output file for the logger 'foo.bar'. I know that you can specify a separate log level for each package. But how to define separate log files?Curtate
honestly I dont know. All the examples indicate that it is possible with code FileHandler fileHandler = new FileHandler("myLogFile"); logger.addHandler(fileHandler); But there is no mentioning of being possible with configuration. On the other hand it is very easy to do that with Log4J using property file. I think Log4j is more flexible and more widely used then the logging APIRousing
this question was bugging me so I did a little research and updated the post with the resultRousing
Thanks for your comment. Yes I think this can solve my problem :-)Curtate
What is PropertyTestingLogger here? Where is it defined?Circinus
I tried setting file name to be "name%u.log". Running app in Eclipse results in "name0.log" being overwritten. Am I missing something?Whoreson
I'd like to do exactly what Ralph asked and I don't see how this answers Ralph's question.Dubuffet
Is this answer still valid? Can we use multiple file handlers without coding ? I am using java util logger.Moffatt
@JSR if you look into java.util.logger.FileHandler.configure() in Java 8 it still has the same way to define logfile name as I posted above, so I suppose it works the same way. Use Log4J or Logback :)Rousing
@PeterSzanto Thanks. Due to some product restriction I cant use log4j, logback. kind of stuck with java util logger.Moffatt
M
13

You should use a FileHandler.

FileHandler fileHandler = new FileHandler("myLogFile");
logger.addHandler(fileHandler);

See Example Depot for some clear examples configuring loggers.

And you might find these answers useful, for setting from properties file.

Melgar answered 23/11, 2011 at 21:15 Comment(1)
but is it also possible to do this through the configuration file - so that I did not need to specify a hard coded file name for the logfile in my source code?Curtate
S
6

With java.util.logging, it IS NOT possible simply by modifying the configuration file. Unfortunately, you must extend FileHandler and configure that handler to filter what you want logged.

Snuggle answered 12/1, 2016 at 16:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.