Skip to content

Commit 94f7f96

Browse files
authored
fix: added rotating logfile handler with max size to prevent large logs (#114)
1 parent d7c91cd commit 94f7f96

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

aw_core/log.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
import logging
12
import os
23
import sys
3-
import logging
4-
from typing import Optional, List
54
from datetime import datetime
5+
from logging.handlers import RotatingFileHandler
6+
from typing import List, Optional
67

78
from . import dirs
89
from .decorators import deprecated
@@ -100,7 +101,13 @@ def _create_file_handler(
100101
log_name = name + "_" + ("testing_" if testing else "") + now_str + file_ext
101102
log_file_path = os.path.join(log_dir, log_name)
102103

103-
fh = logging.FileHandler(log_file_path, mode="w")
104+
# Create rotating logfile handler, max 10MB per file, 3 files max
105+
# Prevents logfile from growing too large, like in:
106+
# - https://github.com/ActivityWatch/activitywatch/issues/815#issue-1423555466
107+
# - https://github.com/ActivityWatch/activitywatch/issues/756#issuecomment-1266662861
108+
fh = RotatingFileHandler(
109+
log_file_path, mode="a", maxBytes=10 * 1024 * 1024, backupCount=3
110+
)
104111
if log_json:
105112
fh.setFormatter(_create_json_formatter())
106113
else:

0 commit comments

Comments
 (0)