bash 从日志文件创建 .html 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18991126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating .html file from logfile
提问by user2812626
I need a script which converts log files into easily viewable .html files, accessible by "anyone".
我需要一个脚本,将日志文件转换为易于查看的 .html 文件,“任何人”都可以访问。
Here is what I have so far:
这是我到目前为止所拥有的:
#!/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 [ "" == "" ];
then
echo "Usage : #!/bin/sh
exec >""
cat <<HERE
<html><body><h1>Log output for: </h1>
<pre>
HERE
grep "${3:-^}" ""
echo '</pre></body></html>'
[Source Log file] [Output HTML file] [String to grep (if applicable)]"
exit 255
fi;
LOGFILE=
OUTPUTFILE=
GREPSTRING=
if [ "" == "" ];
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"
My problem is that the output, no matter how much I play with CAT, GREP, SED etc, does not preserve line breaks. It is essential for the output file to look more or less like when doing a normal tail -f or cat.
我的问题是,无论我如何使用 CAT、GREP、SED 等,输出都不会保留换行符。输出文件看起来或多或少像执行正常的 tail -f 或 cat 是必不可少的。
回答by tripleee
Instead of copying things into variables (and then misquoting them), here's a significant refactoring.
不是将事物复制到变量中(然后错误地引用它们),而是进行了重大的重构。
if [ "" == "" ];
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;
Because HTML cannot display terminal color codes anyway, I took out the --color
option. If you have a suitable filter for translating to HTML color tags, by all means throw it in.
因为 HTML 无论如何都不能显示终端颜色代码,所以我去掉了这个--color
选项。如果你有一个合适的过滤器来转换为 HTML 颜色标签,一定要把它扔进去。
Notice also that the regex will default to one which matches all lines if you don't supply a third command-line argument.
另请注意,如果您不提供第三个命令行参数,则正则表达式将默认为匹配所有行的一个。
Many browsers will render the lone opening <pre>
tag as an unattractive empty line, but fixing that is hardly essential here. Break it out into a separate echo -n '<pre>'
if it bothers you.
许多浏览器会将单独的开始<pre>
标记呈现为一个没有吸引力的空行,但在这里修复它几乎不是必需的。echo -n '<pre>'
如果它打扰你,就把它分成一个单独的。
回答by lurker
Since you're going to HTML, you need to replace line break characters (using sed
or whatever) with <br/>
, the HTML line break. HTML ignores CR/LF characters normally in free flowing text.
由于您要使用 HTML,您需要将换行符(使用sed
或其他)替换为<br/>
,HTML 换行符。HTML 通常会忽略自由流动文本中的 CR/LF 字符。
So you could use sed
as below. I removed the 1
s as their purpose is not explained, but you could put them back in if you require.
所以你可以使用sed
如下。我删除了1
s,因为没有解释它们的用途,但如果需要,您可以将它们放回原处。
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();
}
回答by Ravi Kumar
public class Log4jHTMLlayout extends 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 class Log4jHTMLlayout extends HTMLLayout { protected final int BUF_SIZE = 256; 受保护的最终 int MAX_CAPACITY = 1024; 静态字符串 TRACE_PREFIX = "
"; 私有字符串缓冲区 sbuf = 新字符串缓冲区(BUF_SIZE);public static final String LOCATION_INFO_OPTION = "LocationInfo";
ccat check_valgrind_log.log --html > check_valgrind_log.html
} strong text
} 强文本
回答by Nikhil Augustine
You may use ccatfor this purpose.
为此,您可以使用ccat。
If you are on Ubuntu follow installit first and convert a log file to html file as shown below.
如果您使用的是 Ubuntu,请先安装它并将日志文件转换为 html 文件,如下所示。
##代码##I used this package to convert valgrind log to an html file, which gives a nice coloured output.
我使用这个包将 valgrind 日志转换为一个 html 文件,它提供了一个漂亮的彩色输出。