How to log FATAL (or any custom log level) with SLF4J and Log4j2

前端 未结 6 1676
粉色の甜心
粉色の甜心 2021-02-05 15:18

I have those specific requirements :

  • Need to be able to log in FATAL level
  • Need to use SLF4J<
6条回答
  •  生来不讨喜
    2021-02-05 15:40

    Marker is not really what you want here. Marker is for "enriching" log messages, making them more easily searchable. You are trying to change the log level/priority, which is a little different.

    You're using logger.error() which will log the message as an ERROR level.

    If there is no FATAL level pre-defined (usually there is, such as logger.fatal()), then use the generic logger.log() which allows you to specify the log level.

    logger.fatal(yourMessage);
    

    OR

    logger.log(priorityLevel, yourMessage);
    

    UPDATE:

    From the SLF4J website:

    The Marker interface, part of the org.slf4j package, renders the FATAL level largely redundant. If a given error requires attention beyond that allocated for ordinary errors, simply mark the logging statement with a specially designated marker which can be named "FATAL" or any other name to your liking.

    http://www.slf4j.org/faq.html#fatal

    So, with SLF4J, it is not possible to have a FATAL log level. I strongly disagree with the rationale behind this decision, but it is what it is.

提交回复
热议问题