-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy path__init__.py
More file actions
98 lines (71 loc) · 2.74 KB
/
__init__.py
File metadata and controls
98 lines (71 loc) · 2.74 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
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
"""
Stackify Python API
"""
__version__ = '1.2.0'
import logging
import inspect
import atexit
from stackify.constants import DEFAULT_LEVEL
from stackify.handler import StackifyHandler
class NullHandler(logging.Handler):
def emit(self, record):
pass
internal_logger = logging.getLogger(__name__)
internal_logger.addHandler(NullHandler())
internal_logger.propagate = False
internal_logger.setLevel(DEFAULT_LEVEL)
def getLogger(name=None, auto_shutdown=True, basic_config=True, **kwargs):
'''Get a logger and attach a StackifyHandler if needed.
You can pass this function keyword arguments for Stackify configuration.
If they are omitted you can specify them through environment variables:
* STACKIFY_API_KEY
* STACKIFY_APPLICATION
* STACKIFY_ENVIRONMENT
* STACKIFY_API_URL
Args:
name: The name of the logger (or None to automatically make one)
auto_shutdown: Register an atexit hook to shut down logging
basic_config: Set up with logging.basicConfig() for regular logging
Optional Args:
api_key: Your Stackify API key
application: The name of your Stackify application
environment: The Stackfiy environment to log to
api_url: An optional API url if required
Returns:
A logger instance with Stackify handler and listener attached.
'''
if basic_config:
logging.basicConfig()
if not name:
name = getCallerName(2)
logger = logging.getLogger(name)
if not any([isinstance(x, StackifyHandler) for x in logger.handlers]):
internal_logger.debug('Creating handler for logger %s', name)
if auto_shutdown:
internal_logger.debug('Registering atexit callback')
atexit.register(stopLogging, logger)
if logger.getEffectiveLevel() == logging.NOTSET:
logger.setLevel(DEFAULT_LEVEL)
handler = StackifyHandler(ensure_at_exit=not auto_shutdown, **kwargs)
logger.addHandler(handler)
return logger
def stopLogging(logger):
'''Stop logging on the Stackify handler.
Shut down the StackifyHandler on a given logger. This will block
and wait for the queue to finish uploading.
'''
internal_logger.debug('Shutting down all handlers')
for handler in getHandlers(logger):
handler.listener.stop()
def getCallerName(levels=1):
'''Gets the name of the module calling this function'''
try:
frame = inspect.stack()[levels]
module = inspect.getmodule(frame[0])
name = module.__name__
except IndexError:
name = 'stackify-python-unknown'
return name
def getHandlers(logger):
'''Return the StackifyHandlers on a given logger'''
return [x for x in logger.handlers if isinstance(x, StackifyHandler)]