[logback-dev] [JIRA] Created: (LBCORE-69) SMTPAppenderBase: Allow to select charset encoding for email's subject and body

Andrey Rybin (JIRA) noreply-jira at qos.ch
Sun Nov 2 07:42:21 CET 2008


SMTPAppenderBase: Allow to select charset encoding for email's subject and body
-------------------------------------------------------------------------------

                 Key: LBCORE-69
                 URL: http://jira.qos.ch/browse/LBCORE-69
             Project: logback-core
          Issue Type: Improvement
          Components: Appender
            Reporter: Andrey Rybin
            Assignee: Logback dev list


Please add String charsetEncoding property to SMTPAppenderBase and use it for email's subject and content encoding.
Now javaMail default charset encoding is used so only ASCII latin characters can be used in log messages.

For example my fix for log4j SMTPAppender:
+
  private String charsetEncoding = "UTF-8";

  public void activateOptions () {
..
      addressMessage(msg);
      if (getSubject() != null) {
        ((MimeMessage)msg).setSubject(getSubject(), charsetEncoding);  //fix1. Cast looks ugly, but as we always create MimeMessage above, it's ok
      }

 /*** Send the contents of the cyclic buffer as an e-mail message.  */
  @Override
  protected void sendBuffer () {
    /* Note: this code already owns the monitor for this
    appender. This frees us from needing to synchronize on 'cb'. */
    try {
      final MimeBodyPart part = new MimeBodyPart();
      final StringBuilder sb = new StringBuilder();

      String t = layout.getHeader();
      if (t != null) { sb.append(t); } //add header

      final int len = cb.length();
      for (int i = 0; i < len; i++) {
        final LoggingEvent event = cb.get();
        sb.append(layout.format(event));
        if (layout.ignoresThrowable()) {
          final String[] s = event.getThrowableStrRep();
          if (s != null) {
            for (int j = 0; j < s.length; j++) {
              sb.append(s[j]).append(Layout.LINE_SEP);
            }//for
          }//if
        }//if
      }//for

      t = layout.getFooter();
      if (t != null) { sb.append(t); }

//fix2:
      t = layout.getContentType();  
      if (t.startsWith("text/")) {
        part.setText(sb.toString(), charsetEncoding, t.substring(5)); //remove 'text/' from contentType cause javaMail needs here only subtype (html, plain, etc)

      } else {
        part.setContent(sb.toString(), t); //for non text chartset isn't needed
      }//if

      final Multipart mp = new MimeMultipart();
      mp.addBodyPart(part);
      msg.setContent(mp);

      msg.setSentDate(new Date());
      Transport.send(msg);
    } catch (Exception e) {
      LogLog.error("sendBuffer: Error occured while sending e-mail notification from "+getFrom()+" to "+getTo(), e);
    }//try
  }//sendBuffer

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://jira.qos.ch/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        


More information about the logback-dev mailing list