2 //****************************************************************************
3 // Programmer: Duane M. Gran, ragnar@cs.bsu.edu
4 // Program: JhttpServer
5 // Date: April 24, 1998
6 //****************************************************************************
12 import java.text.SimpleDateFormat;
15 //****************************************************************************
17 // Purpose: Handle the behavior for logging connections. The methods simply
18 // add to the private data fields. Has implementation for standard
19 // output, as well as output to file.
21 // An example log entry looks like:
23 // 1.2.3.4 - - [29/JAN/1998:21:40:30 -06] "GET /file.html HTTP/1.0" 200 472
25 //****************************************************************************
29 static public final String log_file_name = "server.log";
31 static public String write_log(Socket s, String Method, String URI,
33 int ReturnCode, long BytesSent) {
35 // Socket.toString() calls (indirectly) some Hashtable.get
36 // method - I tool care of it!
39 String addr = s.toString();
40 String Address = addr.substring(addr.indexOf('/') + 1,
44 // SimpleDateFormat sdf =
45 // new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz"); // RFC 1123
46 // sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
47 // String Date = sdf.format(new Date());
50 /* Address + */ " - - [" + // IP address
51 "Date" + "] \"" + // date
52 Method + " " + // get, post, head
53 URI + " " + // filename
54 Protocol + "\" " + // http/1.?
55 ReturnCode + " " + // 200-500
56 BytesSent + "\n"; // bytes sent
59 BufferedWriter out = new BufferedWriter(
60 new OutputStreamWriter(
61 new FileOutputStream(log_file_name, true)));
63 out.write(Entry,0,Entry.length());
66 } catch (IOException e) {
67 System.err.println("Gicu " + e);