-
Notifications
You must be signed in to change notification settings - Fork 1.5k
DOC: How to handle / customize log records emitted by pypdf #3447
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
Open
Coni63
wants to merge
2
commits into
py-pdf:main
Choose a base branch
from
Coni63:DOC--How-to-handle-/-customize-log-records-emitted-by-pypdf
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
# Logging | ||
|
||
All log messages from `pypdf` go through Python’s standard `logging` library under the logger name `pypdf`. This gives you full control over verbosity, whether you want detailed debug information or only critical errors. | ||
|
||
## Filtering logs | ||
|
||
You can adjust the minimum log level of pypdf as follow: | ||
|
||
```py | ||
import logging | ||
from pypdf import PdfReader | ||
|
||
# Get the logger for the pypdf library | ||
# This allows you to configure its logging level independently | ||
logger = logging.getLogger("pypdf") | ||
logger.setLevel(logging.ERROR) | ||
|
||
reader = PdfReader("file.pdf") | ||
``` | ||
|
||
## Temporarily Reducing Log Noise | ||
|
||
If you only want to suppress logs during a specific operation, you can wrap that code in a context manager: | ||
|
||
```py | ||
import logging | ||
from contextlib import contextmanager | ||
|
||
|
||
@contextmanager | ||
def reduce_log_level(level=logging.ERROR): | ||
logger = logging.getLogger("pypdf") | ||
old_level = logger.level | ||
logger.setLevel(level) | ||
try: | ||
yield | ||
finally: | ||
logger.setLevel(old_level) | ||
``` | ||
|
||
#### Usage: | ||
|
||
```py | ||
from pypdf import PdfReader, PdfWriter | ||
from my_module import reduce_log_level # Adjust path to your module | ||
|
||
# Standard logging level applies | ||
reader = PdfReader("file.pdf") | ||
writer = PdfWriter() | ||
|
||
page = reader.pages[0] | ||
writer.add_page(page) | ||
|
||
with reduce_log_level(level=logging.ERROR): | ||
# Adjusted level applies | ||
# Logs lower than ERROR will be filtered-out | ||
do_something() | ||
|
||
# Original logging level applies | ||
with open("new_file.pdf", "wb") as fp: | ||
writer.write(fp) | ||
``` | ||
|
||
## Customizing Log Records | ||
|
||
If you prefer to remap log levels (e.g., turn errors into warnings), you can subclass `logging.Logger` as follow: | ||
|
||
```py | ||
import logging | ||
from pypdf import PdfReader | ||
|
||
class PypdfCustomLogger(logging.Logger): | ||
def makeRecord(self, name: str, level: int, *args, **kwargs): | ||
if name == "pypdf": | ||
level_mapping = { | ||
logging.NOTSET: logging.NOTSET, | ||
logging.DEBUG: logging.DEBUG, | ||
logging.INFO: logging.DEBUG, | ||
logging.WARNING: logging.INFO, | ||
logging.ERROR: logging.WARNING, | ||
logging.CRITICAL: logging.ERROR, | ||
} | ||
new_level = level_mapping.get(level, logging.DEBUG) | ||
else: | ||
new_level = level | ||
|
||
# Generate a record using the new level defined | ||
return super().makeRecord(name, new_level, *args, **kwargs) | ||
``` | ||
|
||
#### Usage: | ||
|
||
```py | ||
import logging | ||
from my_module import PypdfCustomLogger # Adjust path to your module | ||
|
||
logging.setLoggerClass(PypdfCustomLogger) | ||
logging.basicConfig() | ||
|
||
pdf_logger = logging.getLogger("pypdf") | ||
other_logger = logging.getLogger("other_logger") | ||
|
||
# pypdf logger level is adjusted | ||
pdf_logger.info("This will be captured as a DEBUG message.") | ||
pdf_logger.warning("This will be captured as a INFO message.") | ||
pdf_logger.error("This will be captured as a WARNING message.") | ||
|
||
# Other loggers are not impacted | ||
other_logger.error("This will be captured as a ERROR message.") | ||
``` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.