$darkmode
DENOPTIM
HTMLLogFormatter.java
Go to the documentation of this file.
1/*
2 * DENOPTIM
3 * Copyright (C) 2019 Vishwesh Venkatraman <vishwesh.venkatraman@ntnu.no>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as published
7 * by the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19package denoptim.logging;
20
21import java.text.SimpleDateFormat;
22import java.util.Date;
23import java.util.logging.Formatter;
24import java.util.logging.Handler;
25import java.util.logging.Level;
26import java.util.logging.LogRecord;
27
32public class HTMLLogFormatter extends Formatter
33{
34
35//------------------------------------------------------------------------------
36
37 // This method is called for every log records
38 @Override
39 public String format(LogRecord rec)
40 {
41 StringBuilder buf = new StringBuilder(1000);
42 // Bold any levels >= WARNING
43 buf.append("<tr>");
44 buf.append("<td>");
45
46 if (rec.getLevel().intValue() >= Level.WARNING.intValue())
47 {
48 buf.append("<b>");
49 buf.append(rec.getLevel());
50 buf.append("</b>");
51 } else
52 {
53 buf.append(rec.getLevel());
54 }
55 buf.append("</td>");
56 buf.append("<td>");
57 buf.append(calcDate(rec.getMillis()));
58 buf.append(' ');
59 buf.append(formatMessage(rec));
60 buf.append('\n');
61 buf.append("<td>");
62 buf.append("</tr>\n");
63 return buf.toString();
64 }
65
66//------------------------------------------------------------------------------
67
68 private String calcDate(long millisecs)
69 {
70 SimpleDateFormat date_format = new SimpleDateFormat("MMM dd,yyyy HH:mm");
71 Date resultdate = new Date(millisecs);
72 return date_format.format(resultdate);
73 }
74
75//------------------------------------------------------------------------------
76
77 // This method is called just after the handler using this
78 // formatter is created
79 @Override
80 public String getHead(Handler h)
81 {
82 return "<HTML>\n<HEAD>\n" + (new Date()) + "\n</HEAD>\n<BODY>\n<PRE>\n"
83 + "<table border>\n "
84 + "<tr><th>Time</th><th>Log Message</th></tr>\n";
85 }
86
87//------------------------------------------------------------------------------
88
89 // This method is called just after the handler using this
90 // formatter is closed
91 @Override
92 public String getTail(Handler h)
93 {
94 return "</table>\n </PRE></BODY>\n</HTML>\n";
95 }
96
97//------------------------------------------------------------------------------
98
99}