logback HTMLLayout页面太宽了

时间:2017-02-16 08:02:04

标签: java html html-table logback

我将此布局与RollingFileAppender一起使用。

一切都很好,但只有一个问题:通常表格的像素宽度太宽。

特别是这样,因为我的许多测试方法名称通常都很长......它们离窗口的右边缘不远,我不得不左右滚动。

我还使用(Firefox)NoSquint加载项,您可以在其中放大文本大小,而不仅仅是"一般缩放"。但这并没有真正奏效。我只想规定每个表列的最大(和最小,为什么不?)像素宽度。

1 个答案:

答案 0 :(得分:0)

有兴趣的人,我找到的解决方案是在包含驼峰案例文本的输出­中插入HTML 软连字符String) /或点(也使文字变小)。

- 修改 logback.xml logback-test.xml 文件,如下所示:

    <!--  <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">  -->
    <encoder class="utils.MyLayoutWrappingEncoder">
        <layout class="ch.qos.logback.classic.html.HTMLLayout">
            ....

- 从提取的logback源jar中复制LayoutWrapperEncoder文件,并重命名为MyLayoutWrapperEncoder

- 修改此课程中的几个方法

void writeHeader() throws IOException {
    if (layout != null && (outputStream != null)) {
        StringBuilder sb = new StringBuilder();
        appendIfNotNull(sb, layout.getFileHeader());
        appendIfNotNull(sb, layout.getPresentationHeader());
        if (sb.length() > 0) {
            sb.append(CoreConstants.LINE_SEPARATOR);

            // replace a couple of header names with shorter and smaller text            
            String outString = sb.toString();
            outString = outString.replaceAll( "MethodOfCaller", "MoC" ).replaceAll( "RelativeTime", "RT" );
            outString = outString.replaceAll( "LineOfCaller", "LoC" );
            // make text smaller
            outString = outString.replaceAll( "<td class=(.+?)>", "<td style=\"font-size:13px\" class=$1>" );

            outputStream.write(convertToBytes( outString ));
            outputStream.flush();
        }
    }
}

和:

public void doEncode(E event) throws IOException {
    String txt = layout.doLayout(event);

    /*
    * Identify "camel case" upper case letters and also dots in all <TD> output...
    * and insert soft hyphen at this point.  Also make text smaller.
    */
    Pattern tDPattern = Pattern.compile( "<td class=.+?>(.*?)</td>" );
    StringBuilder wholeStringSB = new StringBuilder();
    Matcher tDMatcher = tDPattern.matcher( txt );
    int lastWholeString = 0;
    while( tDMatcher.find() ){
        String tDContents = tDMatcher.group( 1 );
        Matcher camelCaseAndDotMatcher = Pattern.compile("[a-z]([A-Z\\.])").matcher( tDContents );
        StringBuilder camelCaseAndDotSB = new StringBuilder();
        int last = 0;
        while( camelCaseAndDotMatcher.find() ){
            camelCaseAndDotSB.append( tDContents.substring( last,  camelCaseAndDotMatcher.start( 1 ) ));
            camelCaseAndDotSB.append( "&shy;" + camelCaseAndDotMatcher.group(1) );
            last = camelCaseAndDotMatcher.end(1);
        }
        if( last > 0 ){
            // ... at least one camel case UC char and/or dot WAS found...
            wholeStringSB.append( txt.substring( lastWholeString,  tDMatcher.start( 1 )) );
            camelCaseAndDotSB.append( tDContents.substring(last));    
            wholeStringSB.append( camelCaseAndDotSB.toString().trim() );
            lastWholeString = tDMatcher.end();
        }
    }
    wholeStringSB.append( txt.substring( lastWholeString ).trim() );
    txt = wholeStringSB.toString();
    // make text smaller
    txt = txt.replaceAll( "<td class=(.+?)>", "<td style=\"font-size:13px\" class=$1>" );


    outputStream.write(convertToBytes(txt));
    if (immediateFlush)
        outputStream.flush();
}

PS我还没有找到这些不同TD元素的各种“类”属性的CSS所在的位置(或者,如果不是实际文件,它们是如何生成的或其他什么)。有人知道吗?

PPS虽然这是一种很有前景的方法,但这些软连字符似乎并未应用于最终的“消息”列。需要更多的研究来获得非常出色的解决方案!