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.config.ConfigException;
033: import com.caucho.config.types.Bytes;
034: import com.caucho.config.types.Period;
035: import com.caucho.util.L10N;
036: import com.caucho.vfs.Path;
037:
038: import javax.annotation.PostConstruct;
039: import java.io.IOException;
040:
041: /**
042: * Configuration for a rotating log
043: */
044: public class RotateLog {
045: private final static L10N L = new L10N(RotateLog.class);
046:
047: private static final long ROLLOVER_SIZE = Long.MAX_VALUE / 2;
048:
049: private Path _path;
050: private String _pathFormat;
051: private String _archiveFormat;
052:
053: private Period _rolloverPeriod;
054: private Bytes _rolloverSize;
055: private int _rolloverCount = -1;
056:
057: private RotateStream _rotateStream;
058:
059: private String _timestamp;
060:
061: /**
062: * Gets the output path.
063: */
064: public Path getPath() {
065: return _path;
066: }
067:
068: /**
069: * Sets the output path.
070: */
071: public void setPath(Path path) {
072: _path = path;
073: }
074:
075: /**
076: * Gets the output path.
077: */
078: public String getPathFormat() {
079: return _pathFormat;
080: }
081:
082: /**
083: * Sets the output path.
084: */
085: public void setPathFormat(String path) {
086: _pathFormat = path;
087: }
088:
089: /**
090: * Sets the output path (backward compat).
091: */
092: public void setHref(Path path) {
093: setPath(path);
094: }
095:
096: /**
097: * Sets the rollover period.
098: */
099: public void setRolloverPeriod(Period period) {
100: _rolloverPeriod = period;
101: }
102:
103: /**
104: * Sets the rollover size.
105: */
106: public void setRolloverSize(Bytes size) {
107: _rolloverSize = size;
108: }
109:
110: /**
111: * Sets the rollover count
112: */
113: public int getRolloverCount() {
114: return _rolloverCount;
115: }
116:
117: /**
118: * Sets the rollover count.
119: */
120: public void setRolloverCount(int count) {
121: _rolloverCount = count;
122: }
123:
124: /**
125: * Sets the timestamp
126: */
127: public String getTimestamp() {
128: return _timestamp;
129: }
130:
131: /**
132: * Sets the timestamp.
133: */
134: /*
135: public void setTimestamp(String timestamp)
136: {
137: _timestamp = timestamp;
138: }
139: */
140:
141: /**
142: * Gets the archive format
143: */
144: public String getArchiveFormat() {
145: return _archiveFormat;
146: }
147:
148: /**
149: * Sets the archive format.
150: */
151: public void setArchiveFormat(String format) {
152: _archiveFormat = format;
153: }
154:
155: /**
156: * Returns the rotated stream.
157: */
158: public RotateStream getRotateStream() {
159: return _rotateStream;
160: }
161:
162: /**
163: * Returns the tag name.
164: */
165: public String getTagName() {
166: return "rotate-log";
167: }
168:
169: /**
170: * Initialize the log.
171: */
172: @PostConstruct
173: public void init() throws ConfigException, IOException {
174: if (_path != null)
175: _rotateStream = RotateStream.create(_path);
176: else if (_pathFormat != null)
177: _rotateStream = RotateStream.create(_pathFormat);
178: else
179: throw new ConfigException(
180: L
181: .l(
182: "`path' is a required attribute of <{0}>. Each <{0}> must configure the destination stream.",
183: getTagName()));
184:
185: if (_path != null
186: && _path.exists()
187: && !_path.canRead()
188: && (_rolloverPeriod != null || _rolloverSize != null || _archiveFormat != null)) {
189: throw new ConfigException(
190: L
191: .l(
192: "log path '{0}' is not readable and therefore cannot be rotated.",
193: _path.getURL()));
194: }
195:
196: AbstractRolloverLog rolloverLog = _rotateStream
197: .getRolloverLog();
198:
199: if (_rolloverPeriod != null)
200: rolloverLog.setRolloverPeriod(_rolloverPeriod);
201:
202: if (_rolloverSize != null)
203: rolloverLog.setRolloverSize(_rolloverSize);
204: _rotateStream.setMaxRolloverCount(_rolloverCount);
205: if (_archiveFormat != null)
206: rolloverLog.setArchiveFormat(_archiveFormat);
207:
208: _rotateStream.init();
209: }
210: }
|