-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathflogs.v
70 lines (59 loc) · 1.59 KB
/
flogs.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
module flogs
import term
pub enum Level {
verbose
info
warning
error
critical
}
const colors_supported = term.can_show_color_on_stdout()
const levels_ranking = [Level.critical, .error, .warning, .info, .verbose]
pub struct Logger {
min_logging_level Level
}
fn is_important_enough(lvl Level, minlvl Level) bool {
for value in levels_ranking {
match value {
lvl { return true }
minlvl { return false }
else {}
}
}
return false
}
// NOTE: Consider making the Level optional, defaulting to verbose or info
pub fn (l Logger) log(txt string, lvl Level) {
if is_important_enough(lvl, l.min_logging_level) {
println(match lvl {
.verbose { '\033[90m[VERBOSE] $txt\033[0m' }
.info { '[\033[34mINFO\033[0m] $txt\033[0m' }
.warning { '[\033[33mWARNING\033[0m] $txt\033[0m' }
.error { '[\033[31mERROR\033[0m] $txt\033[0m' }
.critical { '\033[1;31m[CRITICAL] $txt\033[0m' }
})
} else {
println(match lvl {
.verbose { '[VERBOSE] $txt' }
.info { '[INFO] $txt' }
.warning { '[WARNING] $txt' }
.error { '[ERROR] $txt' }
.critical { '[CRITICAL] $txt' }
})
}
}
pub fn (l Logger) verbose(msg string) {
l.log(msg, .verbose)
}
pub fn (l Logger) info(msg string) {
l.log(msg, .info)
}
pub fn (l Logger) warning(msg string) {
l.log(msg, .warning)
}
pub fn (l Logger) error(msg string) {
l.log(msg, .error)
}
pub fn (l Logger) critical(msg string) {
l.log(msg, .critical)
}