java.util.logging:如何抑制日期线

发布于 2024-09-03 22:03:00 字数 441 浏览 4 评论 0原文

在 java.util.logging 中使用默认记录器时,我试图在日志记录期间抑制日期行的输出。例如,这是一个典型的输出:

Jun 1, 2010 10:18:12 AM gamma.utility.application info

INFO: ping: db-time=2010-06-01 10:18:12.0, local-time=20100601t101812, duration=180000
Jun 1, 2010 10:21:12 AM gamma.utility.application info
INFO: ping: db-time=2010-06-01 10:21:12.0, local-time=20100601t102112, duration=180000

我想删除 Jun 1, 2010... 行,它们只会扰乱我的日志输出。我该怎么做?

I'm trying to suppress output of the date line durinng logging when using the default logger in java.util.logging. For example, here is a typical output:

Jun 1, 2010 10:18:12 AM gamma.utility.application info

INFO: ping: db-time=2010-06-01 10:18:12.0, local-time=20100601t101812, duration=180000
Jun 1, 2010 10:21:12 AM gamma.utility.application info
INFO: ping: db-time=2010-06-01 10:21:12.0, local-time=20100601t102112, duration=180000

I would like to get rid of the Jun 1, 2010... lines, they just clutter my log output. How can I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

岁月无声 2024-09-10 22:03:00

从 Java SE 7 开始,有一个新的系统属性:java.util.logging.SimpleFormatter.format

还可以在 java.util.logging 属性文件 (logging.properties) 上配置相同的属性。
如果您是 Eclipse 用户,并且对控制台输出中的双行消息感到恼火,则可以按以下方式更改 jrelogging.properties 文件 (JDK_HOME/jre/lib/logging.properties)

: util.logging.SimpleFormatter.format=%4$s: %5$s [%1$tc]%n

此处提供了一些示例格式:http://docs.oracle.com/javase/7/docs/api/index.html?java/util/日志记录/SimpleFormatter.html

From Java SE 7 there is a new system property: java.util.logging.SimpleFormatter.format.

The same property is also configurable on the java.util.logging properties file (logging.properties).
If you are an Eclipse user, and you are annoyed by the double line message in the console output, you could change the jre logging.properties file (JDK_HOME/jre/lib/logging.properties) in this way:

java.util.logging.SimpleFormatter.format=%4$s: %5$s [%1$tc]%n

Some example format is available here: http://docs.oracle.com/javase/7/docs/api/index.html?java/util/logging/SimpleFormatter.html.

悲凉≈ 2024-09-10 22:03:00

该问题是由父日志中的处理程序引起的。解决方案是从父日志中删除所有处理程序,然后添加自己的自定义处理程序。此代码从父日志中删除处理程序:

      for(Handler iHandler:log.getParent().getHandlers())
        {
        log.getParent().removeHandler(iHandler);
        }

The problem is caused by a handler in the parent log. The solution is to remove all handlers from the parent log, and then add own custom handler. This code removes handlers from the parent log:

      for(Handler iHandler:log.getParent().getHandlers())
        {
        log.getParent().removeHandler(iHandler);
        }
勿忘心安 2024-09-10 22:03:00

编写一个扩展 java.util.logging.Formatter 类的自定义格式化程序,并根据您的需要实现 String format(LogRecord) 方法。例如,以下格式化程序仅显示日志消息(如果正在记录异常,则显示可抛出的堆栈跟踪):

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.logging.Formatter;
import java.util.logging.LogRecord;

class CustomRecordFormatter extends Formatter {
    @Override
    public String format(final LogRecord r) {
        StringBuilder sb = new StringBuilder();
        sb.append(formatMessage(r)).append(System.getProperty("line.separator"));
        if (null != r.getThrown()) {
            sb.append("Throwable occurred: "); //$NON-NLS-1$
            Throwable t = r.getThrown();
            PrintWriter pw = null;
            try {
                StringWriter sw = new StringWriter();
                pw = new PrintWriter(sw);
                t.printStackTrace(pw);
                sb.append(sw.toString());
            } finally {
                if (pw != null) {
                    try {
                        pw.close();
                    } catch (Exception e) {
                        // ignore
                    }
                }
            }
        }
        return sb.toString();
    }
}

这是使用它的方式:

import java.util.logging.ConsoleHandler;
import java.util.logging.Logger;

class A {
    private static final Logger LOGGER = Logger.getLogger(A.class.getName());

    static {
        CustomRecordFormatter formatter = new CustomRecordFormatter();
        ConsoleHandler consoleHandler = new ConsoleHandler();
        consoleHandler.setFormatter(formatter);
        LOGGER.addHandler(consoleHandler);
    }

    public void doSomething() {
        LOGGER.info("something happened");
    }
}

Write a custom formatter extending java.util.logging.Formatter class and implement the String format(LogRecord) method according to your needs. For example, the following formatter shows only the log message (and the throwable stacktrace if an exception is being logged):

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.logging.Formatter;
import java.util.logging.LogRecord;

class CustomRecordFormatter extends Formatter {
    @Override
    public String format(final LogRecord r) {
        StringBuilder sb = new StringBuilder();
        sb.append(formatMessage(r)).append(System.getProperty("line.separator"));
        if (null != r.getThrown()) {
            sb.append("Throwable occurred: "); //$NON-NLS-1$
            Throwable t = r.getThrown();
            PrintWriter pw = null;
            try {
                StringWriter sw = new StringWriter();
                pw = new PrintWriter(sw);
                t.printStackTrace(pw);
                sb.append(sw.toString());
            } finally {
                if (pw != null) {
                    try {
                        pw.close();
                    } catch (Exception e) {
                        // ignore
                    }
                }
            }
        }
        return sb.toString();
    }
}

This is how you use it:

import java.util.logging.ConsoleHandler;
import java.util.logging.Logger;

class A {
    private static final Logger LOGGER = Logger.getLogger(A.class.getName());

    static {
        CustomRecordFormatter formatter = new CustomRecordFormatter();
        ConsoleHandler consoleHandler = new ConsoleHandler();
        consoleHandler.setFormatter(formatter);
        LOGGER.addHandler(consoleHandler);
    }

    public void doSomething() {
        LOGGER.info("something happened");
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文