Mercurial Hosting > luan
annotate src/goodjava/webserver/handlers/LogHandler.java @ 1598:c78d6a4e2d9a
web logging
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 04 Apr 2021 15:09:51 -0600 |
parents | 329197048ff0 |
children | 8a717f0b7f45 |
rev | line source |
---|---|
1402
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1347
diff
changeset
|
1 package goodjava.webserver.handlers; |
1138
4189027691b7
webserver - add LogHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
2 |
1594
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
3 import java.io.File; |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
4 import java.io.IOException; |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
5 import java.text.DateFormat; |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
6 import java.text.SimpleDateFormat; |
1402
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1347
diff
changeset
|
7 import goodjava.logging.Logger; |
1594
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
8 import goodjava.logger.Layout; |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
9 import goodjava.logger.ListLayout; |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
10 import goodjava.logger.DateLayout; |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
11 import goodjava.logger.Layouts; |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
12 import goodjava.logger.Appender; |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
13 import goodjava.logger.ConsoleAppender; |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
14 import goodjava.logger.DailyRollingFileAppender; |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
15 import goodjava.logger.GoodLogger; |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
16 import goodjava.logger.Level; |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
17 import goodjava.io.IoUtils; |
1402
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1347
diff
changeset
|
18 import goodjava.webserver.Handler; |
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1347
diff
changeset
|
19 import goodjava.webserver.Request; |
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1347
diff
changeset
|
20 import goodjava.webserver.Response; |
1138
4189027691b7
webserver - add LogHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
21 |
4189027691b7
webserver - add LogHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
22 |
4189027691b7
webserver - add LogHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
23 public final class LogHandler implements Handler { |
1598 | 24 private static final Layout layout = new ListLayout(new DateLayout("yyyy-MM-dd:HH:mm:ss")," ",Layouts.MESSAGE,"\n"); |
1594
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
25 |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
26 public static Logger consoleLogger() { |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
27 Appender appender = new ConsoleAppender(layout,System.err); |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
28 GoodLogger logger = new GoodLogger("HTTP"); |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
29 logger.appender = appender; |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
30 logger.level = Level.INFO; |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
31 return logger; |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
32 } |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
33 |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
34 public static Logger dirLogger(File dir) { |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
35 if( dir.exists() && !dir.isDirectory() ) |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
36 throw new RuntimeException("must be a directory"); |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
37 try { |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
38 IoUtils.mkdirs(dir); |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
39 } catch(IOException e) { |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
40 throw new RuntimeException(e); |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
41 } |
1598 | 42 DateFormat fmt = new SimpleDateFormat( "'" + dir + "/'yyyy_MM_dd'.log'" ); |
1594
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
43 Appender appender = new DailyRollingFileAppender(layout,fmt); |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
44 GoodLogger logger = new GoodLogger("HTTP"); |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
45 logger.appender = appender; |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
46 logger.level = Level.INFO; |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
47 return logger; |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
48 } |
1138
4189027691b7
webserver - add LogHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
49 |
4189027691b7
webserver - add LogHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
50 private final Handler handler; |
1594
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
51 private final Logger logger; |
1138
4189027691b7
webserver - add LogHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
52 |
4189027691b7
webserver - add LogHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
53 public LogHandler(Handler handler) { |
1594
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
54 this(handler,consoleLogger()); |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
55 } |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
56 |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
57 public LogHandler(Handler handler,Logger logger) { |
1138
4189027691b7
webserver - add LogHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
58 this.handler = handler; |
1594
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
59 this.logger = logger; |
1138
4189027691b7
webserver - add LogHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
60 } |
4189027691b7
webserver - add LogHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
61 |
4189027691b7
webserver - add LogHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
62 public Response handle(Request request) { |
4189027691b7
webserver - add LogHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
63 Response response = handler.handle(request); |
1594
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
64 if( response == null ) |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
65 return null; |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
66 String ip = (String)request.headers.get("x-real-ip"); |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
67 //String agent = (String)request.headers.get("user-agent"); |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
68 logger.info( ip + " \"" + request.method + " " + request.path + "\" " + response.status.code + " " + response.body.length ); |
1138
4189027691b7
webserver - add LogHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
69 return response; |
4189027691b7
webserver - add LogHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
70 } |
4189027691b7
webserver - add LogHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
71 } |