/
log.py
128 lines (102 loc) · 3.93 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
"""
.. module:: log
:platform: Unix, VMS
:synopsis: Module containing various wrappers around python logging
.. moduleauthor:: Lex van Roon <r3boot@r3blog.nl>
"""
import logging
import logging.config
import os
import yaml
# Global variable containing the system logger
LOGGER = None
def get_handler(cfg_file, log_handler):
"""Helper function which loads the log configuration from disk, and returns
a reference to the python logger object for this script. Make sure that
you configure this logging configuration correctly, since this function is
dependent on it.
>>> get_handler('./logging.yml', 'initpki')
<logging.Logger object at 0x7fa86cd4f748>
You need to use this function during script initialization to setup the
global LOGGER variable within this module, so all wrappers can do their
magic. To do this, place code along the lines below in the beginning of
your script
>>> import pkilib.log
>>> log.LOGGER = log.get_handler('./logging.yml', 'initpki')
:param cfg_file: Path to the yaml-based configuration file
:type cfg_file: str
:param log_handler: Name of the log handler to use
:type log_handler: str
:returns: Reference to a python logging handler or None
:rtype: logging.Logger, None
"""
if not os.path.exists(cfg_file):
print('{0}: No such file or directory'.format(cfg_file))
return None
log_cfg = yaml.load(open(cfg_file, 'r').read())
logging.config.dictConfig(log_cfg)
return logging.getLogger(log_handler)
def info(message):
"""Short-hand wrapper around logger.info. If logging has not been setup,
it will display a nag message telling you that.
>>> info('This is an informational message')
[ INFO]: This is an informational message
:param message: The message to display
:type message: str
:returns: Flag indicating if logging was enabled
:rtype: bool
"""
# Handle case where logger has not been setup
if not LOGGER:
print('[logging not configured] {0}'.format(message))
return
LOGGER.info(message)
return True
def warning(message):
"""Short-hand wrapper around logger.warning. If logging has not been setup,
it will display a nag message telling you that.
>>> warning('This is a warning message')
[ WARNING]: This is a warning message
:param message: The message to display
:type message: str
:returns: Flag indicating if logging was enabled
:rtype: bool
"""
# Handle case where logger has not been setup
if not LOGGER:
print('[logging not configured] {0}'.format(message))
return
LOGGER.warning(message)
return True
def debug(message):
"""Short-hand wrapper around logger.debug. If logging has not been setup,
it will display a nag message telling you that.
>>> debug('This is a debugging message')
[ DEBUG]: This is a debugging message
:param message: The message to display
:type message: str
:returns: Flag indicating if logging was enabled
:rtype: bool
"""
# Handle case where logger has not been setup
if not LOGGER:
print('[logging not configured] {0}'.format(message))
return
LOGGER.debug(message)
return True
def error(message):
"""Short-hand wrapper around logger.error. If logging has not been setup,
it will display a nag message telling you that. This function will also
run sys.exit(1) to abort the program.
>>> error('This is an error message')
[ ERROR]: This is an error message
:param message: The message to display
:type message: str
:raises: SystemExit
"""
# Handle case where logger has not been setup
if not LOGGER:
print('[logging not configured] {0}'.format(message))
else:
LOGGER.error(message)
raise SystemExit