user171523
user171523

Reputation: 4345

Log4j SMTPappender with HTML mail

I would like to use log4j and send email in HTML format (Using SMTP Appender). Is it possible in log4j out of the box.

If so please point me to right examples

Upvotes: 1

Views: 2410

Answers (1)

gMale
gMale

Reputation: 17895

Logback provides a Layout that formats the logs as HTML. Combining that with an SMTPAppender would send email in HTML format.

http://logback.qos.ch/manual/layouts.html#ClassicHTMLLayout

Direct quote from the page:

The HTMLLayout is often used in conjunction with SMTPAppender so that outgoing email is pleasantly formatted in HTML.

You could even take the source code and modify it, pretty easily, to suit your needs.

http://logback.qos.ch/xref/ch/qos/logback/classic/html/HTMLLayout.html


They provide the following code to initilaize the appender:

 <appender name="SMTP" class="ch.qos.logback.classic.net.SMTPAppender">
    <layout class="ch.qos.logback.classic.html.HTMLLayout">
      <pattern>%relative%thread%mdc%level%class%msg</pattern>
    </layout>
    <From>[email protected]</From>
    <SMTPHost>mail.domain.net</SMTPHost>
    <Subject>LastEvent: %class - %msg </Subject>
    <To>[email protected]</To> 
  </appender>


I hope that helps in some way,

-gMale

Upvotes: 1

Related Questions