Merge: nitrpg: Move `nitrpg` to its own repository
[nit.git] / lib / popcorn / pop_logging.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2016 Alexandre Terrasa <alexandre@moz-code.org>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 module pop_logging
18
19 import pop_handlers
20 import logger
21 import realtime
22
23 # Initialize a clock for the resquest.
24 #
25 # Can be used to compute the time passed to respond that request.
26 class RequestClock
27 super Handler
28
29 redef fun all(req, res) do req.clock = new Clock
30 end
31
32 # Display log info about request processing.
33 class PopLogger
34 super Logger
35 super Handler
36
37 # Do we want colors in the console output?
38 var no_color = false is optional
39
40 redef var default_formatter = new PopFormatter(no_color) is optional
41
42 redef fun all(req, res) do
43 var clock = req.clock
44 if clock != null then
45 add_raw(info_level, "{req.method} {req.url} {status(res)} ({clock.total}s)")
46 else
47 add_raw(info_level, "{req.method} {req.url} {status(res)}")
48 end
49 end
50
51 # Colorize the request status.
52 private fun status(res: HttpResponse): String do
53 if no_color then return res.status_code.to_s
54 return res.color_status
55 end
56 end
57
58 class PopFormatter
59 super Formatter
60
61 # Do not decorate messages with colors
62 var no_color = false is optional, writable
63
64 redef fun format(level, message) do
65 var string = message.write_to_string
66
67 if level == fatal_level then
68 string = "[FATAL] {string}"
69 else if level == error_level then
70 string = "[ERROR] {string}"
71 else if level == warn_level then
72 string = "[WARN] {string}"
73 else if level == info_level then
74 string = "[INFO] {string}"
75 else if level == debug_level then
76 string = "[DEBUG] {string}"
77 end
78
79 if no_color then return string
80
81 if level == fatal_level then
82 return string.red
83 else if level == error_level then
84 return string.red
85 else if level == warn_level then
86 return string.yellow
87 else if level == info_level then
88 return string.blue
89 else if level == debug_level then
90 return string.gray
91 end
92
93 return string
94 end
95 end
96
97
98 redef class HttpRequest
99 # Time that request was received by the Popcorn app.
100 var clock: nullable Clock = null
101 end
102
103 redef class HttpResponse
104 # Return `self` status colored for console.
105 fun color_status: String do
106 if status_code >= 100 and status_code < 200 then return status_code.to_s.gray
107 if status_code >= 200 and status_code < 300 then return status_code.to_s.green
108 if status_code >= 300 and status_code < 400 then return status_code.to_s.blue
109 if status_code >= 400 and status_code < 500 then return status_code.to_s.yellow
110 if status_code >= 500 and status_code < 600 then return status_code.to_s.red
111 return status_code.to_s
112 end
113 end