This repository has been archived by the owner on Mar 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logging.rb
76 lines (74 loc) · 1.65 KB
/
logging.rb
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
71
72
73
74
75
76
# Pretty logging
require "config.rb"
def get_time
Time.now.to_s
end
module Logging
class <<self
attr_accessor :loglevel
attr_accessor :file
end
GREEN = "\033[32m"
BLUE = "\033[34m"
ORANGE = "\033[33m"
RED = "\033[31m"
ENDC = "\033[39m\033[0m"
BOLD = "\033[1m"
LEVELS = {"debug"=>4,
"info" =>3,
"warn" =>2,
"error"=>1,
"none" =>0}
@file = nil
@loglevel="info"
def self.init()
@file = Config::get("logging.file")
@loglevel = Config::get("logging.level")
end
def self.info(message)
loglevel=@loglevel
loglevel=LEVELS[loglevel]
if loglevel<3 then
return
end
self.write2file("INFO",message)
print("\r#{BLUE}#{BOLD}INFO #{ENDC+BOLD}| #{get_time}:#{ENDC}#{message}\n")
end
def self.debug(message)
loglevel=@loglevel
loglevel=LEVELS[loglevel]
if loglevel<4 then
return
end
self.write2file("DEBUG",message)
print("\r#{GREEN}#{BOLD}DEBUG #{ENDC+BOLD}| #{get_time}:#{ENDC}#{message}\n")
end
def self.warn(message)
loglevel=@loglevel
loglevel=LEVELS[loglevel]
if loglevel<2 then
return
end
self.write2file("WARN",message)
print("\r#{ORANGE}#{BOLD}WARN #{ENDC+BOLD}| #{get_time}:#{ENDC}#{message}\n")
end
def self.error(message)
loglevel=@loglevel
loglevel=LEVELS[loglevel]
if loglevel<1 then
return
end
self.write2file("ERROR",message)
print("\r#{RED}#{BOLD}ERROR #{ENDC+BOLD}| #{get_time}:#{ENDC}#{message}\n")
end
def self.write2file(level,message)
if @file.nil? then
return
end
message="[#{level}]:#{get_time}:#{message}\n"
fname=@file
open(fname, 'a') do |f|
f << message
end
end
end