-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Custom logger fails with KeyError #2261
Comments
I think what's happening is that you're customizing the loggers before the logger for PngImagePlugin is set up.
|
I'm not sure I follow. I've put Is it my error here? |
Try running under pdb ( |
In any case the logger names are: PIL.Image
PIL |
You're missing the loggers that are in lazily loaded image plugins. It's not picking up PIL.PcxImagePlugin or PIL.PngImagPlugin. And, it's also missing PIL.PyAccess, even after init, since that one is conditionally imported if cffi is installed and you're running pypy or specially requested it. |
I see. After the first for loop I put the following lines: for name, logger in logging.Logger.manager.loggerDict.iteritems():
logger = logging.getLogger(name)
print name, logger.filters and then got this back: PIL.Image [<__main__.TaskAddingFilter object at 0x106d35d10>]
PIL [<__main__.TaskAddingFilter object at 0x106d35d90>] which means that they are indeed patched but as you said they are not the right ones. So the question becomes: |
|
So for anyone interested, I managed to fix this by rearranging my code such that Full code: import logging
from PIL import Image
class TaskAddingFilter(logging.Filter):
def __init__(self):
logging.Filter.__init__(self)
def filter(self, record):
record.args = record.args + ('task', '123')
logger = logging.getLogger('THEDEMO')
Image.init()
logging.basicConfig(
filename='mylog.txt',
format='%(asctime)-19.19s|%(task)-36s|%(levelname)s:%(name)s:%(lineno)s: %(message)s',
level=eval('logging.%s' % 'DEBUG'))
for name, elogger in logging.Logger.manager.loggerDict.iteritems():
elogger = logging.getLogger(name)
if name.startswith('PIL'):
elogger.addFilter(TaskAddingFilter())
def tryThis():
with open('my_image.png', 'rb') as im:
logger.debug('Attempting to read image size...', extra={'task': '123'})
try:
image = Image.open(im)
w, h = image.size
image.save('my_image_out.png', 'PNG')
print 'all ran well'
except IOError:
logger.error('Processing failed!', extra={'task': 123})
raise Exception()
tryThis() Thanks @wiredfool |
I've already posted on StackOverflow but I'm also opening an issue here so this can gain more traction.
I'm having difficulties getting the library to play well with my custom logger:
I get this error back:
I am running Python 2.7.12 on macOS Sierra. Just copy past my code with an image of your own and you'll be able to quickly reproduce it.
How should I handle this?
Thank you.
The text was updated successfully, but these errors were encountered: