Annotation of java/classes/org/w3c/jigsaw/http/ExtendedCommonLogger.java, revision 1.1

1.1     ! ylafon      1: // ExtendedCommonLogger.java
        !             2: // $Id$
        !             3: // (c) COPYRIGHT MIT and INRIA, 1999.
        !             4: // Please first read the full copyright statement in file COPYRIGHT.html
        !             5: 
        !             6: package org.w3c.jigsaw.http ;
        !             7: 
        !             8: import java.io.* ;
        !             9: import java.util.Date ;
        !            10: 
        !            11: import org.w3c.jigsaw.daemon.*;
        !            12: import org.w3c.jigsaw.auth.AuthFilter;
        !            13: import org.w3c.util.*;
        !            14: 
        !            15: /**
        !            16:  * The ExtendedCommonLogger class implements the abstract Logger class.
        !            17:  * It just rotates the log every month and use the extended log format
        !            18:  * @see org.w3c.jigsaw.http.CommonLogger
        !            19:  */
        !            20: 
        !            21: 
        !            22: public class ExtendedCommonLogger extends CommonLogger {
        !            23:     private int month = -1;
        !            24:     private int year  = -1;
        !            25:     private int  tz   = -1;
        !            26: 
        !            27:     private static final String monthnames[] = {
        !            28:        "Jan", "Feb", "Mar", "Apr", "May", "Jun",
        !            29:        "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
        !            30:     };
        !            31:        
        !            32:     /**
        !            33:      * Name of the property indicating the log file.
        !            34:      * This property indicates the name of the log file to use.
        !            35:      * <p>This property defaults to the <code>log</code> file in the server
        !            36:      * log directory.
        !            37:      */
        !            38:     public static final 
        !            39:     String LOGNAME_P = "org.w3c.jigsaw.logger.logname" ;
        !            40:     /**
        !            41:      * Name of the property indicating the error log file.
        !            42:      * This property indicates the name of the error log file to use.
        !            43:      * <p>This property defaults to the <code>errlog</code> file in the
        !            44:      * server log directory.
        !            45:      */
        !            46:     public static final 
        !            47:     String ERRLOGNAME_P = "org.w3c.jigsaw.logger.errlogname" ;
        !            48:     /**
        !            49:      * Name of the property indicating the server trace file.
        !            50:      * This property indicates the name of the trace file to use.
        !            51:      * <p>This property defaults to the <code>trace</code> file in the 
        !            52:      * server log directory.
        !            53:      */
        !            54:     public static final 
        !            55:     String TRACELOGNAME_P = "org.w3c.jigsaw.logger.tracelogname";
        !            56:     /**
        !            57:      * Name of the property indicating the buffer size for the logger.
        !            58:      * This buffer size applies only the the log file, not to the error
        !            59:      * log file, or the trace log file. It can be set to zero if you want
        !            60:      * no buffering.
        !            61:      * <p>This property default to <strong>4096</strong>.
        !            62:      */
        !            63:     public static final 
        !            64:     String BUFSIZE_P = "org.w3c.jigsaw.logger.bufferSize";
        !            65: 
        !            66:     private   byte                 msgbuf[] = null ;
        !            67: 
        !            68:     protected void openLogFile() {
        !            69:        // do nothing
        !            70:     }
        !            71: 
        !            72:     protected void openLogFile(int year, int month) {
        !            73:        this.year = year;
        !            74:        this.month = month;
        !            75:        
        !            76:        String ext = null;
        !            77:        if (month < 9)
        !            78:            ext = "_"+year+"_0"+(month+1);
        !            79:        else
        !            80:            ext = "_"+year+"_"+(month+1);
        !            81: 
        !            82:        String logname = getFilename(LOGNAME_P, "log") + ext;
        !            83:        try {
        !            84:            RandomAccessFile old = log ;
        !            85:            log = new RandomAccessFile (logname, "rw") ;
        !            86:            log.seek (log.length()) ;
        !            87:            if ( old != null )
        !            88:                old.close () ;
        !            89:        } catch (IOException e) {
        !            90:            throw new HTTPRuntimeException (this.getClass().getName()
        !            91:                                            , "openLogFile"
        !            92:                                            , "unable to open "+logname);
        !            93:        }
        !            94:     }
        !            95: 
        !            96:     /**
        !            97:      * Log the given HTTP transaction.
        !            98:      * This is shamelessly slow.
        !            99:      */
        !           100: 
        !           101:     public void log (Request request, Reply reply, int nbytes, long duration) {
        !           102:        Client client = request.getClient() ;
        !           103:        String entry  = null ;
        !           104:        long   date   = reply.getDate();
        !           105:        // Compute the time zone offset, first call only.
        !           106:        if ( tz == -1 ) {
        !           107:            tz = new Date().getTimezoneOffset();
        !           108:        }
        !           109:        Date   now    = (date < 0) ? new Date() : new Date(date+(tz*60*1000));
        !           110: 
        !           111:        if ((now.getYear()+1900) != year || now.getMonth() != month) {
        !           112:            if (log!= null) {
        !           113:                sync();
        !           114:                try {
        !           115:                    log.close();
        !           116:                } catch (IOException ex) {};
        !           117:                log = null;
        !           118:            }
        !           119:            openLogFile(now.getYear()+1900, now.getMonth());
        !           120:        }
        !           121:        String user = (String) request.getState(AuthFilter.STATE_AUTHUSER);
        !           122: 
        !           123:        entry = client.getInetAddress().getHostAddress()
        !           124:            + " " + "-"                            // user name
        !           125:            + " " + ((user == null ) ? "-" : user) // auth user name
        !           126:            + ((now.getDate() < 10) ? " [0" : " [")
        !           127:            + (now.getDate()                       // current date
        !           128:               + "/" + monthnames[now.getMonth()]
        !           129:               + "/" + (now.getYear() + 1900)
        !           130:               + ((now.getHours() < 10)
        !           131:                  ? (":0" + now.getHours())
        !           132:                  : (":" + now.getHours()))
        !           133:               + ((now.getMinutes() < 10)
        !           134:                  ? (":0" + now.getMinutes())
        !           135:                  : (":" + now.getMinutes()))
        !           136:               + ((now.getSeconds() < 10)
        !           137:                  ? (":0" + now.getSeconds())
        !           138:                  : (":" + now.getSeconds()))
        !           139:               + ((date<0) ?
        !           140:                  ((now.getTimezoneOffset() < 0)
        !           141:                   ? " " + (now.getTimezoneOffset() / 60)
        !           142:                   : " +" + (now.getTimezoneOffset() / 60))
        !           143:                  : " +0")
        !           144:               + "]")
        !           145:            + " \"" + request.getMethod()       // request line
        !           146:            + " " + request.getURL()
        !           147:            + " " + request.getVersion()
        !           148:            + "\" " + reply.getStatus()         // reply status
        !           149:            + " " + nbytes                      // # of emited bytes
        !           150:            + " \"" + request.getReferer() + "\"" // the referer (ext log)
        !           151:            + " \"" + request.getUserAgent() + "\"" // User agent
        !           152:            + "\n" ;
        !           153:        logmsg (entry) ;
        !           154:     }
        !           155:     
        !           156:     /**
        !           157:      * Construct a new Logger instance.
        !           158:      */
        !           159:      
        !           160:     ExtendedCommonLogger () {
        !           161:        this.msgbuf = new byte[128] ;
        !           162:     }   
        !           163:     
        !           164: }

Webmaster