Skip to content

Commit

Permalink
Merge pull request #528 from hneiva/log-rotation
Browse files Browse the repository at this point in the history
Bug 1613283 - Add log rotation option for taskcluster.yaml
  • Loading branch information
hneiva committed Feb 10, 2022
2 parents 4724371 + db3f684 commit a851bb9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/scriptworker/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@
"task_max_timeout_status": STATUSES["intermittent-task"],
"invalid_reclaim_status": STATUSES["intermittent-task"],
"task_script": ("bash", "-c", "echo foo && sleep 19 && exit 1"),
# Logging settings
"verbose": True,
"log_max_bytes": 0,
"log_max_backups": 10,
# Task settings
"work_dir": "...",
"log_dir": "...",
Expand Down
6 changes: 6 additions & 0 deletions src/scriptworker/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ def update_logging_config(context: Any, log_name: Optional[str] = None, file_nam
# If we rotate the log file via logrotate.d, let's watch the file
# so we can automatically close/reopen on move.
handler = logging.handlers.WatchedFileHandler(path) # type: ignore
elif context.config["log_max_bytes"] and context.config["log_max_backups"]:
handler = logging.handlers.RotatingFileHandler( # type: ignore
filename=path,
maxBytes=context.config["log_max_bytes"],
backupCount=context.config["log_max_backups"],
)
else:
# Avoid using WatchedFileHandler during scriptworker unittests
handler = logging.FileHandler(path) # type: ignore
Expand Down
17 changes: 17 additions & 0 deletions tests/test_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,20 @@ def test_watched_log_file(rw_context):
with open(path, "r") as fh:
assert fh.read().rstrip() == "INFO - bar"
close_handlers(log_name=rw_context.config["log_dir"])


def test_rotating_log_file(rw_context):
# 500 should be enough to ~fill 2 files
MAX_SIZE = 500 # bytes
rw_context.config["watch_log_file"] = False
rw_context.config["log_max_bytes"] = MAX_SIZE
rw_context.config["log_max_backups"] = 1
rw_context.config["log_fmt"] = "%(levelname)s - %(message)s"
swlog.update_logging_config(rw_context, log_name=rw_context.config["log_dir"])
path = os.path.join(rw_context.config["log_dir"], "worker.log")
log = logging.getLogger(rw_context.config["log_dir"])
for x in range(30):
log.info(f"{x}" * x)
assert os.path.getsize(path) < MAX_SIZE
assert os.path.getsize(path + ".1") < MAX_SIZE
close_handlers(log_name=rw_context.config["log_dir"])

0 comments on commit a851bb9

Please sign in to comment.