- Notifications
You must be signed in to change notification settings - Fork11.2k
Added structured logging to systemd journal.#7115
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:master
Are you sure you want to change the base?
Conversation
wRAR commentedOct 24, 2025
Obligatory "I wonder how to test this". I'm also not sure if enabling this if and only if |
scrapy/utils/log.py Outdated
| handler=logging.FileHandler(filename,mode=mode,encoding=encoding) | ||
| elifsettings.getbool("LOG_ENABLED"): | ||
| handler=logging.StreamHandler() | ||
| # Use systemd journal handler if running under systemd |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
I don’t think we should do this automatically, I think this should be opt-in through a new setting.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Something like this?
# OPT-IN: Use systemd JournalHandler only if explicitly enabledif (settings.getbool("LOG_TO_SYSTEMD",False)andSYSTEMD_JOURNAL_AVAILABLE ):handler=systemd.journal.JournalHandler()else:handler=logging.StreamHandler()
Gallaecio commentedOct 24, 2025
|
AliAlimohammadi commentedOct 24, 2025
Please check it now. |
docs/topics/logging.rst Outdated
| *:setting:`LOG_DATEFORMAT` | ||
| *:setting:`LOG_STDOUT` | ||
| *:setting:`LOG_SHORT_NAMES` | ||
| *:setting:`LOG_TO_SYSTEMD` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
I thinkLOG_SYSTEMD orLOG_SYSTEMD_ENABLED would be more in line with the naming of other Scrapy settings.
docs/topics/logging.rst Outdated
| If:setting:`LOG_TO_SYSTEMD` is set to ``True``, Scrapy will send logs to the | ||
| systemd journal using ``systemd.journal.JournalHandler`` as its logging handler. | ||
| This requires the external Python package ``systemd`` to be installed (minimum | ||
| version 234 recommended). You can install it via pip:: | ||
| pip install systemd | ||
| This feature is opt-in and disabled by default. When enabled, Scrapy will raise an | ||
| ``ImportError`` if the ``systemd`` package is not found. Use this setting when | ||
| running Scrapy under systemd to utilize ``journalctl`` for log management and | ||
| filtering. |
GallaecioOct 24, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
You can have a minor reference here, but the setting documentation should live in the settings.rst page, with a section of its own properly labeled so that:setting:`FOO` actually links there. Consider runningtox -e docs to check how your docs render.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Done.
codecovbot commentedOct 26, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@## master #7115 +/- ##==========================================- Coverage 91.43% 91.41% -0.03%========================================== Files 165 165 Lines 12644 12648 +4 Branches 1621 1622 +1 ==========================================+ Hits 11561 11562 +1- Misses 810 812 +2- Partials 273 274 +1
|
AliAlimohammadi commentedOct 27, 2025
Anything else I should do? |
wRAR commentedOct 27, 2025
@AliAlimohammadi at the very least you should fix the CI failures and make sure you run these checks locally before pushing. |
AliAlimohammadi commentedOct 27, 2025
But we wanted to raise an exception. Unless you want me to gracefully raise an exception in try block? |
AliAlimohammadi commentedOct 27, 2025
@Gallaecio Can you please let me know how exactly to handle the exception? |
wRAR commentedOct 28, 2025
It doesn't look like you understood what I said, do you need some clarifications? |
Gallaecio commentedOct 28, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
The docs CI reports: You should run Also, you shoulduse pre-commit. |
AliAlimohammadi commentedNov 4, 2025
Can you check it now? |
tox.ini Outdated
| deps = | ||
| {[testenv:extra-deps]deps} | ||
| pylint==4.0.2 | ||
| systend-python; sys-platform=="linux" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
??
scrapy/utils/log.py Outdated
| handler=logging.StreamHandler() | ||
| ifsettings.getbool("LOG_SYSTEMD",False): | ||
| # Opt-in systemd journal logging, will raise if systemd.journal is missing | ||
| importsystemd.journal |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
It doesn't look like you've started using pre-commit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
I used pre-commit and it passed without an issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
I don't know what else to do.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Then it's likely that you've used it incorrectly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
I fixed it. There was a typo.
wRAR commentedNov 5, 2025
Have you tested these changes? If so, can you please describe how? |
AliAlimohammadi commentedNov 12, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Testing DocumentationYes, I have thoroughly tested these changes. Here's a detailed breakdown: 1. Existing Test Suite ✓Ran the complete logging utility test suite to ensure backward compatibility: pytest tests/test_utils_log.py -v Result: All 24 tests passed successfully, confirming that existing logging functionality remains intact. 2. Manual Testing on Linux ✓Basic functionality test:fromscrapy.utils.logimportconfigure_loggingfromscrapy.settingsimportSettingssettings=Settings()settings.set('LOG_SYSTEMD',True)configure_logging(settings) Result: Systemd logging configured successfully without errors. Real-world spider test:scrapy startproject test_systemdcd test_systemdecho"LOG_SYSTEMD = True">> test_systemd/settings.pyscrapy genspider example example.comscrapy crawl example Result: Verified logs appear correctly in 3. Backward Compatibility ✓
4. Error Handling ✓Tested scenario where
5. Platform Compatibility ✓
6. Documentation ✓
7. Code Quality ✓
|
AliAlimohammadi commentedNov 13, 2025
Based on my tests, everything is working. |
AliAlimohammadi commentedNov 14, 2025
The CI failure is occurring because I believe the best approach is to make Proposed SolutionI'll remove pip install systemd-python Why This Approach?
DocumentationI've already documented this clearly in ..note:: This feature requires the `systemd-python` package to be installed separately: ..code-block::console pip install systemd-python The package is only available on Linux systems with systemd. When users enable Does this approach sound good to you? |
wRAR commentedNov 14, 2025
It should be possible to install it in
The way Scrapy handles those is having them in the Though this is only needed if there are tests for this code, and it's still an open question if there will be any.
You can skip all of this fluff and just write your own thoughts. It's mostly wrong anyway. |
AliAlimohammadi commentedNov 17, 2025
I kept it optional and removed |
AliAlimohammadi commentedNov 17, 2025
tox -e pylint.pkg: _optional_hooks> python /localhome/user/.local/lib/python3.10/site-packages/pyproject_api/_backend.py True hatchling.build.pkg: get_requires_for_build_sdist> python /localhome/user/.local/lib/python3.10/site-packages/pyproject_api/_backend.py True hatchling.build.pkg: build_sdist> python /localhome/user/.local/lib/python3.10/site-packages/pyproject_api/_backend.py True hatchling.buildpylint: install_package> python -I -m pip install --force-reinstall --no-deps /local-scratch/localhome/user/Desktop/scrapy/.tox/.tmp/package/13/scrapy-2.13.3.tar.gzpylint: commands[0]> pylint conftest.py docs extras scrapy tests------------------------------------Your code has been rated at 10.00/10.pkg: _exit> python /localhome/user/.local/lib/python3.10/site-packages/pyproject_api/_backend.py True hatchling.build pylint: OK (46.90=setup[2.74]+cmd[44.16] seconds) congratulations :) (46.94 seconds) |
Fixed the issue#4858.