001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.log;
031:
032: import com.caucho.util.L10N;
033: import com.caucho.vfs.Path;
034: import com.caucho.vfs.WriteStream;
035:
036: import java.io.IOException;
037: import java.util.logging.Handler;
038: import java.util.logging.LogRecord;
039:
040: /**
041: * Resin's rotating path-based log.
042: */
043: public class PathHandler extends Handler {
044: private static final L10N L = new L10N(PathHandler.class);
045:
046: private Path _path;
047: private String _encoding;
048:
049: public PathHandler(Path path) {
050: _path = path;
051: }
052:
053: /**
054: * Publishes the record.
055: */
056: public void publish(LogRecord record) {
057: if (record.getLevel().intValue() < getLevel().intValue())
058: return;
059:
060: WriteStream os = null;
061: synchronized (_path) {
062: try {
063: try {
064: os = _path.openAppend();
065: } catch (Throwable e) {
066: _path.getParent().mkdirs();
067: os = _path.openAppend();
068: }
069:
070: if (_encoding != null)
071: os.setEncoding(_encoding);
072:
073: String msg = record.getMessage();
074: os.println(msg);
075: } catch (Throwable e) {
076: e.printStackTrace();
077: } finally {
078: try {
079: if (os != null)
080: os.close();
081: } catch (IOException e) {
082: }
083: }
084: }
085: }
086:
087: /**
088: * Flushes the buffer.
089: */
090: public void flush() {
091: }
092:
093: /**
094: * Closes the handler.
095: */
096: public void close() {
097: }
098:
099: /**
100: * Returns the hash code.
101: */
102: public int hashCode() {
103: return _path.hashCode();
104: }
105:
106: /**
107: * Test for equality.
108: */
109: public boolean equals(Object o) {
110: if (this == o)
111: return true;
112: else if (!(o instanceof PathHandler))
113: return false;
114:
115: PathHandler handler = (PathHandler) o;
116:
117: return _path.equals(handler._path);
118: }
119:
120: public String toString() {
121: return "PathHandler[" + _path + "]";
122: }
123: }
|