forked from stackify/stackify-api-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
120 lines (85 loc) · 3.09 KB
/
__init__.py
File metadata and controls
120 lines (85 loc) · 3.09 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
"""
Stackify Python API
"""
__version__ = '0.0.1'
API_URL = 'https://api.stackify.com'
READ_TIMEOUT = 5000
MAX_BATCH = 100
QUEUE_SIZE = 1000
import logging
import inspect
import atexit
DEFAULT_LEVEL = logging.ERROR
LOGGING_LEVELS = {
logging.CRITICAL: 'CRITICAL',
logging.ERROR: 'ERROR',
logging.WARNING: 'WARNING',
logging.INFO: 'INFO',
logging.DEBUG: 'DEBUG',
logging.NOTSET: 'NOTSET'
}
class NullHandler(logging.Handler):
def emit(self, record):
pass
logging.getLogger(__name__).addHandler(NullHandler())
from stackify.application import ApiConfiguration
from stackify.http import HTTPClient
from stackify.handler import StackifyHandler
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 [isinstance(x, StackifyHandler) for x in logger.handlers]:
internal_logger = logging.getLogger(__name__)
internal_logger.debug('Creating handler for logger %s', name)
handler = StackifyHandler(**kwargs)
logger.addHandler(handler)
if auto_shutdown:
internal_logger.debug('Registering atexit callback')
atexit.register(stopLogging, logger)
if logger.getEffectiveLevel() == logging.NOTSET:
logger.setLevel(DEFAULT_LEVEL)
handler.listener.start()
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 = logging.getLogger(__name__)
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)]