examples: annotate examples
[nit.git] / lib / nitcorn / log.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # Services inserting a timestamp in all prints and to log each requests
16 # Also keep track of the performances of the requests
17 module log
18
19 import reactor
20 import realtime
21 import performance_analysis
22
23 redef class Action
24
25 redef fun prepare_respond_and_close(request, truncated_uri, http_server) do
26 if not log_nitcorn_actions then
27 super
28 return
29 end
30
31 print "{http_server.remote_address}: {class_name} prepare for url:'{request.url}' body:'{request.body}' cookie:'{request.cookie.join(",", ":")}'"
32
33 var clock = new Clock
34
35 super
36
37 var perf = sys.perfs[class_name]
38 perf.add(clock.lapse)
39 if perf.count % perfs_print_period == 0 then print "{class_name} perfs: {perf}:"
40 end
41 end
42
43 redef class HttpServer
44 redef fun read_http_request(str)
45 do
46 print "{remote_address}: received HTTP request"
47 super
48 end
49
50 redef fun respond(response)
51 do
52 super
53 if log_nitcorn_actions then print "{remote_address}: response header '{response.header.join(",", ":")}' to '{remote_address}'"
54 end
55 end
56
57 redef fun print(object) do
58 var timestamp = new Tm.gmtime
59 super "{timestamp.year}/{timestamp.mon}/{timestamp.mday} "+
60 "{timestamp.hour}:{timestamp.min}:{timestamp.sec}: {object}"
61 end
62
63 redef fun print_error(object) do
64 var timestamp = new Tm.gmtime
65 super "{timestamp.year}/{timestamp.mon}/{timestamp.mday} "+
66 "{timestamp.hour}:{timestamp.min}:{timestamp.sec}: {object}"
67 end
68
69 # Should the actions be logged? This may log sensitive data.
70 fun log_nitcorn_actions: Bool do return false
71
72 # Number of actions executed before printing the perfs
73 fun perfs_print_period: Int do return 20