Spring boot - sending emails with Logback

前端 未结 3 1490
栀梦
栀梦 2020-12-29 04:12

I am trying to configure Logback to send emails whenever an exception (Logging level: ERROR) occurs. I have not been able to make it work so far so I would like to ask for y

3条回答
  •  [愿得一人]
    2020-12-29 04:33

    You have to enable Marker based triggering of email.

    include an evaluator in your logback.xml under SMTP appender refer below link

    http://logback.qos.ch/manual/appenders.html#OnMarkerEvaluator

    then add below logic in your Exception catch block,pass Marker object to logger method if you want to send email like below -

    Marker notifyAdmin = MarkerFactory.getMarker("NOTIFY_ADMIN");
    logger.error(notifyAdmin,
      "This is a serious an error requiring the admin's attention",
       new Except
    ion("Just testing"));
    

    its possible that in some scenario you dont want to send email and only want to log the exception then just dont pass Marker object to logger method.

    logger.error("This is a serious an error requiring the admin's attention",
       new Except
    ion("Just testing"));
    

提交回复
热议问题