📄 commonlogger.java
字号:
// CommonLogger.java// $Id: CommonLogger.java,v 1.50 2002/09/18 15:22:12 ylafon Exp $// (c) COPYRIGHT MIT and INRIA, 1996.// Please first read the full copyright statement in file COPYRIGHT.htmlpackage org.w3c.jigsaw.http ;import java.io.File;import java.io.IOException;import java.io.RandomAccessFile;import java.net.URL;import java.util.Calendar;import java.util.Date;import java.util.TimeZone;import org.w3c.jigsaw.auth.AuthFilter;import org.w3c.util.ObservableProperties;import org.w3c.util.PropertyMonitoring;/** * The CommonLogger class implements the abstract Logger class. * The resulting log will conform to the * <a href="http://www.w3.org/Daemon/User/Config/Logging.html#common-logfile-format">common log format</a>). * @see org.w3c.jigsaw.http.Logger */public class CommonLogger extends Logger implements PropertyMonitoring { protected static final String monthnames[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; protected static String noUrl = "*NOURL*"; /** * Name of the property indicating the log file. * This property indicates the name of the log file to use. * <p>This property defaults to the <code>log</code> file in the server * log directory. */ public static final String LOGNAME_P = "org.w3c.jigsaw.logger.logname" ; /** * Name of the property indicating the error log file. * This property indicates the name of the error log file to use. * <p>This property defaults to the <code>errlog</code> file in the * server log directory. */ public static final String ERRLOGNAME_P = "org.w3c.jigsaw.logger.errlogname" ; /** * Name of the property indicating the server trace file. * This property indicates the name of the trace file to use. * <p>This property defaults to the <code>trace</code> file in the * server log directory. */ public static final String LOGDIRNAME_P = "org.w3c.jigsaw.logger.logdirname"; /** * Name of the property indicating the server log directory. * <p>This property defaults to the <code>logs</code> directory in the * server main directory. */ public static final String TRACELOGNAME_P = "org.w3c.jigsaw.logger.tracelogname"; /** * Name of the property indicating the buffer size for the logger. * This buffer size applies only the the log file, not to the error * log file, or the trace log file. It can be set to zero if you want * no buffering. * <p>This property default to <strong>4096</strong>. */ public static final String BUFSIZE_P = "org.w3c.jigsaw.logger.bufferSize"; /** * Name of the property indicating the buffer size for the logger. * This buffer size applies only the the log file, not to the error * log file, or the trace log file. It can be set to zero if you want * no buffering. * <p>This property default to <strong>4096</strong>. */ public static final String ROTATE_LEVEL_P = "org.w3c.jigsaw.logger.rotateLevel"; private byte msgbuf[] = null ; protected RandomAccessFile log = null ; protected RandomAccessFile errlog = null ; protected RandomAccessFile trace = null ; protected httpd server = null ; protected ObservableProperties props = null ; protected int bufsize = 8192 ; protected int bufptr = 0 ; protected int rotateLevel = 0 ; protected byte buffer[] = null ; protected int year = -1 ; protected int month = -1 ; protected int day = -1 ; protected int hour = -1 ; private Calendar cal = null ; private long datestamp = -1 ; private char datecache[] = { 'D','D','/','M','M','M', '/','Y','Y','Y','Y',':', 'H','H',':','M','M',':', 'S','S',' ','+','0','0', '0','0'}; /** * Property monitoring for the logger. * The logger allows you to dynamically (typically through the property * setter) change the names of the file to which it logs error, access * and traces. * @param name The name of the property that has changed. * @return A boolean, <strong>true</strong> if the change was made, * <strong>false</strong> otherwise. */ public boolean propertyChanged (String name) { if ( name.equals(LOGNAME_P) ) { try { openLogFile () ; } catch (Exception e) { e.printStackTrace() ; return false ; } return true ; } else if ( name.equals(ERRLOGNAME_P) ) { try { openErrorLogFile() ; } catch (Exception e) { e.printStackTrace() ; return false ; } return true ; } else if ( name.equals(TRACELOGNAME_P) ) { try { openTraceFile() ; } catch (Exception e) { e.printStackTrace() ; return false ; } return true ; } else if ( name.equals(LOGDIRNAME_P) ) { try { openLogFile () ; openErrorLogFile() ; openTraceFile() ; } catch (Exception e) { e.printStackTrace() ; return false ; } return true ; } else if ( name.equals(BUFSIZE_P) ) { synchronized (this) { bufsize = props.getInteger(name, bufsize); // Reset buffer before resizing: if ( bufptr > 0 ) { try { log.write(buffer, 0, bufptr); bufptr = 0; } catch (IOException ex) { } } // Set new buffer: buffer = (bufsize > 0) ? new byte[bufsize] : null; return true; } } else if (name.equals(ROTATE_LEVEL_P) ) { int newLevel = props.getInteger(name, rotateLevel); if (newLevel != rotateLevel) { synchronized (this) { sync(); rotateLevel = newLevel; openLogFile(); } } return true; } else { return true ; } } /** * Output the given message to the given RandomAccessFile. * This method makes its best effort to avoid one byte writes (which you * get when writing the string as a whole). It first copies the string * bytes into a private byte array, and than, write them all at once. * @param f The RandomAccessFile to write to, which should be one of * log, errlog or trace. * @param msg The message to be written. * @exception IOException If writing to the output failed. */ protected synchronized void output (RandomAccessFile f, String msg) throws IOException { int len = msg.length() ; if ( len > msgbuf.length ) msgbuf = new byte[len] ; msg.getBytes (0, len, msgbuf, 0) ; f.write (msgbuf, 0, len) ; } protected synchronized void appendLogBuffer(String msg) throws IOException { int msglen = msg.length(); if ( bufptr + msglen > buffer.length ) { // Flush the buffer: log.write(buffer, 0, bufptr); bufptr = 0; // Check for messages greater then buffer: if ( msglen > buffer.length ) { byte huge[] = new byte[msglen]; msg.getBytes(0, msglen, huge, 0); log.write(huge, 0, msglen); return; } } // Append that message to buffer: msg.getBytes(0, msglen, buffer, bufptr); bufptr += msglen; } protected void logmsg (String msg) { if ( log != null ) { try { if ( buffer == null ) { output (log, msg) ; } else { appendLogBuffer(msg); } } catch (IOException e) { throw new HTTPRuntimeException (this,"logmsg",e.getMessage()) ; } } } protected void errlogmsg (String msg) { if ( errlog != null ) { try { output (errlog, msg) ; } catch (IOException e) { throw new HTTPRuntimeException (this , "errlogmsg" , e.getMessage()) ; } } } protected void tracemsg (String msg) { if ( trace != null ) { try { output (trace, msg) ; } catch (IOException e) { throw new HTTPRuntimeException (this , "tracemsg" , e.getMessage()) ; } } } protected synchronized void checkLogFile(Date now) { if (cal == null) { TimeZone tz = TimeZone.getTimeZone("UTC"); cal = Calendar.getInstance(tz); } cal.setTime(now); int nowYear = cal.get(Calendar.YEAR); if (rotateLevel == 1) { // rotate every year if (nowYear != year) { if (log != null) { sync(); } this.year = nowYear; openLogFile(year); } } else { int nowMonth = cal.get(Calendar.MONTH); if (rotateLevel == 2) { // rotate every month if ((nowYear != year) || (nowMonth != month)) { if (log != null) { sync(); } this.year = nowYear; this.month = nowMonth; openLogFile(year, month); } } else { int nowDay = cal.get(Calendar.DAY_OF_MONTH); if (rotateLevel == 3) { // rotate every day if ((nowYear != year) || (nowMonth != month) || (nowDay != day)) { if (log != null) { sync(); } this.year = nowYear; this.month = nowMonth; this.day = nowDay; openLogFile(year, month, day); } } } } } protected void openLogFile(int year, int month, int day) { this.year = year; this.month = month; this.day = day; String ext = null; if (month < 9) { if (day < 10) { ext = "_"+year+"_0"+(month+1)+"_0"+day; } else { ext = "_"+year+"_0"+(month+1)+"_"+day; } } else { if (day < 10) { ext = "_"+year+"_"+(month+1)+"_0"+day; } else { ext = "_"+year+"_"+(month+1)+"_"+day; } } String logname = getFilename(LOGNAME_P, "log") + ext; try { RandomAccessFile old = log ; log = new RandomAccessFile (logname, "rw") ; log.seek (log.length()) ; if ( old != null ) old.close () ; } catch (IOException e) { throw new HTTPRuntimeException (this.getClass().getName() , "openLogFile" , "unable to open "+logname); } } protected void openLogFile(int year, int month) { this.year = year; this.month = month; String ext = null; if (month < 9) ext = "_"+year+"_0"+(month+1); else ext = "_"+year+"_"+(month+1); String logname = getFilename(LOGNAME_P, "log") + ext; try { RandomAccessFile old = log ; log = new RandomAccessFile (logname, "rw") ; log.seek (log.length()) ; if ( old != null ) old.close () ; } catch (IOException e) { throw new HTTPRuntimeException (this.getClass().getName() , "openLogFile" , "unable to open "+logname); } } protected void openLogFile(int year) { this.year = year; String logname = getFilename(LOGNAME_P, "log") + "_" + year; try { RandomAccessFile old = log ; log = new RandomAccessFile (logname, "rw") ; log.seek (log.length()) ; if ( old != null ) old.close () ; } catch (IOException e) { throw new HTTPRuntimeException (this.getClass().getName() , "openLogFile" , "unable to open "+logname); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -