简体   繁体   中英

java.util.logging custom formatter not working as expected

I am using Java 6. I have created a custom formatter which creates only the time and the message, but it always prints null class name and method name and the log level besides the time and the message in two lines:

Mar 8, 2015 6:48:09 PM null null

ALL: This is a message

Also, the log files generated are in the following format, where I expected it to start with xxx.log.0 first, once it is full, then xxx.log.1, but it generated all 10 files at the same time.

xxxx.log.0.9

xxxx.log.0.8

xxxx.log.0.7

xxxx.log.0.6

xxxx.log.0.5

xxxx.log.0.4

xxxx.log.0.3

Can someone let me know how I can log only the time and the message and how I correct the log file extensions? Much appreciated!

public class MyFormatter extends Formatter {
    public String format(LogRecord record) 
    {
        String recordStr = new Date() + " " + formatMessage(record)
        return recordStr;
    }
}

The code which uses MyFormatter:

Logger fileLogger = Logger.getLogger(xxxx.class.getPackage().getName());
FileHandler handler = new FileHandler(pattern, limit, numLogFiles, true);
MyFormatter formatter = new MyFormatter();       
handler.setFormatter(formatter);                    
fileLogger.addHandler(handler);
Level logLevel = java.util.logging.Level.ALL;
fileLogger.log(new LogRecord(logLevel, "This is a message"));    

I am using Java 6. I have created a custom formatter which creates only the time and the message, but it always prints null class name and method name and the log level besides the time and the message in two lines:

That output is from the java.util.logging.SimpleFormatter which has support for printing just the date and message. The following pattern can be used to change the SimpleFormatter to work like your custom formatter:

-Djava.util.logging.SimpleFormatter.format="[%1$tc] %5$s%n"

The code that uses your formatter is not setting the method name , class name , or logger name . Calling the Logger.log(Level,String) method will compute the call site for you.

        private static final Logger fileLogger = Logger.getLogger(xxxx.class.getPackage().getName());

        private static void initLogging() throws IOException {
            FileHandler handler = new FileHandler(pattern, limit, numLogFiles, true);
            MyFormatter formatter = new MyFormatter();       
            handler.setFormatter(formatter);                    
            fileLogger.addHandler(handler);
            fileLogger.log(Level.ALL, "This is a message");
        }

Not sure if it is intended but, you should use the event time of the LogRecord and not the current time.

Also, the log files generated are in the following format, where I expected it to start with xxx.log.0 first, once it is full, then xxx.log.1, but it generated all 10 files at the same time.

For every FileHandler you create, you have to be sure it is closed by your code before you create a new one with the same pattern. By default the LogManager will close any attached handlers on shutdown. You also have to make sure that you hold a strong reference to the logger that holds the attached handler. Method local references like what is used in your example doesn't count.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM