-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogging_example.py
More file actions
40 lines (36 loc) · 1.05 KB
/
logging_example.py
File metadata and controls
40 lines (36 loc) · 1.05 KB
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
import logging
# OneLineExceptionFormatter
class OLEF(logging.Formatter):
def formatException(self, exc_info):
result = super(OLEF, self).formatException(exc_info)
return repr(result)
def format(self, record):
s = super(OLEF, self).format(record)
if record.exc_text: s = s.replace('\n', '')
return s
def logging_setup(filename):
logger=logging.getLogger() #root logger
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
logger.addHandler(ch)
formatter = OLEF('%(asctime)s | %(name)s | %(message)s')
fh = logging.FileHandler(filename, mode='a')
fh.setLevel(logging.DEBUG)
fh.setFormatter(formatter)
logger.addHandler(fh)
class MyDb:
pass
dryrun=False
def main():
logging_setup(filename='test.log')
db = MyDb()
l = "mordor"
try:
if not dryrun:
logging.info("going to add locality")
l_id = db.add_locality(l)
4/0
logging.debug("ADDED L: %s" % l)
except Exception as error:
logging.exception(error)
main()