OpenTransformer commited on
Commit
eb5b2f9
·
verified ·
1 Parent(s): 59bcafe

Upload rotating_log.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. rotating_log.py +81 -0
rotating_log.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ ROTATING STDOUT - Dual logs: 5000 lines (main) + 2500 lines (compact)
3
+ """
4
+ import sys
5
+ import os
6
+ from datetime import datetime
7
+
8
+ MAX_LINES = 5000
9
+ MAX_LINES_COMPACT = 2500
10
+ LOG_FILE = os.environ.get('TRAINING_LOG', '/root/training.log')
11
+ LOG_FILE_COMPACT = LOG_FILE.replace('.log', '_compact.log')
12
+ ARCHIVE_DIR = LOG_FILE.replace('.log', '_archives')
13
+
14
+ class RotatingStdout:
15
+ def __init__(self, max_lines=MAX_LINES, max_compact=MAX_LINES_COMPACT):
16
+ self.max_lines = max_lines
17
+ self.max_compact = max_compact
18
+ self.line_count = 0
19
+ self.line_count_compact = 0
20
+ self.original_stdout = sys.stdout
21
+ self.rotating = False
22
+ os.makedirs(ARCHIVE_DIR, exist_ok=True)
23
+ self.log_file = open(LOG_FILE, 'a', buffering=1)
24
+ self.log_compact = open(LOG_FILE_COMPACT, 'a', buffering=1)
25
+
26
+ def _rotate_main(self):
27
+ self.log_file.close()
28
+ timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
29
+ archive_path = os.path.join(ARCHIVE_DIR, f'archive_{timestamp}.log')
30
+ with open(LOG_FILE, 'r') as f:
31
+ lines = f.readlines()
32
+ keep = min(self.max_lines // 2, len(lines))
33
+ with open(archive_path, 'w') as f:
34
+ f.writelines(lines[:-keep] if keep else lines)
35
+ with open(LOG_FILE, 'w') as f:
36
+ f.writelines(lines[-keep:] if keep else [])
37
+ self.line_count = keep
38
+ self.log_file = open(LOG_FILE, 'a', buffering=1)
39
+
40
+ def _rotate_compact(self):
41
+ self.log_compact.close()
42
+ with open(LOG_FILE_COMPACT, 'r') as f:
43
+ lines = f.readlines()
44
+ keep = min(self.max_compact // 2, len(lines))
45
+ with open(LOG_FILE_COMPACT, 'w') as f:
46
+ f.writelines(lines[-keep:] if keep else [])
47
+ self.line_count_compact = keep
48
+ self.log_compact = open(LOG_FILE_COMPACT, 'a', buffering=1)
49
+
50
+ def _rotate_if_needed(self):
51
+ if self.rotating:
52
+ return
53
+ self.rotating = True
54
+ if self.line_count > self.max_lines:
55
+ self._rotate_main()
56
+ if self.line_count_compact > self.max_compact:
57
+ self._rotate_compact()
58
+ self.rotating = False
59
+
60
+ def write(self, text):
61
+ self.original_stdout.write(text)
62
+ if not self.rotating:
63
+ self.log_file.write(text)
64
+ self.log_compact.write(text)
65
+ newlines = text.count('\n')
66
+ self.line_count += newlines
67
+ self.line_count_compact += newlines
68
+ self._rotate_if_needed()
69
+
70
+ def flush(self):
71
+ self.original_stdout.flush()
72
+ if self.log_file:
73
+ self.log_file.flush()
74
+ if self.log_compact:
75
+ self.log_compact.flush()
76
+
77
+ def install_rotating_log():
78
+ rotating = RotatingStdout()
79
+ sys.stdout = rotating
80
+ sys.stderr = rotating
81
+ print(f"[rotating_log] Active. Main={MAX_LINES}, Compact={MAX_LINES_COMPACT} lines.")