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