1 |
// Jericho HTML Parser - Java based library for analysing and manipulating HTML
|
2 |
// Version 3.2
|
3 |
// Copyright (C) 2004-2009 Martin Jericho
|
4 |
// http://jericho.htmlparser.net/
|
5 |
//
|
6 |
// This library is free software; you can redistribute it and/or
|
7 |
// modify it under the terms of either one of the following licences:
|
8 |
//
|
9 |
// 1. The Eclipse Public License (EPL) version 1.0,
|
10 |
// included in this distribution in the file licence-epl-1.0.html
|
11 |
// or available at http://www.eclipse.org/legal/epl-v10.html
|
12 |
//
|
13 |
// 2. The GNU Lesser General Public License (LGPL) version 2.1 or later,
|
14 |
// included in this distribution in the file licence-lgpl-2.1.txt
|
15 |
// or available at http://www.gnu.org/licenses/lgpl.txt
|
16 |
//
|
17 |
// This library is distributed on an "AS IS" basis,
|
18 |
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
|
19 |
// See the individual licence texts for more details.
|
20 |
|
21 |
package net.htmlparser.jericho;
|
22 |
|
23 |
import java.util.logging.*;
|
24 |
|
25 |
/**
|
26 |
* Provides basic formatting for log messages.
|
27 |
* <p>
|
28 |
* This class extends the <code>java.util.logging.Formatter</code> class, allowing it to be specified as a formatter for the <code>java.util.logging</code> system.
|
29 |
* <p>
|
30 |
* The static {@link #format(String level, String message, String loggerName)} method provides a means of using the same formatting
|
31 |
* outside of the <code>java.util.logging</code> framework. See the documentation of this method for more details.
|
32 |
*/
|
33 |
public class BasicLogFormatter extends Formatter {
|
34 |
/**
|
35 |
* Determines whether the <a href="Logger.html#LoggingLevel">logging level</a> is included in the output.
|
36 |
* <p>
|
37 |
* The default value is <code>true</code>.
|
38 |
* <p>
|
39 |
* As this is a static property, changing the value will affect all <code>BasicLogFormatter</code> instances, as well as the behaviour of the
|
40 |
* static {@link #format(String level, String message, String loggerName)} method.
|
41 |
*/
|
42 |
public static boolean OutputLevel=true;
|
43 |
|
44 |
/**
|
45 |
* Determines whether the logger name is included in the output.
|
46 |
* <p>
|
47 |
* The default value is <code>false</code>.
|
48 |
* <p>
|
49 |
* The logger name used for all automatically created {@link Logger} instances is "<code>net.htmlparser.jericho</code>".
|
50 |
* <p>
|
51 |
* As this is a static property, changing the value will affect all <code>BasicLogFormatter</code> instances, as well as the behaviour of the
|
52 |
* static {@link #format(String level, String message, String loggerName)} method.
|
53 |
*/
|
54 |
public static boolean OutputName=false;
|
55 |
|
56 |
static final Formatter INSTANCE=new BasicLogFormatter();
|
57 |
|
58 |
/**
|
59 |
* Returns a formatted string representing the log entry information contained in the specified <code>java.util.logging.LogRecord</code>.
|
60 |
* <p>
|
61 |
* This method is not called directly, but is used by the <code>java.util.logging</code> framework when this class is specified
|
62 |
* as a formatter in the <code>logging.properties</code> file.
|
63 |
* <p>
|
64 |
* See the documentation of the parent <code>java.util.logging.Formatter</code> class in the Java SDK for more details.
|
65 |
*
|
66 |
* @param logRecord a <code>java.util.logging.LogRecord</code> object containing all of the log entry information.
|
67 |
* @return a formatted string representing the log entry information contained in the specified <code>java.util.logging.LogRecord</code>.
|
68 |
*/
|
69 |
public String format(final LogRecord logRecord) {
|
70 |
return format(logRecord.getLevel().getName(),logRecord.getMessage(),logRecord.getLoggerName());
|
71 |
}
|
72 |
|
73 |
/**
|
74 |
* Returns a formatted string representing the specified log entry information.
|
75 |
* <p>
|
76 |
* This method is used by the default implementation of the {@link WriterLogger#log(String level, String message)} method.
|
77 |
* <p>
|
78 |
* The static properties {@link #OutputLevel} and {@link #OutputName} affect what information is included in the output.
|
79 |
* <p>
|
80 |
* The static {@link Config#NewLine} property determines the character sequence used for line breaks.
|
81 |
* <p>
|
82 |
* A line of output typically looks like this:
|
83 |
* <blockquote class="SmallVerticalMargin"><code>INFO: this is the log message</code></blockquote>
|
84 |
* or if the {@link #OutputName} property is set to <code>true</code>, the output would look similar to this:
|
85 |
* <blockquote class="SmallVerticalMargin"><code>INFO: [net.htmlparser.jericho] this is the log message</code></blockquote>
|
86 |
*
|
87 |
* @param level a string representing the <a href="Logger.html#LoggingLevel">logging level</a> of the log entry.
|
88 |
* @param message the log message.
|
89 |
* @param loggerName the name of the logger.
|
90 |
* @return a formatted string representing the specified log entry information.
|
91 |
*/
|
92 |
public static String format(final String level, final String message, final String loggerName) {
|
93 |
final StringBuilder sb=new StringBuilder(message.length()+40);
|
94 |
if (OutputLevel) sb.append(level).append(": ");
|
95 |
if (OutputName && loggerName!=null) sb.append('[').append(loggerName).append("] ");
|
96 |
sb.append(message);
|
97 |
sb.append(Config.NewLine);
|
98 |
return sb.toString();
|
99 |
}
|
100 |
}
|