Mercurial Hosting > luan
annotate src/goodjava/webserver/handlers/LogHandler.java @ 1640:570f3d483a31
host - move https.luan into src
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 21 Dec 2021 16:54:26 -0700 |
parents | 94ea190714dd |
children | 8b3ad1fb219f |
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 |
1600
8a717f0b7f45
add DailyRollingFileAppender.deleteAfter
Franklin Schmidt <fschmidt@gmail.com>
parents:
1598
diff
changeset
|
34 public static Logger dirLogger(File dir,long deleteAfter) { |
1594
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'" ); |
1600
8a717f0b7f45
add DailyRollingFileAppender.deleteAfter
Franklin Schmidt <fschmidt@gmail.com>
parents:
1598
diff
changeset
|
43 DailyRollingFileAppender appender = new DailyRollingFileAppender(layout,fmt); |
8a717f0b7f45
add DailyRollingFileAppender.deleteAfter
Franklin Schmidt <fschmidt@gmail.com>
parents:
1598
diff
changeset
|
44 appender.deleteAfter = deleteAfter; |
1594
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
45 GoodLogger logger = new GoodLogger("HTTP"); |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
46 logger.appender = appender; |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
47 logger.level = Level.INFO; |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
48 return logger; |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
49 } |
1138
4189027691b7
webserver - add LogHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
50 |
4189027691b7
webserver - add LogHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
51 private final Handler handler; |
1594
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
52 private final Logger logger; |
1138
4189027691b7
webserver - add LogHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
53 |
4189027691b7
webserver - add LogHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
54 public LogHandler(Handler handler) { |
1594
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
55 this(handler,consoleLogger()); |
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 |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
58 public LogHandler(Handler handler,Logger logger) { |
1138
4189027691b7
webserver - add LogHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
59 this.handler = handler; |
1594
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
60 this.logger = logger; |
1138
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 |
4189027691b7
webserver - add LogHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
63 public Response handle(Request request) { |
4189027691b7
webserver - add LogHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
64 Response response = handler.handle(request); |
1594
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
65 if( response == null ) |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
66 return null; |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
67 String ip = (String)request.headers.get("x-real-ip"); |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
68 //String agent = (String)request.headers.get("user-agent"); |
1610 | 69 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
|
70 return response; |
4189027691b7
webserver - add LogHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
71 } |
4189027691b7
webserver - add LogHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
72 } |