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 console
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 ConsoleLog
34 super Handler
35
36 # Logger level
37 #
38 # * `0`: silent
39 # * `1`: errors
40 # * `2`: warnings
41 # * `3`: info
42 # * `4`: debug
43 #
44 # Request status are always logged, whatever the logger level is.
45 var level = 4 is writable
46
47 # Do we want colors in the console output?
48 var no_colors = false
49
50 redef fun all(req, res) do
51 var clock = req.clock
52 if clock != null then
53 log "{req.method} {req.url} {status(res)} ({clock.total}s)"
54 else
55 log "{req.method} {req.url} {status(res)}"
56 end
57 end
58
59 # Colorize the request status.
60 private fun status(res: HttpResponse): String do
61 if no_colors then return res.status_code.to_s
62 return res.color_status
63 end
64
65 # Display a `message` with `level`.
66 #
67 # Message will only be displayed if `level <= self.level`.
68 # Colors will be used depending on `colors`.
69 #
70 # Use `0` for no coloration.
71 private fun display(level: Int, message: String) do
72 if level > self.level then return
73 if no_colors then
74 print message
75 return
76 end
77 if level == 0 then print message
78 if level == 1 then print message.red
79 if level == 2 then print message.yellow
80 if level == 3 then print message.blue
81 if level == 4 then print message.gray
82 end
83
84 # Display a message wathever the `level`
85 fun log(message: String) do display(0, message)
86
87 # Display a red error `message`.
88 fun error(message: String) do display(1, "[ERROR] {message}")
89
90 # Display a yellow warning `message`.
91 fun warning(message: String) do display(2, "[WARN] {message}")
92
93 # Display a blue info `message`.
94 fun info(message: String) do display(3, "[INFO] {message}")
95
96 # Display a gray debug `message`.
97 fun debug(message: String) do display(4, "[DEBUG] {message}")
98 end
99
100 redef class HttpRequest
101 # Time that request was received by the Popcorn app.
102 var clock: nullable Clock = null
103 end
104
105 redef class HttpResponse
106 # Return `self` status colored for console.
107 fun color_status: String do
108 if status_code >= 100 and status_code < 200 then return status_code.to_s.gray
109 if status_code >= 200 and status_code < 300 then return status_code.to_s.green
110 if status_code >= 300 and status_code < 400 then return status_code.to_s.blue
111 if status_code >= 400 and status_code < 500 then return status_code.to_s.yellow
112 if status_code >= 500 and status_code < 600 then return status_code.to_s.red
113 return status_code.to_s
114 end
115 end