【Java开源代码栏目提醒】:网学会员在Java开源代码频道为大家收集整理了WriterLogSink.java提供大家参考,希望对大家有所帮助!
// ========================================================================
// Copyright (c) 1997 MortBay Consulting, Sydney
// $Id: WriterLogSink.java,v 1.14 2000/12/17 10:45:47 gregwilkins Exp $
// ========================================================================
package com.mortbay.Util;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.io.FileWriter;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import java.util.StringTokenizer;
import java.util.Calendar;
import java.util.GregorianCalendar;
/* ------------------------------------------------------------ */
/** A Log sink.
* This class represents both a concrete or abstract sink of
* Log data. The default implementation logs to a PrintWriter, but
* derived implementations may log to files, syslog, or other
* logging APIs.
*
* If a logFilename is specified, output is sent to that file.
* If the filename contains "yyyy_mm_dd", the log file date format
* is used to create the actual filename and the log file is rolled
* over at local midnight.
* If append is set, existing logfiles are appended to, otherwise
* a backup is created with a timestamp.
* Dated log files are deleted after retain days.
*
* @version $Id: WriterLogSink.java,v 1.14 2000/12/17 10:45:47 gregwilkins Exp $
* @author Greg Wilkins (gregw)
*/
public class WriterLogSink
implements LogSink
{
/*-------------------------------------------------------------------*/
public final static char OPT_TIMESTAMP = 't';
public final static char OPT_LABEL = 'L';
public final static char OPT_TAG = 'T';
public final static char OPT_STACKSIZE = 's';
public final static char OPT_STACKTRACE = 'S';
public final static char OPT_ONELINE = 'O';
/* ------------------------------------------------------------ */
private final static String __lineSeparator =
System.getProperty("line.separator");
private final static String __indentBase ="";
private final static String __indentSeparator =
__lineSeparator+__indentBase;
private final static int __lineSeparatorLen =
__lineSeparator.length();
private final static String YYYY_MM_DD="yyyy_mm_dd";
private static SimpleDateFormat __fileBackupFormat =
new SimpleDateFormat(System.getProperty("LOG_FILE_BACKUP_FORMAT","HHmmssSSS"));
/*-------------------------------------------------------------------*/
private SimpleDateFormat _fileDateFormat =
new SimpleDateFormat(System.getProperty("LOG_FILE_DATE_FORMAT","yyyy_MM_dd"));
private int _retainDays =Integer.getInteger("LOG_FILE_RETAIN_DAYS",31).intValue();
protected DateCache _dateFormat=
new DateCache(System.getProperty("LOG_DATE_FORMAT","HH:mm:ss.SSS "));
protected String _logTimezone=
System.getProperty("LOG_TIME_ZONE");
{
if (_logTimezone!=null)
_dateFormat.getFormat().setTimeZone(TimeZone.getTimeZone(_logTimezone));
}
/* ------------------------------------------------------------ */
protected boolean _logTimeStamps=true;
protected boolean _logLabels=true;
protected boolean _logTags=true;
protected boolean _logStackSize=true;
protected boolean _logStackTrace=false;
protected boolean _logOneLine=false;
/*-------------------------------------------------------------------*/
protected PrintWriter _out;
protected boolean _started;
private String _filename;
private boolean _append=true;
private StringBuffer _stringBuffer = new StringBuffer(512);
private Thread _rollover;
/* ------------------------------------------------------------ */
/** Constr