Skip to content

Commit 09c8ad5

Browse files
committed
Clean up internal logging
1 parent ad22f11 commit 09c8ad5

3 files changed

Lines changed: 20 additions & 15 deletions

File tree

stackify/__init__.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@
2828
logging.NOTSET: 'NOTSET'
2929
}
3030

31-
logging.basicConfig()
31+
class NullHandler(logging.Handler):
32+
def emit(self, record):
33+
pass
34+
35+
logging.getLogger(__name__).addHandler(NullHandler())
3236

3337

3438
from stackify.application import ApiConfiguration
@@ -37,28 +41,26 @@
3741
from stackify.handler import StackifyHandler
3842

3943

40-
# TODO
41-
# holds our listeners, since more than one handler can service
42-
# the same listener
43-
__listener_cache = {}
44-
45-
46-
def getLogger(name=None, auto_shutdown=True, **kwargs):
44+
def getLogger(name=None, auto_shutdown=True, basic_config=True, **kwargs):
4745
'''
4846
Get a logger and attach a StackifyHandler if needed.
4947
'''
48+
if basic_config:
49+
logging.basicConfig()
50+
5051
if not name:
5152
name = getCallerName(2)
5253

5354
logger = logging.getLogger(name)
5455

5556
if not [isinstance(x, StackifyHandler) for x in logger.handlers]:
56-
logger = logging.getLogger(__name__)
57-
logger.debug('Creating handler for logger %s', name)
57+
internal_logger = logging.getLogger(__name__)
58+
internal_logger.debug('Creating handler for logger %s', name)
5859
handler = StackifyHandler(**kwargs)
5960
logger.addHandler(handler)
6061

6162
if auto_shutdown:
63+
internal_logger.debug('Registering atexit callback')
6264
atexit.register(stopLogging, logger)
6365

6466
if logger.getEffectiveLevel() == logging.NOTSET:
@@ -73,8 +75,8 @@ def stopLogging(logger):
7375
Shut down the StackifyHandler on a given logger. This will block
7476
and wait for the queue to finish uploading.
7577
'''
76-
logger = logging.getLogger(__name__)
77-
logger.debug('Shutting down all handlers')
78+
internal_logger = logging.getLogger(__name__)
79+
internal_logger.debug('Shutting down all handlers')
7880
for handler in getHandlers(logger):
7981
handler.listener.stop()
8082

stackify/handler.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class StackifyHandler(QueueHandler):
2828
def __init__(self, queue_=None, listener=None, **kwargs):
2929
if queue_ is None:
3030
queue_ = queue.Queue(QUEUE_SIZE)
31+
logger = logging.getLogger(__name__)
3132

3233
super(StackifyHandler, self).__init__(queue_)
3334

@@ -40,10 +41,10 @@ def enqueue(self, record):
4041
'''
4142
Put a new record on the queue. If it's full, evict an item.
4243
'''
43-
logger = logging.getLogger(__name__)
4444
try:
4545
self.queue.put_nowait(record)
4646
except queue.Full:
47+
logger = logging.getLogger(__name__)
4748
logger.warn('StackifyHandler queue is full, evicting oldest record')
4849
self.queue.get_nowait()
4950
self.queue.put_nowait(record)

tests/test_handler.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
from stackify.handler import StackifyHandler, StackifyListener
1414
from stackify.application import ApiConfiguration
15-
from stackify import internal_log
1615

1716
import logging
1817

@@ -26,7 +25,8 @@ def test_queue_full(self):
2625
'''The queue should evict when full'''
2726
q = queue.Queue(1)
2827
handler = StackifyHandler(queue_=q, listener=Mock())
29-
internal_log.setLevel(logging.CRITICAL) # don't print warnings on overflow
28+
# don't print warnings on overflow, so mute stackify logger
29+
logging.getLogger('stackify').propagate = False
3030
handler.enqueue('test1')
3131
handler.enqueue('test2')
3232
handler.enqueue('test3')
@@ -45,6 +45,8 @@ def setUp(self):
4545
environment = 'test_environment',
4646
api_key = 'test_apikey',
4747
api_url = 'test_apiurl')
48+
# don't print warnings on http crashes, so mute stackify logger
49+
logging.getLogger('stackify').propagate = False
4850

4951
@patch('stackify.handler.LogMsg')
5052
@patch('stackify.handler.StackifyListener.send_group')

0 commit comments

Comments
 (0)