-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
DOC: How to handle / customize log records emitted by pypdf #2020
Comments
The overwriting was meant for testing, so rather for contributors to pypdf than pure users, if I remember correctly. From a users perspective, this should be good: import logging
from pypdf import PdfReader
logger = logging.getLogger("pypdf")
logger.setLevel(logging.ERROR) # <--- if you comment this line out, you will see a warning
pdf_path = "sample-files/017-unreadable-meta-data/unreadablemetadata.pdf"
reader = PdfReader(pdf_path) The warning you will see:
Does that solve the issue? |
Thanks for the explanations. For me it sounds like this indeed might be a user-callable configuration.
While this might be a solution, |
I don't understand the issue here. What are you doing that the following doesn't do? import logging
logger = logging.getLogger("pypdf")
logger.setLevel(logging.WARNING) |
I am doing the following for the different import logging
from contextlib import contextmanager
from pypdf import _reader, PdfReader
def custom_logger_warning(msg, src):
logging.getLogger(src).info(msg)
@contextmanager
def reduce_log_level():
old_logger_warning = _reader.logger_warning
_reader.logger_warning = custom_logger_warning
yield
_reader.logger_warning = old_logger_warning
with reduce_log_level():
reader = PdfReader('file.pdf') This basically allows me to temporarily reduce the log level for the PyPDF warnings. Why? Logging is enabled including the info level, while monitoring filters everything to start at the warning level. Info messages can be analyzed in the original logfiles if required for some reasons, while keeping the focus on the important stuff in monitoring. For some use cases (background workers which receive lots of broken/strange files) the warning level is too high as the PDF can apparently be processed in some way. Real exceptions are more urgent in these cases, as they might indicate an actual bug. |
I'm sorry, I still don't understand. Your code does not reduce the log-level threshold of the system, but reduce the log level being used. Couldn't you just do the following to reduce the log level threshold? import logging
from contextlib import contextmanager
from pypdf import PdfReader
def custom_logger_warning(msg, src):
logging.getLogger(src).info(msg)
@contextmanager
def reduce_log_level():
logger = logging.getLogger("pypdf")
old_level = logger.level
logger.setLevel(logging.INFO)
yield
logger.setLevel(old_level)
with reduce_log_level():
reader = PdfReader('file.pdf')
That part makes me think that you want to manipulate the log records themselves, e.g: import logging
from pypdf import PdfReader
class NewLogger(logging.Logger):
def makeRecord(self, *args, **kwargs):
if len(args) >= 2:
args = list(args)
args[1] = logging.INFO
if 'level' in kwargs:
kwargs['level'] = logging.INFO
return super(NewLogger, self).makeRecord(*args, **kwargs)
logging.basicConfig()
logging.setLoggerClass(NewLogger)
pdf_logger = logging.getLogger('pypdf')
# Now, all log messages from pypdf will be converted to the "INFO" level.
# Test
pdf_logger.info("This will be captured in the log.")
pdf_logger.warning("This will also be captured in the log.")
pdf_logger.error("This will also be captured in the log.")
# Especially
pdf_path = "sample-files/017-unreadable-meta-data/unreadablemetadata.pdf"
reader = PdfReader(pdf_path) you can apply those two patterns at the same time, of course, to make a decorator. |
This is indeed correct and desired. As
This is true and the proposed plain |
Yes, that would be good :-) And I think we should add more examples (like the ones I gave) to our docs. We have a lot of first-time developers who struggle with this and think it indicates a bug. |
logger_warning
is documented to allow overwriting, but actually doing this is hard
@2HR3Y It does not really sound like you have read the previous discussions and maybe just threw this into some ML model. (1) does not work as The actual underlying issue is that the docs tend to be unclear and should be adjusted to better indicate these things. |
Explanation
According to
pypdf/pypdf/_utils.py
Lines 433 to 449 in 11ee648
logger_warning
is advertised to allow overwriting, but doing so proves to be more complicated than expected.There basically are two reasons for this:
pypdf._utils.logger_warning
, which usually marks an internal implementation and thus I would not assume this to be future-proof.pypdf/pypdf/filters.py
Line 46 in 11ee648
The text was updated successfully, but these errors were encountered: