Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Logging shows unwanted date and method while logging into a file

I am maintaining a log file generated by the java program (java.util.logging), which is working fine but it puts one extra line of info every time when it logs to the file.

Here is the code:

public static void main(String[] args) throws IOException {  

    Logger logger = Logger.getLogger("MyLog");  
    FileHandler fh;  

    try {  

        // This block configure the logger with handler and formatter  
        fh = new FileHandler("C:/temp/ABC/MyLogFile.log");  
        logger.addHandler(fh);
        SimpleFormatter formatter = new SimpleFormatter();  
        fh.setFormatter(formatter);  

        // the following statement is used to log any messages  
        logger.info("Welcome, App is opened at : " + new Date());  

    } catch (SecurityException e) {  
        e.printStackTrace();  
    } catch (IOException e) {  
        e.printStackTrace();  
    }  

    logger.info("It's running now");  
    logger.info("Took Following URLs from the user:"); 
}

output is like:

Feb 05, 2015 3:19:54 PM webservertracker.ImprovedImplementation main
INFO: Welcome, App is opened at : Thu Feb 05 15:19:54 IST 2015
Feb 05, 2015 3:20:49 PM webservertracker.LauncherUI$ButtonClickListener actionPerformed
INFO: It's running now
Feb 05, 2015 3:20:49 PM webservertracker.LauncherUI$ButtonClickListener actionPerformed
INFO: Took following URLS from the User:
Feb 05, 2015 3:20:49 PM webservertracker.LauncherUI$ButtonClickListener actionPerformed    

Readability is less here.

I need something like this :

INFO: Welcome, App is opened at : Thu Feb 05 15:19:54 IST 2015
INFO: It's running now
INFO: Took following URLS from the User:

How to achieve this. Do I need to customized it, if so then How?

Can anyone help me out.

like image 794
NawazSE Avatar asked Apr 17 '26 21:04

NawazSE


1 Answers

Using a properties file

You need to use the java.util.logging.SimpleFormatter.format key in logging.properties:

java.util.logging.SimpleFormatter.format=%4$s: %5$s%n

This yields a result like this one:

INFO: This is an informational message

Using a command line parameter

Another option is specifying this in a command line parameter:

-Djava.util.logging.SimpleFormatter.format="%4$s: %5$s%n"

Setting it up programmatically

If you are setting up the logger programmatically, you have to make your own subclass of java.util.logging.Formatter, because you the format field is a private variable in SimpleFormatter. For example, in your case, your formatter can be implemented as:

class MyFormatter extends Formatter {
    @Override
    public String format(LogRecord record) {
        StringBuilder builder = new StringBuilder();
        builder.append(record.getLevel() + ": ");
        builder.append(formatMessage(record));
        builder.append(System.lineSeparator());
        // pre-Java7: builder.append(System.getProperty('line.separator'));
        return builder.toString();
    }
}

then you can use MyFormatter to format you log messages. You can set up your console logging using this Java code:

ConsoleHandler handler = new ConsoleHandler();
logger.addHandler(handler);
Formatter formatter = new MyFormatter();  
handler.setFormatter(formatter);  
like image 108
meskobalazs Avatar answered Apr 19 '26 11:04

meskobalazs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!