-
Notifications
You must be signed in to change notification settings - Fork 2
/
log.py
53 lines (39 loc) · 1.45 KB
/
log.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
# Copyright 2010 Daniel Richman and Simrun Basuita
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
import sys
import time
DEBUG = (0, "Debug ")
INFO = (1, "Info ")
NOTICE = (2, "Notice")
ERROR = (3, "Error ")
class Log:
def __init__(self, config):
self.config = config
self.file = open(self.config.filename, "a", 1)
self.file.write("Griffonbot starting up: log began at %s\n" % \
time.strftime("%H:%M:%S", time.gmtime()))
def msg(self, level, str):
str = str.strip()
header = "%s %s" % (time.strftime("%H:%M:%S", time.gmtime()), level[1])
for line in str.splitlines():
message = "%s %s\n" % (header, line)
if level[0] >= self.config.file[0]:
self.file.write(message)
if level[0] >= self.config.stderr[0]:
sys.stderr.write(message)
# Shortcuts
def debug(self, str):
self.msg(DEBUG, str)
def info(self, str):
self.msg(INFO, str)
def notice(self, str):
self.msg(NOTICE, str)
def error(self, str):
self.msg(ERROR, str)