Skip to content

Conversation

@AliAlimohammadi
Copy link

Fixed the issue #4858.

@wRAR
Copy link
Member

wRAR commented Oct 24, 2025

Obligatory "I wonder how to test this".

I'm also not sure if enabling this if and only if JOURNAL_STREAM is set is correct (but then I'm not sure what should be the use cases).

handler = logging.FileHandler(filename, mode=mode, encoding=encoding)
elif settings.getbool("LOG_ENABLED"):
handler = logging.StreamHandler()
# Use systemd journal handler if running under systemd
Copy link
Member

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.

Copy link
Author

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 enabled
        if (
            settings.getbool("LOG_TO_SYSTEMD", False)
            and SYSTEMD_JOURNAL_AVAILABLE
        ):
            handler = systemd.journal.JournalHandler()
        else:
            handler = logging.StreamHandler()

@Gallaecio
Copy link
Member

What do you guys think I should do?

  • Remove the env var condition unless it is necessary.
  • Define a new boolean Scrapy setting, False by default.
  • If the setting is enabled, do the whole thing, and let the exception raise if the systemd Python package is missing.
  • In the documentation for the new setting, mention the need to install the Python systemd package for the feature to work. Also, if the very first version of that package is not compatible, you should document the minimum required version.

@AliAlimohammadi
Copy link
Author

Please check it now.

* :setting:`LOG_DATEFORMAT`
* :setting:`LOG_STDOUT`
* :setting:`LOG_SHORT_NAMES`
* :setting:`LOG_TO_SYSTEMD`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think LOG_SYSTEMD or LOG_SYSTEMD_ENABLED would be more in line with the naming of other Scrapy settings.

Comment on lines 198 to 208
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.
Copy link
Member

@Gallaecio Gallaecio Oct 24, 2025

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 running tox -e docs to check how your docs render.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@codecov
Copy link

codecov bot commented Oct 26, 2025

Codecov Report

❌ Patch coverage is 50.00000% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 91.41%. Comparing base (8c5fa6e) to head (c9997eb).
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
scrapy/utils/log.py 40.00% 2 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #7115      +/-   ##
==========================================
- Coverage   91.43%   91.41%   -0.03%     
==========================================
  Files         165      165              
  Lines       12644    12649       +5     
  Branches     1621     1622       +1     
==========================================
+ Hits        11561    11563       +2     
- Misses        810      812       +2     
- Partials      273      274       +1     
Files with missing lines Coverage Δ
scrapy/settings/default_settings.py 100.00% <100.00%> (ø)
scrapy/utils/log.py 92.98% <40.00%> (-2.48%) ⬇️

@AliAlimohammadi
Copy link
Author

Anything else I should do?

@wRAR
Copy link
Member

wRAR commented Oct 27, 2025

@AliAlimohammadi at the very least you should fix the CI failures and make sure you run these checks locally before pushing.

@AliAlimohammadi
Copy link
Author

But we wanted to raise an exception. Unless you want me to gracefully raise an exception in try block?

@AliAlimohammadi
Copy link
Author

@Gallaecio Can you please let me know how exactly to handle the exception?

@wRAR
Copy link
Member

wRAR commented Oct 28, 2025

But we wanted to raise an exception. Unless you want me to gracefully raise an exception in try block?

It doesn't look like you understood what I said, do you need some clarifications?

@Gallaecio
Copy link
Member

Gallaecio commented Oct 28, 2025

The docs CI reports:

/home/runner/work/scrapy/scrapy/docs/topics/settings.rst:1475: ERROR: Unexpected indentation. [docutils]
/home/runner/work/scrapy/scrapy/docs/topics/settings.rst:1476: WARNING: Block quote ends without a blank line; unexpected unindent. [docutils]

You should run tox -e docs to build the docs locally and make sure they do not error out.

Also, you should use pre-commit.

@AliAlimohammadi
Copy link
Author

Can you check it now?

tox.ini Outdated
deps =
{[testenv:extra-deps]deps}
pylint==4.0.2
systend-python; sys-platform=="linux"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

??

handler = logging.StreamHandler()
if settings.getbool("LOG_SYSTEMD", False):
# Opt-in systemd journal logging, will raise if systemd.journal is missing
import systemd.journal
Copy link
Member

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.

Copy link
Author

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.

Copy link
Author

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.

Copy link
Member

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.

Copy link
Author

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 wRAR marked this pull request as draft November 4, 2025 20:22
@AliAlimohammadi AliAlimohammadi marked this pull request as ready for review November 5, 2025 00:31
@wRAR
Copy link
Member

wRAR commented Nov 5, 2025

Have you tested these changes? If so, can you please describe how?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants