Skip to content

Commit a851bb9

Browse files
authored
Merge pull request #528 from hneiva/log-rotation
Bug 1613283 - Add log rotation option for taskcluster.yaml
2 parents 4724371 + db3f684 commit a851bb9

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

src/scriptworker/constants.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@
5656
"task_max_timeout_status": STATUSES["intermittent-task"],
5757
"invalid_reclaim_status": STATUSES["intermittent-task"],
5858
"task_script": ("bash", "-c", "echo foo && sleep 19 && exit 1"),
59+
# Logging settings
5960
"verbose": True,
61+
"log_max_bytes": 0,
62+
"log_max_backups": 10,
6063
# Task settings
6164
"work_dir": "...",
6265
"log_dir": "...",

src/scriptworker/log.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ def update_logging_config(context: Any, log_name: Optional[str] = None, file_nam
5757
# If we rotate the log file via logrotate.d, let's watch the file
5858
# so we can automatically close/reopen on move.
5959
handler = logging.handlers.WatchedFileHandler(path) # type: ignore
60+
elif context.config["log_max_bytes"] and context.config["log_max_backups"]:
61+
handler = logging.handlers.RotatingFileHandler( # type: ignore
62+
filename=path,
63+
maxBytes=context.config["log_max_bytes"],
64+
backupCount=context.config["log_max_backups"],
65+
)
6066
else:
6167
# Avoid using WatchedFileHandler during scriptworker unittests
6268
handler = logging.FileHandler(path) # type: ignore

tests/test_log.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,20 @@ def test_watched_log_file(rw_context):
115115
with open(path, "r") as fh:
116116
assert fh.read().rstrip() == "INFO - bar"
117117
close_handlers(log_name=rw_context.config["log_dir"])
118+
119+
120+
def test_rotating_log_file(rw_context):
121+
# 500 should be enough to ~fill 2 files
122+
MAX_SIZE = 500 # bytes
123+
rw_context.config["watch_log_file"] = False
124+
rw_context.config["log_max_bytes"] = MAX_SIZE
125+
rw_context.config["log_max_backups"] = 1
126+
rw_context.config["log_fmt"] = "%(levelname)s - %(message)s"
127+
swlog.update_logging_config(rw_context, log_name=rw_context.config["log_dir"])
128+
path = os.path.join(rw_context.config["log_dir"], "worker.log")
129+
log = logging.getLogger(rw_context.config["log_dir"])
130+
for x in range(30):
131+
log.info(f"{x}" * x)
132+
assert os.path.getsize(path) < MAX_SIZE
133+
assert os.path.getsize(path + ".1") < MAX_SIZE
134+
close_handlers(log_name=rw_context.config["log_dir"])

0 commit comments

Comments
 (0)