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