001: // Jericho HTML Parser - Java based library for analysing and manipulating HTML
002: // Version 2.5
003: // Copyright (C) 2007 Martin Jericho
004: // http://jerichohtml.sourceforge.net/
005: //
006: // This library is free software; you can redistribute it and/or
007: // modify it under the terms of either one of the following licences:
008: //
009: // 1. The Eclipse Public License (EPL) version 1.0,
010: // included in this distribution in the file licence-epl-1.0.html
011: // or available at http://www.eclipse.org/legal/epl-v10.html
012: //
013: // 2. The GNU Lesser General Public License (LGPL) version 2.1 or later,
014: // included in this distribution in the file licence-lgpl-2.1.txt
015: // or available at http://www.gnu.org/licenses/lgpl.txt
016: //
017: // This library is distributed on an "AS IS" basis,
018: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
019: // See the individual licence texts for more details.
020:
021: package au.id.jericho.lib.html;
022:
023: import java.io.*;
024:
025: /**
026: * Provides an implementation of the {@link Logger} interface that sends output to the specified <code>java.io.Writer</code>.
027: * <p>
028: * Each log entry is formatted using the {@link BasicLogFormatter#format(String level, String message, String loggerName)} method.
029: * <p>
030: * Note that each <a href="Logger.html#LoggingLevel">logging level</a> can be enabled independently in this implementation.
031: */
032: public class WriterLogger implements Logger {
033: private final Writer writer;
034: private final String name;
035:
036: private boolean errorEnabled = true;
037: private boolean warnEnabled = true;
038: private boolean infoEnabled = true;
039: private boolean debugEnabled = true;
040:
041: /**
042: * Constructs a new <code>WriterLogger</code> with the specified <code>Writer</code> and the default name.
043: * <p>
044: * The default logger name is "<code>net.htmlparser.jericho</code>".
045: *
046: * @param writer the <code>Writer</code> to which all output is sent.
047: */
048: public WriterLogger(final Writer writer) {
049: this (writer, Source.PACKAGE_NAME);
050: }
051:
052: /**
053: * Constructs a new <code>WriterLogger</code> with the specified <code>Writer</code> and name.
054: * <p>
055: * The value of the <code>name</code> argument is only relevant if the {@link BasicLogFormatter#OutputName} static property is set to <code>true</code>,
056: * otherwise the name is not included in the output at all.
057: *
058: * @param writer the <code>Writer</code> to which all output is sent.
059: * @param name the logger name, may be <code>null</code>.
060: */
061: public WriterLogger(final Writer writer, final String name) {
062: this .writer = writer;
063: this .name = name;
064: }
065:
066: /**
067: * Returns the <code>Writer</code> to which all output is sent.
068: * @return the <code>Writer</code> to which all output is sent.
069: */
070: public Writer getWriter() {
071: return writer;
072: }
073:
074: /**
075: * Returns the name of this logger.
076: * @return the name of this logger, may be <code>null</code>.
077: */
078: public String getName() {
079: return name;
080: }
081:
082: // Documentation inherited from Logger
083: public void error(final String message) {
084: if (isErrorEnabled())
085: log("ERROR", message);
086: }
087:
088: // Documentation inherited from Logger
089: public void warn(final String message) {
090: if (isWarnEnabled())
091: log("WARN", message);
092: }
093:
094: // Documentation inherited from Logger
095: public void info(final String message) {
096: if (isInfoEnabled())
097: log("INFO", message);
098: }
099:
100: // Documentation inherited from Logger
101: public void debug(final String message) {
102: if (isDebugEnabled())
103: log("DEBUG", message);
104: }
105:
106: // Documentation inherited from Logger
107: public boolean isErrorEnabled() {
108: return errorEnabled;
109: }
110:
111: /**
112: * Sets whether logging is enabled at the ERROR level.
113: * @param errorEnabled determines whether logging is enabled at the ERROR level.
114: */
115: public void setErrorEnabled(final boolean errorEnabled) {
116: this .errorEnabled = errorEnabled;
117: }
118:
119: // Documentation inherited from Logger
120: public boolean isWarnEnabled() {
121: return warnEnabled;
122: }
123:
124: /**
125: * Sets whether logging is enabled at the WARN level.
126: * @param warnEnabled determines whether logging is enabled at the WARN level.
127: */
128: public void setWarnEnabled(final boolean warnEnabled) {
129: this .warnEnabled = warnEnabled;
130: }
131:
132: // Documentation inherited from Logger
133: public boolean isInfoEnabled() {
134: return infoEnabled;
135: }
136:
137: /**
138: * Sets whether logging is enabled at the INFO level.
139: * @param infoEnabled determines whether logging is enabled at the INFO level.
140: */
141: public void setInfoEnabled(final boolean infoEnabled) {
142: this .infoEnabled = infoEnabled;
143: }
144:
145: // Documentation inherited from Logger
146: public boolean isDebugEnabled() {
147: return debugEnabled;
148: }
149:
150: /**
151: * Sets whether logging is enabled at the DEBUG level.
152: * @param debugEnabled determines whether logging is enabled at the DEBUG level.
153: */
154: public void setDebugEnabled(final boolean debugEnabled) {
155: this .debugEnabled = debugEnabled;
156: }
157:
158: private void log(final String level, final String message) {
159: try {
160: writer
161: .write(BasicLogFormatter.format(level, message,
162: name));
163: writer.flush();
164: } catch (IOException ex) {
165: throw new RuntimeException(ex);
166: }
167: }
168: }
|