Logging from multiple threads?
Closed this issue · 2 comments
Does cutelog support logging from multiple threads? If I run each of these in two terminals:
logger = logging.getLogger('fruit.apple')
socket_handler = SocketHandler('127.0.0.1', 19996)
logger.addHandler(socket_handler)
logger.error('apples are great')
logger = logging.getLogger('fruit.banana')
socket_handler = SocketHandler('127.0.0.1', 19996)
logger.addHandler(socket_handler)
logger.error('peanutbutterjelly')
I expect to see them in the same cutlog tab, but instead I get two tabs, one with fruit.apple and the other with fruit.banana. Am I missing something?
They are in two tabs because you're creating two handlers. Each handler is a separate connection.
How I would structure your code:
import logging
from logging.handlers import SocketHandler
root_logger = logging.getLogger('fruit')
socket_handler = SocketHandler('127.0.0.1', 19996)
root_logger.addHandler(socket_handler)
apple_logger = root_logger.getChild('apple')
banana_logger = root_logger.getChild('banana')
apple_logger.error('apples are great')
banana_logger.error('peanutbutterjelly')
The way I do it is I always pass the parent logger to my objects and then they create their own child loggers (which then can become parent loggers for objects they create).
In this case I would pass root_logger
to Apple
and Banana
objects, and then they would pass the child loggers they created to BananaPeel
and AppleStem
, for example.
EDIT: I just noticed you said you were running two terminals. In this case there are two solutions:
- Enable single tab mode (
File -> Single tab mode
or in Server settings) - Create a proxy that maintains one connection to cutelog so you could send it whatever you want from anywhere and it will pass that to cutelog through its one connection.
Ah I think what I was missing was the 'single tab mode' for this problem. Thanks.