File size: 10,750 Bytes
0bbe516
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
"""
Log Management Module for Cain Memory System.

Provides log rotation, compression, and cleanup functionality to prevent
unbounded log growth and maintain system health.

Features:
- Retains logs for 7 days
- Compresses logs older than 3 days using gzip
- Cleans up .bak files, keeping only the 3 most recent
- Provides health statistics about log storage
"""

import os
import gzip
import shutil
import logging
from datetime import datetime, timedelta
from typing import List, Dict, Tuple
from pathlib import Path

logger = logging.getLogger(__name__)

class LogRetentionConfig:
    """Configuration for log retention policy."""

    # Retention periods
    MAX_LOG_AGE_DAYS = 7          # Delete logs older than 7 days
    COMPRESS_AGE_DAYS = 3         # Compress logs older than 3 days
    MAX_BAK_FILES = 3             # Keep only 3 most recent .bak files

    # Log directories to manage
    LOG_DIRECTORIES = [
        "/home/node/logs",
        "/tmp/claude-workspace/.openclaw/logs",
        "/data/logs",
        "/app/logs",
    ]

    # File patterns to consider as logs
    LOG_PATTERNS = ["*.log", "*.txt", "*.json"]

    @classmethod
    def add_log_directory(cls, path: str):
        """Add a custom log directory to manage."""
        if path not in cls.LOG_DIRECTORIES:
            cls.LOG_DIRECTORIES.append(path)


class LogManager:
    """Manages log rotation, compression, and cleanup."""

    def __init__(self, config: LogRetentionConfig = None):
        """
        Initialize the log manager.

        Args:
            config: Log retention configuration. Uses defaults if None.
        """
        self.config = config or LogRetentionConfig()
        self.stats = {
            "processed_dirs": 0,
            "logs_compressed": 0,
            "logs_deleted": 0,
            "bak_files_deleted": 0,
            "space_freed_bytes": 0,
            "errors": []
        }

    def rotate_all_logs(self) -> Dict[str, any]:
        """
        Run log rotation on all configured directories.

        Returns:
            Dictionary with rotation statistics.
        """
        self.stats = {
            "processed_dirs": 0,
            "logs_compressed": 0,
            "logs_deleted": 0,
            "bak_files_deleted": 0,
            "space_freed_bytes": 0,
            "errors": []
        }

        logger.info("Starting log rotation process...")

        for log_dir in self.config.LOG_DIRECTORIES:
            if os.path.isdir(log_dir):
                try:
                    self._rotate_directory(log_dir)
                    self.stats["processed_dirs"] += 1
                except Exception as e:
                    error_msg = f"Failed to rotate {log_dir}: {e}"
                    logger.error(error_msg)
                    self.stats["errors"].append(error_msg)
            else:
                logger.debug(f"Log directory not found: {log_dir}")

        # Log summary
        logger.info(
            f"Log rotation complete. "
            f"Dirs: {self.stats['processed_dirs']}, "
            f"Compressed: {self.stats['logs_compressed']}, "
            f"Deleted: {self.stats['logs_deleted']}, "
            f"BAK cleaned: {self.stats['bak_files_deleted']}, "
            f"Space freed: {self._format_bytes(self.stats['space_freed_bytes'])}"
        )

        return self.stats

    def _rotate_directory(self, log_dir: str):
        """Rotate logs in a single directory."""
        logger.debug(f"Processing log directory: {log_dir}")

        # Get all log files
        log_files = self._get_log_files(log_dir)

        for log_file in log_files:
            try:
                self._process_log_file(log_file)
            except Exception as e:
                error_msg = f"Failed to process {log_file}: {e}"
                logger.warning(error_msg)
                self.stats["errors"].append(error_msg)

        # Clean up .bak files
        self._cleanup_bak_files(log_dir)

    def _get_log_files(self, log_dir: str) -> List[Path]:
        """Get all log files in the directory."""
        log_files = []
        log_path = Path(log_dir)

        for pattern in self.config.LOG_PATTERNS:
            log_files.extend(log_path.glob(pattern))
            # Also check for already compressed files
            log_files.extend(log_path.glob(f"{pattern}.gz"))

        # Filter out directories and sort by modification time
        return sorted(
            [f for f in log_files if f.is_file()],
            key=lambda f: f.stat().st_mtime
        )

    def _process_log_file(self, log_file: Path):
        """Process a single log file (compress or delete)."""
        mtime = datetime.fromtimestamp(log_file.stat().st_mtime)
        age_days = (datetime.now() - mtime).days
        size_before = log_file.stat().st_size

        # Skip if already compressed
        if log_file.suffix == ".gz":
            if age_days > self.config.MAX_LOG_AGE_DAYS:
                logger.debug(f"Deleting old compressed log: {log_file} ({age_days} days old)")
                log_file.unlink()
                self.stats["logs_deleted"] += 1
                self.stats["space_freed_bytes"] += size_before
            return

        # Delete old logs
        if age_days > self.config.MAX_LOG_AGE_DAYS:
            logger.debug(f"Deleting old log: {log_file} ({age_days} days old)")
            log_file.unlink()
            self.stats["logs_deleted"] += 1
            self.stats["space_freed_bytes"] += size_before
            return

        # Compress logs older than threshold
        if age_days > self.config.COMPRESS_AGE_DAYS:
            self._compress_log(log_file)
            self.stats["logs_compressed"] += 1
            # Calculate space saved
            if log_file.with_suffix(log_file.suffix + ".gz").exists():
                size_after = log_file.with_suffix(log_file.suffix + ".gz").stat().st_size
                self.stats["space_freed_bytes"] += (size_before - size_after)

    def _compress_log(self, log_file: Path):
        """Compress a log file using gzip."""
        compressed_path = log_file.with_suffix(log_file.suffix + ".gz")

        if compressed_path.exists():
            logger.debug(f"Compressed file already exists: {compressed_path}")
            return

        logger.debug(f"Compressing: {log_file}")
        with open(log_file, 'rb') as f_in:
            with gzip.open(compressed_path, 'wb') as f_out:
                shutil.copyfileobj(f_in, f_out)

        # Remove original after successful compression
        log_file.unlink()
        logger.debug(f"Compressed: {log_file} -> {compressed_path}")

    def _cleanup_bak_files(self, log_dir: str):
        """Clean up .bak files, keeping only the most recent ones."""
        log_path = Path(log_dir)
        bak_files = sorted(
            log_path.glob("*.bak"),
            key=lambda f: f.stat().st_mtime,
            reverse=True  # Newest first
        )

        # Keep the N most recent, delete the rest
        to_delete = bak_files[self.config.MAX_BAK_FILES:]

        for bak_file in to_delete:
            size = bak_file.stat().st_size
            logger.debug(f"Deleting old .bak file: {bak_file}")
            bak_file.unlink()
            self.stats["bak_files_deleted"] += 1
            self.stats["space_freed_bytes"] += size

    def get_log_health_status(self) -> Dict[str, any]:
        """
        Get health status of log directories.

        Returns:
            Dictionary with log health information.
        """
        status = {
            "directories": {},
            "total_size_bytes": 0,
            "total_files": 0,
            "needs_rotation": False
        }

        for log_dir in self.config.LOG_DIRECTORIES:
            if not os.path.isdir(log_dir):
                continue

            dir_info = self._get_directory_info(log_dir)
            status["directories"][log_dir] = dir_info
            status["total_size_bytes"] += dir_info["size_bytes"]
            status["total_files"] += dir_info["file_count"]

            if dir_info.get("has_old_logs", False):
                status["needs_rotation"] = True

        status["total_size_human"] = self._format_bytes(status["total_size_bytes"])
        return status

    def _get_directory_info(self, log_dir: str) -> Dict[str, any]:
        """Get information about a log directory."""
        log_path = Path(log_dir)
        files = list(log_path.glob("*"))
        total_size = sum(f.stat().st_size for f in files if f.is_file())

        # Check for old logs
        now = datetime.now()
        has_old_logs = False
        oldest_age_days = 0

        for f in files:
            if f.is_file():
                mtime = datetime.fromtimestamp(f.stat().st_mtime)
                age_days = (now - mtime).days
                if age_days > self.config.COMPRESS_AGE_DAYS:
                    has_old_logs = True
                oldest_age_days = max(oldest_age_days, age_days)

        return {
            "size_bytes": total_size,
            "size_human": self._format_bytes(total_size),
            "file_count": len(files),
            "has_old_logs": has_old_logs,
            "oldest_age_days": oldest_age_days
        }

    @staticmethod
    def _format_bytes(size_bytes: int) -> str:
        """Format byte count to human-readable string."""
        for unit in ['B', 'KB', 'MB', 'GB']:
            if size_bytes < 1024.0:
                return f"{size_bytes:.1f}{unit}"
            size_bytes /= 1024.0
        return f"{size_bytes:.1f}TB"


def rotate_logs() -> Dict[str, any]:
    """
    Convenience function to rotate logs with default configuration.

    Returns:
        Dictionary with rotation statistics.
    """
    manager = LogManager()
    return manager.rotate_all_logs()


def get_log_health() -> Dict[str, any]:
    """
    Convenience function to get log health status.

    Returns:
        Dictionary with log health information.
    """
    manager = LogManager()
    return manager.get_log_health_status()


if __name__ == "__main__":
    # Run log rotation when executed directly
    logging.basicConfig(
        level=logging.INFO,
        format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
    )

    stats = rotate_logs()
    print(f"\n=== Log Rotation Summary ===")
    print(f"Directories processed: {stats['processed_dirs']}")
    print(f"Logs compressed: {stats['logs_compressed']}")
    print(f"Logs deleted: {stats['logs_deleted']}")
    print(f"BAK files deleted: {stats['bak_files_deleted']}")
    print(f"Space freed: {LogManager._format_bytes(stats['space_freed_bytes'])}")

    if stats['errors']:
        print(f"\nErrors encountered: {len(stats['errors'])}")
        for error in stats['errors']:
            print(f"  - {error}")