28 lines
579 B
Python
28 lines
579 B
Python
|
import datetime
|
||
|
|
||
|
LOG_LEVEL_INFO = 0
|
||
|
LOG_LEVEL_ERROR = 1
|
||
|
|
||
|
|
||
|
def log(log_level, format_string, *args, out_file=None):
|
||
|
if log_level == LOG_LEVEL_INFO:
|
||
|
prefix = '[info] '
|
||
|
else:
|
||
|
prefix = '[error] '
|
||
|
|
||
|
now = datetime.datetime.now()
|
||
|
date = '[%d-%02d-%02d %02d:%02d]' % (
|
||
|
now.year,
|
||
|
now.month,
|
||
|
now.day,
|
||
|
now.hour,
|
||
|
now.minute
|
||
|
)
|
||
|
|
||
|
output = ''.join((date, prefix, format_string.format(*args)))
|
||
|
print(output)
|
||
|
|
||
|
if out_file is not None:
|
||
|
with open(out_file, 'a') as f:
|
||
|
f.write(output + '\n')
|