Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Open
AliAlimohammadi wants to merge9 commits intoscrapy:master
base:master
Choose a base branch
Loading
fromAliAlimohammadi:systemd_logs
Open
Show file tree
Hide file tree
Changes from1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
PrevPrevious commit
NextNext commit
Added optional structured logging to systemd journal.
  • Loading branch information
@AliAlimohammadi
AliAlimohammadi committedOct 24, 2025
commit4c2dc389f2d20f48c77d8ef5b907a6b7aa8d596d
13 changes: 13 additions & 0 deletionsdocs/topics/logging.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -170,6 +170,7 @@ These settings can be used to configure the logging:
* :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 thinkLOG_SYSTEMD orLOG_SYSTEMD_ENABLED would be more in line with the naming of other Scrapy settings.


The first couple of settings define a destination for log messages. If
:setting:`LOG_FILE` is set, messages sent through the root logger will be
Expand All@@ -194,6 +195,18 @@ If :setting:`LOG_SHORT_NAMES` is set, then the logs will not display the Scrapy
component that prints the log. It is unset by default, hence logs contain the
Scrapy component responsible for that log output.

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

@GallaecioGallaecioOct 24, 2025
edited
Loading

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.

Choose a reason for hiding this comment

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

Done.


Command-line options
--------------------

Expand Down
2 changes: 2 additions & 0 deletionsscrapy/settings/default_settings.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -124,6 +124,7 @@
"LOG_LEVEL",
"LOG_SHORT_NAMES",
"LOG_STDOUT",
"LOG_TO_SYSTEMD",
"LOG_VERSIONS",
"MAIL_FROM",
"MAIL_HOST",
Expand DownExpand Up@@ -399,6 +400,7 @@
LOG_LEVEL = "DEBUG"
LOG_SHORT_NAMES = False
LOG_STDOUT = False
LOG_TO_SYSTEMD = False
LOG_VERSIONS = [
"lxml",
"libxml2",
Expand Down
15 changes: 3 additions & 12 deletionsscrapy/utils/log.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -22,13 +22,6 @@
from scrapy.crawler import Crawler
from scrapy.logformatter import LogFormatterResult

# Check for systemd journal availability
try:
import systemd.journal
SYSTEMD_JOURNAL_AVAILABLE = True
except ImportError:
SYSTEMD_JOURNAL_AVAILABLE = False


logger = logging.getLogger(__name__)

Expand DownExpand Up@@ -172,11 +165,9 @@ def _get_handler(settings: Settings) -> logging.Handler:
encoding = settings.get("LOG_ENCODING")
handler = logging.FileHandler(filename, mode=mode, encoding=encoding)
elif settings.getbool("LOG_ENABLED"):
# Use systemd journal handler if running under systemd
if (
SYSTEMD_JOURNAL_AVAILABLE
and os.getenv("JOURNAL_STREAM")
):
if settings.getbool("LOG_TO_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.

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.

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.

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.

handler = systemd.journal.JournalHandler()
else:
handler = logging.StreamHandler()
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp