开发者

How to set HTML encoding in HTMLLayout in logback?

By default logback only produces the following header for HTML log files:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
  <head>
    <title>Logback Log Messages</title>
<style  type="text/css">

</style>
</head>

But encoding here is not set. And some browsers default encoding to something they want like 'windows-1251' or 'ISO-8859-1' or 'ISO-8859-5'.

How do I add something like

<meta http-equiv="Content-Type" con开发者_如何学Ctent="text/html; charset=utf-16">

to HTML header? Also since all strings in Java are UTF-16, shouldn't this be permanently set in logback source code?

Is there any way to do it via logback configuration file or should I create my own HTMLLayout descendant?


You couldn't set "head" tag,but you can extend "HTMLLayout",such example:

public class CustomizationHTMLLayout extends HTMLLayoutBase<ILoggingEvent>{

    @Override
    public String getFileHeader()
    {
        StringBuilder sbuf = new StringBuilder();
        sbuf.append("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"");
        sbuf.append(" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">");
        sbuf.append(LINE_SEPARATOR);
        sbuf.append("<html>");
        sbuf.append(LINE_SEPARATOR);
        sbuf.append("  <head>");
        sbuf.append(LINE_SEPARATOR);
        // customization code
        sbuf.append("    <meta charset=\"utf-8\">");
        sbuf.append(LINE_SEPARATOR);
        // customization code
        sbuf.append("    <meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">");
        sbuf.append(LINE_SEPARATOR);
        sbuf.append("    <title>");
        sbuf.append(title);
        sbuf.append("</title>");
        sbuf.append(LINE_SEPARATOR);

        cssBuilder.addCss(sbuf);

        sbuf.append(LINE_SEPARATOR);
        sbuf.append("  </head>");
        sbuf.append(LINE_SEPARATOR);
        sbuf.append("<body>");
        sbuf.append(LINE_SEPARATOR);

        return sbuf.toString();
    }

}

if you use xml configraution , change "layout" tag,use your class

<layout class="CustomizationHTMLLayout">


It appears from the javadocs that no such option exists, but check the source to be certain. Logback is still a moving target until 1.0.

If you find it is not present, then raise a bug in the logback JIRA instance -- http://jira.qos.ch/secure/Dashboard.jspa - and ask for it, or submit a patch that adds the functionality.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜