-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.lua
58 lines (52 loc) · 1.85 KB
/
log.lua
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
logger = {}
-- Log without any tag --
local function log_write(self, fmt, ...)
local time = os.date("*t")
local str = string.format(fmt, ...)
local str2 = string.format("[%02i:%02i:%02i] %s\n", time.hour, time.min, time.sec, str)
local file_str = os.date(self.filename.."_%Y_%m_%d.log")
local file = io.open(file_str, "a+")
file:write(str2)
file:close()
end
-- Log with DEBUG: tag --
local function log_debug(self, fmt, ...)
if self.debug_print == false then return end
local time = os.date("*t")
local str = string.format(fmt, ...)
local str2 = string.format("[%02i:%02i:%02i] DEBUG: %s\n", time.hour, time.min, time.sec, str)
local file_str = os.date(self.filename.."_%Y_%m_%d.log")
local file = io.open(file_str, "a+")
file:write(str2)
file:close()
end
-- Log with WARNING: tag --
local function log_warning(self, fmt, ...)
local time = os.date("*t")
local str = string.format(fmt, ...)
local str2 = string.format("[%02i:%02i:%02i] WARNING: %s\n", time.hour, time.min, time.sec, str)
local file_str = os.date(self.filename.."_%Y_%m_%d.log")
local file = io.open(file_str, "a+")
file:write(str2)
file:close()
end
-- Log WITH ERROR: tag and throw error --
local function log_error(self, fmt, ...)
local time = os.date("*t")
local str = string.format(fmt, ...)
local str2 = string.format("[%02i:%02i:%02i] ERROR: %s\n", time.hour, time.min, time.sec, str)
local file_str = os.date(self.filename.."_%Y_%m_%d.log")
local file = io.open(file_str, "a+")
file:write(str2)
file:close()
error(str)
end
function log_debug_allow(self, boolean)
self.debug_print = true
end
function logger.open(filename)
local local_table = { write = log_write, debug = log_debug, warning = log_warning, error = log_error, debug_allow = log_debug_allow }
local_table.debug_print = false
local_table.filename = string.format("%s/%s", LOGS_PATH, filename)
return local_table
end