-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlogging.py
More file actions
42 lines (34 loc) · 1.38 KB
/
logging.py
File metadata and controls
42 lines (34 loc) · 1.38 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
41
42
#!/usr/bin/env python
#-*- coding:utf-8 -*-
from __future__ import absolute_import
import logging
import logging.handlers
def make_multiline_formatter():
"""Create a logging formatter to dump all information."""
formatter = ["-" * 78,
"Name: %(name)s",
"Message type: %(levelname)s",
"Location: %(pathname)s:%(lineno)d",
"Module: %(module)s",
"Function: %(funcName)s",
"Time: %(asctime)s",
"Message:",
"%(message)s",
"-" * 78]
return logging.Formatter("\n".join(formatter))
def make_file_handler(path, formatter_factory=make_multiline_formatter,
level=logging.DEBUG):
"""Create a file handler to record logging information into a file."""
handler = logging.handlers.WatchedFileHandler(path)
handler.setLevel(level)
handler.setFormatter(formatter_factory())
return handler
def make_smtp_handler(fromaddr, toaddrs, host="127.0.0.1",
subject="Application Log",
formatter_factory=make_multiline_formatter,
level=logging.ERROR):
"""Create a STMP handler to send logging information with email."""
handler = logging.handlers.SMTPHandler(host, fromaddr, toaddrs, subject)
handler.setLevel(level)
handler.setFormatter(formatter_factory())
return handler