简体   繁体   中英

How to avoid printing logger main class name always, while printing logs?

I want to print the log in console. I am using java.util.logging.Logger class to print the log. Below is my code.

import java.text.MessageFormat;
import java.util.logging.Logger;
import com.practice.exception.InvalidOperationException;

public final class Log {

    private static Logger logger = Logger.getLogger(Log.class.getSimpleName());
    private Log() {
        throw new InvalidOperationException("Object creation is not allowed.");
    }
    public static void logInfo(String pattern, Object...arguments) {
        MessageFormat format = new MessageFormat(pattern);
        String message = format.format(arguments);
        logger.info(message);
    }
}

I am using Log.logInfo() method to print the log. My question is when I am printing the log it is always printing in below format. I do not want to print "Apr 20, 2019 1:06:14 PM com.practice.utils.Log logInfo" every time I print some logs. Is there any way we can achieve this?

Apr 20, 2019 1:06:14 PM com.practice.utils.Log logInfo
INFO: First text I have entered.
Apr 20, 2019 1:06:14 PM com.practice.utils.Log logInfo
INFO: Second text I have entered.

See a few suggestions here: https://www.logicbig.com/tutorials/core-java-tutorial/logging/customizing-default-format.html

The very easiest would be (from the link):

System.setProperty("java.util.logging.SimpleFormatter.format", "[%1$tF %1$tT] [%4$-7s] %5$s %n");

Or even simpler (to just print log level and message):

System.setProperty("java.util.logging.SimpleFormatter.format", "%4$s: %5$s %n");

can define format in configuration file. java.util.logging.SimpleFormatter.format= [%p] %t: %m

where message %m level/severity %p thread %t

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