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 #4840

Closed
wants to merge 2 commits into from
Closed
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
: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
from twisted.python.failure import Failure

Expand Down Expand Up @@ -122,7 +122,14 @@ def _get_handler(settings):
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:
max_bytes = settings.get('LOG_MAX_BYTES', 0)
log_backup_count = settings.get('LOG_BACKUP_COUNT', 0)
handler = RotatingFileHandler(filename, maxBytes=max_bytes,
backupCount=log_backup_count,
encoding=encoding)
else:
handler = logging.FileHandler(filename, encoding=encoding)
elif settings.getbool('LOG_ENABLED'):
handler = logging.StreamHandler()
else:
Expand Down