Skip to content
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

improve the log configuration with using RotatingFileHandler. #4844

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 8 additions & 1 deletion docs/topics/logging.rst
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ Logging settings
These settings can be used to configure the logging:

* :setting:`LOG_FILE`
* :setting:`LOG_ROTATING`
* :setting:`LOG_MAX_BYTES`
* :setting:`LOG_BACKUP_COUNT`
* :setting:`LOG_ENABLED`
* :setting:`LOG_ENCODING`
* :setting:`LOG_LEVEL`
Expand All @@ -154,7 +157,11 @@ These settings can be used to configure the logging:
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
redirected to a file named :setting:`LOG_FILE` with encoding
:setting:`LOG_ENCODING`. If unset and :setting:`LOG_ENABLED` is ``True``, log
:setting:`LOG_ENCODING`. If :setting:`LOG_FILE` is set and
:setting:`LOG_ROTATING` is ```True```, the file named
Copy link
Member

Choose a reason for hiding this comment

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

💄

Suggested change
:setting:`LOG_ROTATING` is ```True```, the file named
:setting:`LOG_ROTATING` is ``True``, the file named

:setting:`LOG_FILE` is allowed to rollover at a predetermined size according to
:setting:`LOG_MAX_BYTES` and :setting:`LOG_BACKUP_COUNT`.
If unset and :setting:`LOG_ENABLED` is ``True``, log
messages will be displayed on the standard error. Lastly, if
:setting:`LOG_ENABLED` is ``False``, there won't be any visible log output.

Expand Down
11 changes: 9 additions & 2 deletions scrapy/utils/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import sys
import warnings
from logging.config import dictConfig

from logging.handlers import RotatingFileHandler
from twisted.python import log as twisted_log
Comment on lines 4 to 6
Copy link
Member

Choose a reason for hiding this comment

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

💄 Please, keep the empty line separating imports from the standard library from imports from third-party libraries.

from twisted.python.failure import Failure

Expand Down Expand Up @@ -122,7 +122,14 @@
filename = settings.get('LOG_FILE')
if filename:
encoding = settings.get('LOG_ENCODING')
handler = logging.FileHandler(filename, encoding=encoding)
if settings.get("LOG_ROTATING") is True:
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if settings.get("LOG_ROTATING") is True:
if settings.getbool("LOG_ROTATING") is True:

This makes it possible to set the setting from the command line, where every value is a string.

max_bytes = settings.get('LOG_MAX_BYTES', 0)
log_backup_count = settings.get('LOG_BACKUP_COUNT', 0)
handler = RotatingFileHandler(filename, maxBytes=max_bytes,

Check warning on line 128 in scrapy/utils/log.py

View check run for this annotation

Codecov / codecov/patch

scrapy/utils/log.py#L126-L128

Added lines #L126 - L128 were not covered by tests
backupCount=log_backup_count,
encoding=encoding)
else:
handler = logging.FileHandler(filename, encoding=encoding)
elif settings.getbool('LOG_ENABLED'):
handler = logging.StreamHandler()
else:
Expand Down