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.io.*;
|
24 |
|
25 |
/**
|
26 |
* Provides an implementation of the {@link Logger} interface that sends output to the specified <code>java.io.Writer</code>.
|
27 |
* <p>
|
28 |
* Each log entry is formatted using the {@link BasicLogFormatter#format(String level, String message, String loggerName)} method.
|
29 |
* <p>
|
30 |
* Note that each <a href="Logger.html#LoggingLevel">logging level</a> can be enabled independently in this implementation.
|
31 |
* All levels are enabled by default.
|
32 |
*/
|
33 |
public class WriterLogger implements Logger {
|
34 |
private final Writer writer;
|
35 |
private final String name;
|
36 |
|
37 |
private boolean errorEnabled=true;
|
38 |
private boolean warnEnabled=true;
|
39 |
private boolean infoEnabled=true;
|
40 |
private boolean debugEnabled=true;
|
41 |
|
42 |
/**
|
43 |
* Constructs a new <code>WriterLogger</code> with the specified <code>Writer</code> and the default name.
|
44 |
* <p>
|
45 |
* The default logger name is "<code>net.htmlparser.jericho</code>".
|
46 |
*
|
47 |
* @param writer the <code>Writer</code> to which all output is sent.
|
48 |
*/
|
49 |
public WriterLogger(final Writer writer) {
|
50 |
this(writer,Source.PACKAGE_NAME);
|
51 |
}
|
52 |
|
53 |
/**
|
54 |
* Constructs a new <code>WriterLogger</code> with the specified <code>Writer</code> and name.
|
55 |
* <p>
|
56 |
* The value of the <code>name</code> argument is only relevant if the {@link BasicLogFormatter#OutputName} static property is set to <code>true</code>,
|
57 |
* otherwise the name is not included in the output at all.
|
58 |
*
|
59 |
* @param writer the <code>Writer</code> to which all output is sent.
|
60 |
* @param name the logger name, may be <code>null</code>.
|
61 |
*/
|
62 |
public WriterLogger(final Writer writer, final String name) {
|
63 |
this.writer=writer;
|
64 |
this.name=name;
|
65 |
}
|
66 |
|
67 |
/**
|
68 |
* Returns the <code>Writer</code> to which all output is sent.
|
69 |
* @return the <code>Writer</code> to which all output is sent.
|
70 |
*/
|
71 |
public Writer getWriter() {
|
72 |
return writer;
|
73 |
}
|
74 |
|
75 |
/**
|
76 |
* Returns the name of this logger.
|
77 |
* @return the name of this logger, may be <code>null</code>.
|
78 |
*/
|
79 |
public String getName() {
|
80 |
return name;
|
81 |
}
|
82 |
|
83 |
// Documentation inherited from Logger
|
84 |
public void error(final String message) {
|
85 |
if (isErrorEnabled()) log("ERROR",message);
|
86 |
}
|
87 |
|
88 |
// Documentation inherited from Logger
|
89 |
public void warn(final String message) {
|
90 |
if (isWarnEnabled()) log("WARN",message);
|
91 |
}
|
92 |
|
93 |
// Documentation inherited from Logger
|
94 |
public void info(final String message) {
|
95 |
if (isInfoEnabled()) log("INFO",message);
|
96 |
}
|
97 |
|
98 |
// Documentation inherited from Logger
|
99 |
public void debug(final String message) {
|
100 |
if (isDebugEnabled()) log("DEBUG",message);
|
101 |
}
|
102 |
|
103 |
// Documentation inherited from Logger
|
104 |
public boolean isErrorEnabled() {
|
105 |
return errorEnabled;
|
106 |
}
|
107 |
|
108 |
/**
|
109 |
* Sets whether logging is enabled at the ERROR level.
|
110 |
* @param errorEnabled determines whether logging is enabled at the ERROR level.
|
111 |
*/
|
112 |
public void setErrorEnabled(final boolean errorEnabled) {
|
113 |
this.errorEnabled=errorEnabled;
|
114 |
}
|
115 |
|
116 |
// Documentation inherited from Logger
|
117 |
public boolean isWarnEnabled() {
|
118 |
return warnEnabled;
|
119 |
}
|
120 |
|
121 |
/**
|
122 |
* Sets whether logging is enabled at the WARN level.
|
123 |
* @param warnEnabled determines whether logging is enabled at the WARN level.
|
124 |
*/
|
125 |
public void setWarnEnabled(final boolean warnEnabled) {
|
126 |
this.warnEnabled=warnEnabled;
|
127 |
}
|
128 |
|
129 |
// Documentation inherited from Logger
|
130 |
public boolean isInfoEnabled() {
|
131 |
return infoEnabled;
|
132 |
}
|
133 |
|
134 |
/**
|
135 |
* Sets whether logging is enabled at the INFO level.
|
136 |
* @param infoEnabled determines whether logging is enabled at the INFO level.
|
137 |
*/
|
138 |
public void setInfoEnabled(final boolean infoEnabled) {
|
139 |
this.infoEnabled=infoEnabled;
|
140 |
}
|
141 |
|
142 |
// Documentation inherited from Logger
|
143 |
public boolean isDebugEnabled() {
|
144 |
return debugEnabled;
|
145 |
}
|
146 |
|
147 |
/**
|
148 |
* Sets whether logging is enabled at the DEBUG level.
|
149 |
* @param debugEnabled determines whether logging is enabled at the DEBUG level.
|
150 |
*/
|
151 |
public void setDebugEnabled(final boolean debugEnabled) {
|
152 |
this.debugEnabled=debugEnabled;
|
153 |
}
|
154 |
|
155 |
/**
|
156 |
* Logs the specified message at the specified level.
|
157 |
* <p>
|
158 |
* This method is called internally by the {@link #error(String)}, {@link #warn(String)}, {@link #info(String)} and {@link #debug(String)} methods,
|
159 |
* with the <code>level</code> argument set to the text "<code>ERROR</code>", "<code>WARN</code>", "<code>INFO</code>", or "<code>DEBUG</code>" respectively.
|
160 |
* <p>
|
161 |
* The default implementation of this method sends the the output of
|
162 |
* {@link BasicLogFormatter#format(String,String,String) BasicLogFormatter.format}<code>(level,message,</code>{@link #getName()}<code>)</code>
|
163 |
* to the {@link #getWriter() Writer} specified in the class constructor, and then flushes it.
|
164 |
* <p>
|
165 |
* Overriding this method in a subclass provides a convenient means of logging to a <code>Writer</code> using a different format.
|
166 |
*
|
167 |
* @param level a string representing the level of the log message.
|
168 |
* @param message the message to log.
|
169 |
*/
|
170 |
protected void log(final String level, final String message) {
|
171 |
try {
|
172 |
writer.write(BasicLogFormatter.format(level,message,name));
|
173 |
writer.flush();
|
174 |
} catch (IOException ex) {
|
175 |
throw new RuntimeException(ex);
|
176 |
}
|
177 |
}
|
178 |
}
|