001: // ExtendedCommonLogger.java
002: // $Id: ExtendedCommonLogger.java,v 1.16 2002/09/18 15:22:12 ylafon Exp $
003: // (c) COPYRIGHT MIT and INRIA, 1999.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.jigsaw.http;
007:
008: import java.net.URL;
009:
010: import java.util.Date;
011:
012: import org.w3c.jigsaw.auth.AuthFilter;
013:
014: /**
015: * The ExtendedCommonLogger class implements the abstract Logger class.
016: * It just rotates the log every month and use the extended log format
017: * @see org.w3c.jigsaw.http.CommonLogger
018: */
019:
020: public class ExtendedCommonLogger extends CommonLogger {
021:
022: private byte msgbuf[] = null;
023:
024: /**
025: * Log the given HTTP transaction.
026: * This is shamelessly slow.
027: */
028:
029: public void log(Request request, Reply reply, int nbytes,
030: long duration) {
031: Client client = request.getClient();
032: long date = reply.getDate();
033:
034: String user = (String) request
035: .getState(AuthFilter.STATE_AUTHUSER);
036: URL urlst = (URL) request.getState(Request.ORIG_URL_STATE);
037: String requrl;
038: if (urlst == null) {
039: URL u = request.getURL();
040: if (u == null) {
041: requrl = noUrl;
042: } else {
043: requrl = u.toExternalForm();
044: }
045: } else {
046: requrl = urlst.toExternalForm();
047: }
048: StringBuffer sb = new StringBuffer(512);
049: String logs;
050: int status = reply.getStatus();
051: if ((status > 999) || (status < 0)) {
052: status = 999; // means unknown
053: }
054: synchronized (sb) {
055: byte ib[] = client.getInetAddress().getAddress();
056: if (ib.length == 4) {
057: boolean doit;
058: for (int i = 0; i < 4; i++) {
059: doit = false;
060: int b = ib[i];
061: if (b < 0) {
062: b += 256;
063: }
064: if (b > 99) {
065: sb.append((char) ('0' + (b / 100)));
066: b = b % 100;
067: doit = true;
068: }
069: if (doit || (b > 9)) {
070: sb.append((char) ('0' + (b / 10)));
071: b = b % 10;
072: }
073: sb.append((char) ('0' + b));
074: if (i < 3) {
075: sb.append('.');
076: }
077: }
078: } else { // ipv6, let's be safe :)
079: sb.append(client.getInetAddress().getHostAddress());
080: }
081: sb.append(" - ");
082: if (user == null) {
083: sb.append("- [");
084: } else {
085: sb.append(user);
086: sb.append(" [");
087: }
088: dateCache(date, sb);
089: sb.append("] \"");
090: sb.append(request.getMethod());
091: sb.append(' ');
092: sb.append(requrl);
093: sb.append(' ');
094: sb.append(request.getVersion());
095: sb.append("\" ");
096: sb.append((char) ('0' + status / 100));
097: status = status % 100;
098: sb.append((char) ('0' + status / 10));
099: status = status % 10;
100: sb.append((char) ('0' + status));
101: sb.append(' ');
102: if (nbytes < 0) {
103: sb.append('-');
104: } else {
105: sb.append(nbytes);
106: }
107: if (request.getReferer() == null) {
108: if (request.getUserAgent() == null) {
109: sb.append(" \"-\" \"-\"");
110: } else {
111: sb.append(" \"-\" \"");
112: sb.append(request.getUserAgent());
113: sb.append('\"');
114: }
115: } else {
116: if (request.getUserAgent() == null) {
117: sb.append(" \"");
118: sb.append(request.getReferer());
119: sb.append("\" \"-\"");
120: } else {
121: sb.append(" \"");
122: sb.append(request.getReferer());
123: sb.append("\" \"");
124: sb.append(request.getUserAgent());
125: sb.append('\"');
126: }
127: }
128: sb.append('\n');
129: logs = sb.toString();
130: }
131: logmsg(logs);
132: }
133:
134: /**
135: * Construct a new Logger instance.
136: */
137:
138: ExtendedCommonLogger() {
139: this .msgbuf = new byte[128];
140: }
141:
142: }
|