-
Notifications
You must be signed in to change notification settings - Fork 10
/
template.py
executable file
·73 lines (61 loc) · 2.57 KB
/
template.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
#! /usr/bin/env python
"""
What this will do
Based on the Python script template at
https://github.com/BrianGallew/script_templates/blob/master/template.py
"""
import logging
import logging.handlers
import argparse
LOGGER = logging.getLogger(__name__)
OPTION_GROUP = argparse.Namespace()
def setup_logging():
"""Sets up logging in a syslog format by log level
:param OPTION_GROUP: options as returned by the OptionParser
"""
stderr_log_format = "%(levelname) -8s %(asctime)s %(funcName)s line:%(lineno)d: %(message)s"
file_log_format = "%(asctime)s - %(levelname)s - %(message)s"
syslog_format = "%(processName)s[%(process)d] - %(message)s"
LOGGER.setLevel(level=OPTION_GROUP.loglevel)
handlers = []
if OPTION_GROUP.syslog:
handlers.append(
logging.handlers.SysLogHandler(facility=OPTION_GROUP.syslog, address='/dev/log'))
handlers[-1].setFormatter(logging.Formatter(syslog_format))
# Use standard format here because timestamp and level will be added by
# syslogd.
if OPTION_GROUP.logfile:
handlers.append(logging.FileHandler(OPTION_GROUP.logfile))
handlers[0].setFormatter(logging.Formatter(file_log_format))
if not handlers:
handlers.append(logging.StreamHandler())
handlers[0].setFormatter(logging.Formatter(stderr_log_format))
# Remove all the old handler(s)
for handler in logging.root.handlers:
logging.root.removeHandler(handler)
# Add our new handler(s) back in
for handler in handlers:
logging.root.addHandler(handler)
return
def main():
"""Primary entry point."""
parser = argparse.ArgumentParser(description=__doc__,
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
# Standard logging options.
parser.add_argument("-v", "--verbose", action='store_const',
const=logging.INFO, dest='loglevel',
default=logging.WARNING, help="Verbose output")
parser.add_argument("-d", "--debug", action='store_const',
const=logging.DEBUG, dest='loglevel',
default=logging.WARNING, help="Debugging output")
parser.add_argument("--syslog", metavar="FACILITY",
help="Send log messages to the syslog")
parser.add_argument("--logfile", metavar="FILENAME",
help="Send log messages to a file")
# script-specific options here
parser.parse_args(namespace=OPTION_GROUP)
setup_logging()
# Your code here.
return
if __name__ == '__main__':
main()