繁体   English   中英

在打印日志时如何避免始终打印记录器主类名?

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

我想打印登录控制台。 我正在使用java.util.logging.Logger类来打印日志。 以下是我的代码。

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);
    }
}

我正在使用Log.logInfo()方法来打印日志。 我的问题是,当我打印日志时,它总是以下面的格式打印。 每次打印一些日志时我都不想打印“2019年4月20日下午1:06:14 com.practice.utils.Log logInfo” 有什么方法可以实现这个目标吗?

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.

请在此处查看一些建议: https//www.logicbig.com/tutorials/core-java-tutorial/logging/customizing-default-format.html

最简单的是(来自链接):

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

甚至更简单(只打印日志级别和消息):

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

可以在配置文件中定义格式。 java.util.logging.SimpleFormatter.format = [%p]%t:%m

消息%m level / severity%p thread%t

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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