有什么方法可以从java.util.logging.Logger输出中删除信息行? [英] Is there any way to remove the information line from java.util.logging.Logger output?

查看:200
本文介绍了有什么方法可以从java.util.logging.Logger输出中删除信息行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用java.util.logging.Logger将某些日志输出到控制台,如下所示:

Using java.util.logging.Logger to output some log to the console just like this:

public static void main(String[] args) {
    Logger logger = Logger.getLogger("test");
    logger.info("Hello Wolrd!");
}

输出为:

FEB 16, 2012 10:17:43 AM com.abc.HelloWorld main
INFO: Hello World.

这似乎还可以,但是...

This seems to be OK, however...

我们在所有Ant任务(内部标准)中都使用java.util.logging.Logger,并且我们有一个大型的ant项目.一个完整周期的控制台输出可能大于300KB,其中我们自己的记录器输出至少达到50KB.

We are using java.util.logging.Logger in all our Ant tasks (an internal standard) and we have a large ant project. The console output of a full cycle can be larger than 300KB, in which our own logger output taks at least 50.

现在,我不想看到有关Level.INFO输出的时间,类名和方法的信息.此外,信息行还使得您很难专注于自定义消息.

Now I don't want to see the information about time, class name and method of Level.INFO outputs. Also the information line makes it hard to focus on the custom messages.

那么有什么方法可以从每个输出(或者只是从每个Level.INFO输出)中删除第一行(时间戳,类和方法的信息)吗?

So is there any way to remove the first line (information of timestamp, class and method) from each output (or just from each Level.INFO output)?

推荐答案

请参见

See How do I get java logging output to appear on a single line?. However, the only difference is removing the first line, instead of putting it on one line.

仅对INFO级执行此操作,请扩展要有条件修改的格式化程序(例如SimpleFormatter),并覆盖format方法.您可以执行以下操作:

To only do this for INFO levels, extend the formatter you want to conditionally modify (e.g. SimpleFormatter), and override the format method. You could do something like this:

public String format(LogRecord record){
  if(record.getLevel() == Level.INFO){
    return record.getMessage() + "\r\n";
  }else{
    return super.format(record);
  }
}

如果仅针对INFO执行此操作,则可能不需要"INFO:"前缀-但可以根据需要将其重新添加.

If you're doing this only for INFO, you probably don't need the "INFO: " prefix - but you could add it back on if you'd like.

这篇关于有什么方法可以从java.util.logging.Logger输出中删除信息行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆