从logfile创建.html文件

时间:2013-09-24 20:10:08

标签: html bash grep cat

我需要一个脚本,将日志文件转换为易于查看的.html文件,可由“任何人”访问。

这是我到目前为止所做的:

#!/bin/bash

## The purpose of this script is to create .html files from log files, with the ability to optionally GREP for certain strings

if [ "$2" == "" ];
then
        echo "Usage : $0 [Source Log file] [Output HTML file] [String to grep (if applicable)]"
        exit 255
fi;

LOGFILE=$1
OUTPUTFILE=$2
GREPSTRING=$3

if [ "$3" == "" ];
then
        echo "Cat'ing $LOGFILE to $OUTPUTFILE"
        LOGCONTENTS=`cat $LOGFILE`

else
        echo "Grep'ing for $GREPSTRING in $LOGFILE"
        LOGCONTENTS=`grep --color $GREPSTRING $LOGFILE | sed -e "s/^.*$/&1\n/"`
fi;

# Below is the html heading which will be appended to the final .html file
HEADING="<html><body><h1>Log output for: $LOGFILE</h1>"

# Below is the end of the html file
END="</body></html>"

# Below all the prepared variables are stitched together to the OUTPUT/LOG FILE
echo $HEADING > $OUTPUTFILE
echo $LOGCONTENTS >> $OUTPUTFILE
echo $END >> $OUTPUTFILE


# For debugging, enable the lines below
echo $LOGCONTENTS
echo "Done preparing $OUTPUTFILE"

我的问题是输出,无论我玩CAT,GREP,SED等多少,都不会保留换行符。在执行正常的tail -f或cat时,输出文件看起来或多或少都是必不可少的。

4 个答案:

答案 0 :(得分:4)

不是将事物复制到变量中(然后错误地引用它们),而是重要的重构。

#!/bin/sh
exec >"$2"
cat <<HERE
<html><body><h1>Log output for: $1</h1>
<pre>
HERE
grep "${3:-^}" "$1"
echo '</pre></body></html>'

因为HTML无论如何都无法显示终端颜色代码,所以我拿出了--color选项。如果你有一个合适的过滤器来翻译HTML颜色标签,那么一定要把它扔进去。

另请注意,如果您不提供第三个命令行参数,则正则表达式将默认为匹配所有行的正则表达式。

许多浏览器会将单独的开放<pre>标记渲染为一条没有吸引力的空行,但修复它并不是必需的。如果困扰你,请将其分解为单独的echo -n '<pre>'

答案 1 :(得分:2)

由于您要使用HTML,因此需要将换行符(使用sed或其他)替换为HTML换行符<br/>。 HTML通常以自由流动的文本忽略CR / LF字符。

所以你可以使用sed,如下所示。我删除了1,因为没有解释他们的目的,但是如果你需要,你可以把它们放回去。

if [ "$3" == "" ];
then
        echo "Cat'ing $LOGFILE to $OUTPUTFILE"
        LOGCONTENTS=`sed -e "s/^.*$/&<br\/>/" $LOGFILE`

else
        echo "Grep'ing for $GREPSTRING in $LOGFILE"
        LOGCONTENTS=`grep --color $GREPSTRING $LOGFILE | sed -e "s/^.*$/&<br\/>/"`
fi;

答案 2 :(得分:0)

公共类Log4jHTMLlayout扩展HTMLLayout {     protected final int BUF_SIZE = 256;     protected final int MAX_CAPACITY = 1024;     static String TRACE_PREFIX =“
”;     private StringBuffer sbuf = new StringBuffer(BUF_SIZE);     public static final String LOCATION_INFO_OPTION =“LocationInfo”;

public String format(LoggingEvent event) {

    if (sbuf.capacity() > MAX_CAPACITY) {
        sbuf = new StringBuffer(BUF_SIZE);
    } else {
        sbuf.setLength(0);
    }

    sbuf.append(Layout.LINE_SEP + "<tr>" + Layout.LINE_SEP);

    sbuf.append("<td>");
    Calendar cal=Calendar.getInstance();
    //sbuf.append(event.timeStamp - LoggingEvent.getStartTime());
    Timestamp stmp=new Timestamp(cal.getTimeInMillis());
    sbuf.append(stmp.toString());
    sbuf.append("</td>" + Layout.LINE_SEP);

    String escapedThread = Transform.escapeTags(event.getThreadName());
    sbuf.append("<td title=\"" + escapedThread + " thread\">");
    sbuf.append(escapedThread);
    sbuf.append("</td>" + Layout.LINE_SEP);

    sbuf.append("<td title=\"Level\">");
    if (event.getLevel().equals(Level.DEBUG)) {
        sbuf.append("<font color=\"#339933\">");
        sbuf.append(Transform.escapeTags(String.valueOf(event.getLevel())));
        sbuf.append("</font>");
    } else if (event.getLevel().isGreaterOrEqual(Level.WARN)) {
        sbuf.append("<font color=\"#993300\"><strong>");
        sbuf.append(Transform.escapeTags(String.valueOf(event.getLevel())));
        sbuf.append("</strong></font>");
    } else {
        sbuf.append(Transform.escapeTags(String.valueOf(event.getLevel())));
    }
    sbuf.append("</td>" + Layout.LINE_SEP);

    String escapedLogger = Transform.escapeTags(event.getLoggerName());
    sbuf.append("<td title=\"" + escapedLogger + " category\">");
    sbuf.append(escapedLogger);
    sbuf.append("</td>" + Layout.LINE_SEP);

    if (getLocationInfo()) {
        LocationInfo locInfo = event.getLocationInformation();
        sbuf.append("<td>");
        sbuf.append(Transform.escapeTags(locInfo.getFileName()));
        sbuf.append(':');
        sbuf.append(locInfo.getLineNumber());
        sbuf.append("</td>" + Layout.LINE_SEP);
    }

    sbuf.append("<td title=\"Message\">");
    sbuf.append(Transform.escapeTags(event.getRenderedMessage()));
    sbuf.append("</td>" + Layout.LINE_SEP);
    sbuf.append("</tr>" + Layout.LINE_SEP);

    if (event.getNDC() != null) {
        sbuf.append("<tr><td bgcolor=\"#EEEEEE\" style=\"font-size : xx-small;\" colspan=\"6\" title=\"Nested Diagnostic Context\">");
        sbuf.append("NDC: " + Transform.escapeTags(event.getNDC()));
        sbuf.append("</td></tr>" + Layout.LINE_SEP);
    }

    String[] s = event.getThrowableStrRep();
    if (s != null) {
        sbuf.append("<tr><td bgcolor=\"#993300\" style=\"color:White; font-size : xx-small;\" colspan=\"6\">");
        appendThrowableAsHTML(s, sbuf,true);
        sbuf.append("</td></tr>" + Layout.LINE_SEP);
    }

    return sbuf.toString();

}

public void appendThrowableAsHTML(String[] s, StringBuffer sbuf,boolean isAppend) {
         if(s != null) {
           int len = s.length;
           if(len == 0)
               return;
           sbuf.append(Transform.escapeTags(s[0]));
           sbuf.append(Layout.LINE_SEP);
           for(int i = 1; i < len; i++) {
                sbuf.append(TRACE_PREFIX);
                sbuf.append(Transform.escapeTags(s[i]));
                sbuf.append(Layout.LINE_SEP);
           }
       }
}
public String getHeader() {
    StringBuffer sbuf = new StringBuffer();
    sbuf.append("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">" + Layout.LINE_SEP);
    sbuf.append("<html>" + Layout.LINE_SEP);
    sbuf.append("<head>" + Layout.LINE_SEP);
    sbuf.append("<title>" + getTitle() + "</title>" + Layout.LINE_SEP);
    sbuf.append("<style type=\"text/css\">" + Layout.LINE_SEP);
    sbuf.append("<!--" + Layout.LINE_SEP);
    sbuf.append("body, table {font-family: arial,sans-serif; font-size: x-small;}" + Layout.LINE_SEP);
    sbuf.append("th {background: #336699; color: #FFFFFF; text-align: left;}" + Layout.LINE_SEP);
    sbuf.append("-->" + Layout.LINE_SEP);
    sbuf.append("</style>" + Layout.LINE_SEP);
    sbuf.append("</head>" + Layout.LINE_SEP);
    sbuf.append("<body bgcolor=\"#FFFFFF\" topmargin=\"6\" leftmargin=\"6\">" + Layout.LINE_SEP);
    sbuf.append("<hr size=\"1\" noshade>" + Layout.LINE_SEP);
    sbuf.append("Log session start time " + new java.util.Date() + "<br>" + Layout.LINE_SEP);
    sbuf.append("<br>" + Layout.LINE_SEP);
    sbuf.append("<table cellspacing=\"0\" cellpadding=\"4\" border=\"1\" bordercolor=\"#224466\" width=\"100%\">" + Layout.LINE_SEP);
    sbuf.append("<tr>" + Layout.LINE_SEP);
    sbuf.append("<th>Time</th>" + Layout.LINE_SEP);
    sbuf.append("<th>Thread</th>" + Layout.LINE_SEP);
    sbuf.append("<th>Level</th>" + Layout.LINE_SEP);
    sbuf.append("<th>Category</th>" + Layout.LINE_SEP);
    if (getLocationInfo()) {
        sbuf.append("<th>File:Line</th>" + Layout.LINE_SEP);
    }
    sbuf.append("<th>Message</th>" + Layout.LINE_SEP);
    sbuf.append("</tr>" + Layout.LINE_SEP);
    return sbuf.toString();
}

} 强文

答案 3 :(得分:0)

您可以为此目的使用ccat

如果您使用的是Ubuntu,请先按照install进行操作,然后将日志文件转换为html文件,如下所示。

box

我使用此程序包将valgrind日志转换为html文件,从而提供了漂亮的彩色输出。