简体   繁体   English

如何自定义默认的Java日志记录?

[英]How to customize the default java logging?

I need to have custom log levels(other than java.util.logging.Level), and custom functions in the java util logger to log messages of these levels. 我需要具有自定义日志级别(不是java.util.logging.Level),并且需要Java util logger中的自定义函数来记录这些级别的消息。

Custom levels: 1. DEBUG 2. ERROR 3. FATAL 自定义级别:1.调试2.错误3.致命

So I created XYZLevel extended from Level as follows: 所以我创建了从Level扩展的XYZLevel,如下所示:

public class OKILevel extends Level {

public OKILevel(String name, int value) {
    super(name, value);
}

/* OKI Log Levels */
public static final Level FATAL = new OKILevel("FATAL",
        Level.SEVERE.intValue() + 100);

public static final Level ERROR = new OKILevel("ERROR",
        Level.SEVERE.intValue());

public static final Level DEBUG = new OKILevel("DEBUG",
        Level.FINE.intValue());

}

Custom functions required: .debug(), .severe() etc similar to .info in logger. 需要自定义函数:.debug()、. severe()等,类似于记录器中的.info。

For logger I do: 对于记录器,我这样做:

static final Logger logger = Logger.getLogger(ABC.class.getName());

I am unable to figure out: 我无法弄清楚:

static final XYZLogger logger = XYZLogger.getLogger(ABC.class.getName());

extending the Logger doesn't help. 扩展记录器无济于事。

Also, for this application the logging.properties need to be passed in. How do I use these new levels in .level of logging.properties? 另外,对于此应用程序,需要传递logging.properties。如何在logging.properties的.level中使用这些新级别?

If you are creating log wrapper then you should leverage some of the standard frameworks before inventing something new. 如果要创建日志包装器,则应在发明新内容之前利用一些标准框架。

If you want your application to use a new logger implementation then you have to implement and install a custom LogManager . 如果希望您的应用程序使用新的记录器实现,则必须实现并安装自定义LogManager

Also, for this application the logging.properties need to be passed in. How do I use these new levels in .level of logging.properties? 另外,对于此应用程序,需要传递logging.properties。如何在logging.properties的.level中使用这些新级别?

All of the .level parsing performed via the Level.parse method. 所有的.level解析都是通过Level.parse方法执行的。 Per the docs: 根据文档:

The argument string may consist of either a level name or an integer value. 参数字符串可以由级别名称或整数值组成。

For example: 例如:

  • "SEVERE" “严重”

  • "1000" “ 1000”

So you should be able to write: 因此,您应该能够编写:

#Level.SEVERE + 100 = FATAL
some.logger.name.level = 1100

That said there are some known issues with finding the correct level value by int in some releases of Java. 也就是说在某些Java版本中, 通过int查找正确的级别值存在一些已知的问题。

Another option if you just want the rendered output of a level to be different then you should have a look at Best way to override java.util.logging Level class to change the level strings . 如果您只是希望某个级别的呈现输出与众不同,则可以使用另一种方法,那就应该看看重写java.util.logging Level类以更改级别字符串的最佳方法

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

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