Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging at Gevent application

Tags:

python

gevent

I'm trying to use standard Python logging module alongside with gevent. I have monkey patched threading and I expect logging to work with my app:

import gevent.monkey
gevent.monkey.patch_all()

import logging

logger = logging.getLogger()
fh = logging.FileHandler('E:\\spam.log')
fh.setLevel(logging.DEBUG)
logger.addHandler(fh)

def foo():
    logger.debug('Running in foo')
    gevent.sleep(0)
    logger.debug('Explicit context switch to foo again')

def bar():
    logger.debug('Explicit context to bar')
    gevent.sleep(0)
    logger.debug('Implicit context switch back to bar')


gevent.joinall([
    gevent.spawn(foo),
    gevent.spawn(bar),
])

Unfortunately E:\spam.log is empty and I can see no output to the console. It seems that either I haven't configured logging properly or gevent does not support it at all (which I don't believe, because gevent documentation says that it does). So, how can I log at gevent app?

like image 832
Xanx Avatar asked May 15 '26 01:05

Xanx


1 Answers

You haven't configured it correctly. You need to set the DEBUG level on the logger rather than the handler, otherwise the logger's default level (WARNING) causes your debug messages to be dropped. Try doing a

logger.setLevel(logging.DEBUG)

and it should work.

like image 123
Vinay Sajip Avatar answered May 17 '26 13:05

Vinay Sajip