-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.py
139 lines (102 loc) · 3.45 KB
/
logger.py
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/local/bin/python
###############################################################################
# 'logger.py'
#
# About:
#
# Version: 0.01
# Author: Kevin Kelley [[email protected]] (http://thesta.in)
# License: WTFPL
###############################################################################
# Configuration
###############################################################################
# Code
class Logger(object):
def __init__(self):
pass
def __del__(self):
self.close()
def configure(self, configuration=None):
# List of required configuration items
required = ["debug", "log_filename", ]
self.error = False
self.error_message = {
"log_error": None,
}
if configuration is None:
# Error
self.error = True
self.error_message["log_error"] = "No configuration provided."
return None
if type(configuration) is not type({}):
# Error
self.error = True
self.error_message["log_error"] = "Invalid configuration data provided."
return None
for i in required:
if i not in configuration.keys():
# Error
self.error = True
self.error_message["log_error"] = "Required configuration data '%s' not provided." % (i)
if "log_format" not in configuration.keys():
log_format = "%Y-%m-%d %H:%M:%S"
else:
log_format = configuration["log_format"]
# Configure the instance
self.debug = configuration["debug"]
self.log = None
self.log_filename = configuration["log_filename"]
self.log_format = log_format
self.open()
return None
def open(self):
if self.log is not None:
# Error
self.error = True
self.error_message["log_error"] = "Log file is already open."
return None
try:
self.log = open(self.log_filename, 'a')
except IOError as err:
# Error
self.error = True
self.error_message["log_error"] = "Unable to open log file '%s'. %s" % (self.log_filename, err)
return None
return None
def close(self):
if self.log is None:
return None
try:
self.log.close()
except IOError as err:
# Error
self.error = True
self.error_message["log_error"] = "Unable to close log file '%s'. %s" % (self.log_filename, err)
return None
return None
def write(self, message=None):
if self.message is None:
# Error
self.error = True
self.error_message = "No log message provided."
return None
if self.log is None:
self.open()
if self.debug is True:
message = "[DEBUG] %s" % (message)
message = "[%s] %s\n" % (time.strftime(self.logformat, time.localtime()), message)
try:
self.log.write(message)
except IOError as err:
# Error
self.error = True
self.error_message["log_error"] = "Unable to write to log file '%s'. %s" % (self.log_filename, err)
return None
return None
###############################################################################
# Main
if __name__ == "__main__":
# Add code to support single file conversion from command-line.
pass
###############################################################################
# End