busimus / cutelog

GUI for logging
MIT License
479 stars 45 forks source link

Logging from multiple threads? #8

Closed Teque5 closed 5 years ago

Teque5 commented 5 years ago

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?

busimus commented 5 years ago

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:

Teque5 commented 5 years ago

Ah I think what I was missing was the 'single tab mode' for this problem. Thanks.