Mercurial Hosting > luan
annotate src/goodjava/webserver/handlers/LogHandler.java @ 1733:8b3ad1fb219f
make LogHandler configurable
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Wed, 14 Sep 2022 07:51:13 -0600 |
parents | 94ea190714dd |
children |
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 { |
1733
8b3ad1fb219f
make LogHandler configurable
Franklin Schmidt <fschmidt@gmail.com>
parents:
1610
diff
changeset
|
24 public static int level = Level.INFO; |
8b3ad1fb219f
make LogHandler configurable
Franklin Schmidt <fschmidt@gmail.com>
parents:
1610
diff
changeset
|
25 public static 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
|
26 |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
27 public static Logger consoleLogger() { |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
28 Appender appender = new ConsoleAppender(layout,System.err); |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
29 GoodLogger logger = new GoodLogger("HTTP"); |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
30 logger.appender = appender; |
1733
8b3ad1fb219f
make LogHandler configurable
Franklin Schmidt <fschmidt@gmail.com>
parents:
1610
diff
changeset
|
31 logger.level = level; |
1594
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
32 return logger; |
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 |
1600
8a717f0b7f45
add DailyRollingFileAppender.deleteAfter
Franklin Schmidt <fschmidt@gmail.com>
parents:
1598
diff
changeset
|
35 public static Logger dirLogger(File dir,long deleteAfter) { |
1594
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
36 if( dir.exists() && !dir.isDirectory() ) |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
37 throw new RuntimeException("must be a directory"); |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
38 try { |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
39 IoUtils.mkdirs(dir); |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
40 } catch(IOException e) { |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
41 throw new RuntimeException(e); |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
42 } |
1598 | 43 DateFormat fmt = new SimpleDateFormat( "'" + dir + "/'yyyy_MM_dd'.log'" ); |
1600
8a717f0b7f45
add DailyRollingFileAppender.deleteAfter
Franklin Schmidt <fschmidt@gmail.com>
parents:
1598
diff
changeset
|
44 DailyRollingFileAppender appender = new DailyRollingFileAppender(layout,fmt); |
8a717f0b7f45
add DailyRollingFileAppender.deleteAfter
Franklin Schmidt <fschmidt@gmail.com>
parents:
1598
diff
changeset
|
45 appender.deleteAfter = deleteAfter; |
1594
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
46 GoodLogger logger = new GoodLogger("HTTP"); |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
47 logger.appender = appender; |
1733
8b3ad1fb219f
make LogHandler configurable
Franklin Schmidt <fschmidt@gmail.com>
parents:
1610
diff
changeset
|
48 logger.level = level; |
1594
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
49 return logger; |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
50 } |
1138
4189027691b7
webserver - add LogHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
51 |
4189027691b7
webserver - add LogHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
52 private final Handler handler; |
1594
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
53 private final Logger logger; |
1138
4189027691b7
webserver - add LogHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
54 |
4189027691b7
webserver - add LogHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
55 public LogHandler(Handler handler) { |
1594
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
56 this(handler,consoleLogger()); |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
57 } |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
58 |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
59 public LogHandler(Handler handler,Logger logger) { |
1138
4189027691b7
webserver - add LogHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
60 this.handler = handler; |
1594
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
61 this.logger = logger; |
1138
4189027691b7
webserver - add LogHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
62 } |
4189027691b7
webserver - add LogHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
63 |
4189027691b7
webserver - add LogHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
64 public Response handle(Request request) { |
4189027691b7
webserver - add LogHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
65 Response response = handler.handle(request); |
1594
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
66 if( response == null ) |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
67 return null; |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
68 String ip = (String)request.headers.get("x-real-ip"); |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
69 //String agent = (String)request.headers.get("user-agent"); |
1610 | 70 logger.info( ip + " \"" + request.method + " " + request.rawPath + "\" " + response.status.code + " " + response.body.length ); |
1138
4189027691b7
webserver - add LogHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
71 return response; |
4189027691b7
webserver - add LogHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
72 } |
4189027691b7
webserver - add LogHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
73 } |