abstractrolloverlog.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 870 行 · 第 1/2 页

JAVA
870
字号
/* * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved * * This file is part of Resin(R) Open Source * * Each copy or derived work must preserve the copyright notice and this * notice unmodified. * * Resin Open Source is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Resin Open Source is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty * of NON-INFRINGEMENT.  See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License * along with Resin Open Source; if not, write to the * *   Free Software Foundation, Inc. *   59 Temple Place, Suite 330 *   Boston, MA 02111-1307  USA * * @author Scott Ferguson */package com.caucho.log;import com.caucho.config.ConfigException;import com.caucho.config.types.Bytes;import com.caucho.config.types.CronType;import com.caucho.config.types.Period;import com.caucho.util.Alarm;import com.caucho.util.L10N;import com.caucho.util.QDate;import com.caucho.util.ThreadPool;import com.caucho.vfs.Path;import com.caucho.vfs.ReadStream;import com.caucho.vfs.TempStream;import com.caucho.vfs.Vfs;import com.caucho.vfs.WriteStream;import java.io.IOException;import java.io.FilterOutputStream;import java.io.OutputStream;import java.util.ArrayList;import java.util.Collections;import java.util.regex.Matcher;import java.util.regex.Pattern;import java.util.zip.GZIPOutputStream;import java.util.zip.ZipOutputStream;import java.util.zip.DeflaterOutputStream;/** * Abstract class for a log that rolls over based on size or period. */public class AbstractRolloverLog {  protected static final L10N L = new L10N(AbstractRolloverLog.class);  // Milliseconds in an hour  private static final long HOUR = 3600L * 1000L;  // Milliseconds in a day  private static final long DAY = 24L * 3600L * 1000L;  // Default maximum log size = 2G  private static final long DEFAULT_ROLLOVER_SIZE = Bytes.INFINITE;  // How often to check size  private static final long DEFAULT_ROLLOVER_CHECK_PERIOD = 600L * 1000L;  private static final long ROLLOVER_OVERFLOW_MAX = 64 * 1024 * 1024;  // prefix for the rollover  private String _rolloverPrefix;  // template for the archived files  private String _archiveFormat;  // .gz or .zip  private String _archiveSuffix = "";  // Cron description of the rollover  private CronType _rolloverCron;  // How often the logs are rolled over.  private long _rolloverPeriod = Period.INFINITE;  // Maximum size of the log.  private long _rolloverSize = DEFAULT_ROLLOVER_SIZE;  // How often the rolloverSize should be checked  //private long _rolloverCheckPeriod = DEFAULT_ROLLOVER_CHECK_PERIOD;  private long _rolloverCheckPeriod = 120 * 1000;  // How many archives are allowed.  private int _rolloverCount;  private QDate _calendar = QDate.createLocal();  private Path _pwd = Vfs.lookup();    protected Path _path;  protected String _pathFormat;  private String _format;  // The time of the next period-based rollover  private long _nextPeriodEnd = -1;  private long _nextRolloverCheckTime = -1;  private long _lastTime;  private boolean _isRollingOver;  private Path _savedPath;  private TempStream _tempStream;  private long _tempStreamSize;  private ArchiveTask _archiveTask = new ArchiveTask();  private WriteStream _os;  private WriteStream _zipOut;  /**   * Returns the access-log's path.   */  public Path getPath()  {    return _path;  }  /**   * Sets the access-log's path.   */  public void setPath(Path path)  {    _path = path;  }  /**   * Returns the pwd for the rollover log   */  public Path getPwd()  {    return _pwd;  }  /**   * Returns the formatted path   */  public String getPathFormat()  {    return _pathFormat;  }  /**   * Sets the formatted path.   */  public void setPathFormat(String pathFormat)    throws ConfigException  {    _pathFormat = pathFormat;        if (pathFormat.endsWith(".zip")) {      throw new ConfigException(L.l(".zip extension to path-format is not supported."));    }  }  /**   * Sets the archive name format   */  public void setArchiveFormat(String format)  {    if (format.endsWith(".gz")) {      _archiveFormat = format.substring(0, format.length() - ".gz".length());      _archiveSuffix = ".gz";    }    else if (format.endsWith(".zip")) {      _archiveFormat = format.substring(0, format.length() - ".zip".length());      _archiveSuffix = ".zip";    }    else {      _archiveFormat = format;      _archiveSuffix = "";    }  }  /**   * Sets the archive name format   */  public String getArchiveFormat()  {    if (_archiveFormat == null)      return _rolloverPrefix + ".%Y%m%d.%H%M";    else      return _archiveFormat;  }  /**   * Sets the log rollover cron specification   */  public void setRolloverCron(CronType cron)  {    _rolloverCron = cron;  }  /**   * Sets the log rollover period, rounded up to the nearest hour.   *   * @param period the new rollover period in milliseconds.   */  public void setRolloverPeriod(Period period)  {    _rolloverPeriod = period.getPeriod();        if (_rolloverPeriod > 0) {      _rolloverPeriod += 3600000L - 1;      _rolloverPeriod -= _rolloverPeriod % 3600000L;    }    else      _rolloverPeriod = Period.INFINITE;  }  /**   * Sets the log rollover period, rounded up to the nearest hour.   *   * @return the new period in milliseconds.   */  public long getRolloverPeriod()  {    return _rolloverPeriod;  }  /**   * Sets the log rollover size, rounded up to the megabyte.   *   * @param bytes maximum size of the log file   */  public void setRolloverSize(Bytes bytes)  {    long size = bytes.getBytes();        if (size < 0)      _rolloverSize = Bytes.INFINITE;    else      _rolloverSize = size;  }  /**   * Sets the log rollover size, rounded up to the megabyte.   *   * @return maximum size of the log file   */  public long getRolloverSize()  {    return _rolloverSize;  }  /**   * Sets how often the log rollover will be checked.   *   * @param period how often the log rollover will be checked.   */  public void setRolloverCheckPeriod(long period)  {    if (period > 1000)      _rolloverCheckPeriod = period;    else if (period > 0)      _rolloverCheckPeriod = 1000;  }  /**   * Sets how often the log rollover will be checked.   *   * @return how often the log rollover will be checked.   */  public long getRolloverCheckPeriod()  {    return _rolloverCheckPeriod;  }  /**   * Sets the max rollover files.   */  public void setRolloverCount(int count)  {    _rolloverCount = count;  }  public void setLastTime(long lastTime)  {    _lastTime = lastTime;  }    /**   * Initialize the log.   */  public void init()    throws IOException  {    long now = Alarm.getExactTime();        _nextRolloverCheckTime = now + _rolloverCheckPeriod;    Path path = getPath();    if (path != null) {      path.getParent().mkdirs();          _rolloverPrefix = path.getTail();      long lastModified = path.getLastModified();      if (lastModified <= 0)	lastModified = now;          _calendar.setGMTTime(lastModified);      if (_rolloverCron != null)	_nextPeriodEnd = _rolloverCron.nextTime(lastModified);      else	_nextPeriodEnd = Period.periodEnd(lastModified, getRolloverPeriod());    }    else {      if (_rolloverCron != null)	_nextPeriodEnd = _rolloverCron.nextTime(now);      else	_nextPeriodEnd = Period.periodEnd(now, getRolloverPeriod());    }    if (_nextPeriodEnd < _nextRolloverCheckTime && _nextPeriodEnd > 0)      _nextRolloverCheckTime = _nextPeriodEnd;    if (_archiveFormat != null || getRolloverPeriod() <= 0) {    }    else if (_rolloverCron != null)      _archiveFormat = _rolloverPrefix + ".%Y%m%d.%H";    else if (getRolloverPeriod() % DAY == 0)      _archiveFormat = _rolloverPrefix + ".%Y%m%d";    else if (getRolloverPeriod() % HOUR == 0)      _archiveFormat = _rolloverPrefix + ".%Y%m%d.%H";    else      _archiveFormat = _rolloverPrefix + ".%Y%m%d.%H%M";    rolloverLog();  }  public long getNextRolloverCheckTime()  {    if (_nextPeriodEnd < _nextRolloverCheckTime)      return _nextPeriodEnd;    else      return _nextRolloverCheckTime;  }  public boolean isRollover()  {    long now = Alarm.getCurrentTime();        return _nextPeriodEnd <= now || _nextRolloverCheckTime <= now;  }  public boolean rollover()  {    long now = Alarm.getCurrentTime();    if (_nextPeriodEnd <= now || _nextRolloverCheckTime <= now) {      rolloverLog();      return true;    }    else      return false;  }  /**   * Writes to the underlying log.   */  protected void write(byte []buffer, int offset, int length)    throws IOException  {    synchronized (this) {      if (_isRollingOver && ROLLOVER_OVERFLOW_MAX < _tempStreamSize) {	try {	  wait();	} catch (Exception e) {	}      }            if (! _isRollingOver) {	if (_os == null)	  openLog();	if (_os != null)	  _os.write(buffer, offset, length);      }      else {	if (_tempStream == null) {	  _tempStream = new TempStream();	  _tempStreamSize = 0;	}	_tempStreamSize += length;	_tempStream.write(buffer, offset, length, false);      }    }  }  /**   * Writes to the underlying log.   */  protected void flush()    throws IOException  {    synchronized (this) {      if (_os != null)	_os.flush();      if (_zipOut != null)	_zipOut.flush();    }  }  /**   * Check to see if we need to rollover the log.   *   * @param now current time in milliseconds.   */  protected void rolloverLog()  {    long now = Alarm.getExactTime();        boolean isRollingOver = false;        try {      Path savedPath = null;      synchronized (this) {

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?