-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathlogging_helpers.py
More file actions
472 lines (415 loc) · 16.8 KB
/
logging_helpers.py
File metadata and controls
472 lines (415 loc) · 16.8 KB
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
import json
import configparser
from datetime import datetime, timezone
from pathlib import Path
import re
from typing import Optional
# Per-service minimum levels. If a service has an entry here, messages
# whose severity is lower than the configured value are suppressed.
# Values are numeric similar to logging module: DEBUG=10, INFO=20, WARNING=30, ERROR=40, CRITICAL=50
_LEVEL_MAP = {
'DEBUG': 10,
'INFO': 20,
'WARNING': 30,
'ERROR': 40,
'CRITICAL': 50,
}
_service_min_levels = {}
_global_min_level = 0
# Service-to-log-group mapping.
# Services listed here share a common log file (group name = log stem)
# instead of getting their own individual {service}.log file.
LOG_GROUPS: dict[str, str] = {
'VPSManager': 'PBGui',
'Config': 'PBGui',
'ParetoDataLoader':'PBGui',
'Status': 'PBGui',
'HyperliquidAWS': 'PBGui',
'BacktestV7': 'PBGui',
'OptimizeV7': 'PBGui',
}
DEFAULT_ROTATE_MAX_BYTES = 10 * 1024 * 1024
DEFAULT_ROTATE_BACKUP_COUNT = 1
def _normalize_rotate_key(value: str) -> str:
try:
key = re.sub(r"[^a-zA-Z0-9]+", "_", str(value or "").strip().lower()).strip("_")
return key or "default"
except Exception:
return "default"
def _read_rotate_ini():
cfg = configparser.ConfigParser()
cfg.read('pbgui.ini')
if not cfg.has_section('logging'):
cfg.add_section('logging')
return cfg
def _parse_positive_int(value, default_value: int) -> int:
try:
parsed = int(value)
if parsed > 0:
return parsed
except Exception:
pass
return int(default_value)
def get_rotate_defaults() -> tuple[int, int]:
"""Return default rotation settings (max_bytes, backup_count)."""
try:
cfg = _read_rotate_ini()
max_bytes = _parse_positive_int(
cfg.get('logging', 'rotate_default_max_bytes', fallback=str(DEFAULT_ROTATE_MAX_BYTES)),
DEFAULT_ROTATE_MAX_BYTES,
)
backup_count = _parse_positive_int(
cfg.get('logging', 'rotate_default_backup_count', fallback=str(DEFAULT_ROTATE_BACKUP_COUNT)),
DEFAULT_ROTATE_BACKUP_COUNT,
)
return max_bytes, backup_count
except Exception:
return DEFAULT_ROTATE_MAX_BYTES, DEFAULT_ROTATE_BACKUP_COUNT
def set_rotate_defaults(max_bytes: int, backup_count: int):
"""Persist default rotation settings in pbgui.ini under [logging]."""
cfg = _read_rotate_ini()
cfg.set('logging', 'rotate_default_max_bytes', str(_parse_positive_int(max_bytes, DEFAULT_ROTATE_MAX_BYTES)))
cfg.set('logging', 'rotate_default_backup_count', str(_parse_positive_int(backup_count, DEFAULT_ROTATE_BACKUP_COUNT)))
with open('pbgui.ini', 'w', encoding='utf-8') as f:
cfg.write(f)
def get_rotate_settings(service: str = None, logfile: str = None) -> tuple[int, int]:
"""Return effective rotation settings for a specific service/logfile.
Lookup order:
1) [logging] rotate_<service_key>_max_bytes / rotate_<service_key>_backup_count
2) [logging] rotate_default_max_bytes / rotate_default_backup_count
"""
default_max_bytes, default_backup_count = get_rotate_defaults()
key_src = service
if (not key_src) and logfile:
try:
key_src = Path(str(logfile)).stem
except Exception:
key_src = None
if not key_src:
return default_max_bytes, default_backup_count
try:
cfg = _read_rotate_ini()
key = _normalize_rotate_key(key_src)
max_bytes = _parse_positive_int(
cfg.get('logging', f'rotate_{key}_max_bytes', fallback=str(default_max_bytes)),
default_max_bytes,
)
backup_count = _parse_positive_int(
cfg.get('logging', f'rotate_{key}_backup_count', fallback=str(default_backup_count)),
default_backup_count,
)
return max_bytes, backup_count
except Exception:
return default_max_bytes, default_backup_count
def set_rotate_settings(service: str, max_bytes: int, backup_count: int):
"""Persist per-service rotation settings in pbgui.ini under [logging]."""
key = _normalize_rotate_key(service)
cfg = _read_rotate_ini()
cfg.set('logging', f'rotate_{key}_max_bytes', str(_parse_positive_int(max_bytes, DEFAULT_ROTATE_MAX_BYTES)))
cfg.set('logging', f'rotate_{key}_backup_count', str(_parse_positive_int(backup_count, DEFAULT_ROTATE_BACKUP_COUNT)))
with open('pbgui.ini', 'w', encoding='utf-8') as f:
cfg.write(f)
def set_service_min_level(service: str, level: Optional[str]):
"""Set a minimum level for `service`.
`level` may be a string like 'DEBUG'/'INFO'/... or None to remove the override.
"""
try:
if level is None or str(level).strip() == '':
_service_min_levels.pop(service, None)
return
lev = str(level).upper()
if lev in _LEVEL_MAP:
_service_min_levels[service] = _LEVEL_MAP[lev]
else:
# Unknown level: ignore
_service_min_levels.pop(service, None)
except Exception:
pass
def set_global_min_level(level: Optional[str]):
"""Set global minimum level applied when no per-service override exists."""
global _global_min_level
try:
if level is None or str(level).strip() == '':
_global_min_level = 0
return
lev = str(level).upper()
_global_min_level = _LEVEL_MAP.get(lev, 0)
except Exception:
pass
def is_debug_enabled(service: str) -> bool:
"""Return True when the effective minimum level for `service` is DEBUG or lower.
This is a convenience helper used by other modules to enable debug-only
behavior (e.g. verbose payload printing) when the service's log level is
set to DEBUG.
"""
try:
min_lvl = _service_min_levels.get(service, _global_min_level)
return min_lvl <= _LEVEL_MAP.get('DEBUG', 10)
except Exception:
return False
def _now_isoz():
# Local time ISO with milliseconds (consistent with other services)
return datetime.now().isoformat(timespec='milliseconds')
def _extract_leading_brackets(s: str):
"""Extract consecutive leading bracketed tokens from the start of string.
Returns (tags_list, rest_of_string).
"""
tags = []
pos = 0
L = len(s)
# skip leading spaces
while pos < L and s[pos].isspace():
pos += 1
while pos < L and s[pos] == '[':
end = s.find(']', pos+1)
if end == -1:
break
content = s[pos+1:end].strip()
tags.append(content)
pos = end + 1
# skip spaces after bracket
while pos < L and s[pos].isspace():
pos += 1
rest = s[pos:].lstrip()
return tags, rest
def _sanitize_tag(t: str) -> str:
# remove troublesome characters and limit length
t2 = re.sub(r"[\n\r\t]", ' ', t)
t2 = t2.replace(',', '').replace('"', '').replace("'", '')
if len(t2) > 60:
t2 = t2[:60]
return t2
def rotate_logfile_if_oversize(path: str, max_bytes: int = DEFAULT_ROTATE_MAX_BYTES, backup_count: int = DEFAULT_ROTATE_BACKUP_COUNT):
"""Rotate `path` when it exceeds `max_bytes`.
Keep current plus `backup_count` rotated generations named
`<path>.1`, `<path>.2`, ... `<path>.<backup_count>`.
This function is intentionally simple and safe to call before writes.
"""
try:
p = Path(path)
if not p.exists():
return
try:
size = p.stat().st_size
except Exception:
return
if size <= int(max_bytes):
return
backup_count = _parse_positive_int(backup_count, DEFAULT_ROTATE_BACKUP_COUNT)
# Drop oldest generation first if present
oldest = Path(str(path) + f'.{backup_count}')
try:
if oldest.exists():
oldest.unlink()
except Exception:
pass
# Shift existing generations upwards (N-1 -> N, ..., 1 -> 2)
for idx in range(backup_count - 1, 0, -1):
src = Path(str(path) + f'.{idx}')
dst = Path(str(path) + f'.{idx + 1}')
try:
if src.exists():
src.rename(dst)
except Exception:
pass
backup_1 = Path(str(path) + '.1')
# Rename current to .1 (atomic on same filesystem)
try:
p.rename(backup_1)
except Exception:
# Best-effort: try copy-then-truncate
try:
data = p.read_bytes()
backup_1.write_bytes(data)
p.unlink()
except Exception:
pass
except Exception:
# Never raise from logging helpers
pass
def purge_log_to_rotated(path: str, max_bytes: int = 10 * 1024 * 1024):
"""Purge `path` but keep the file. If `<path>.1` exists and has room,
append the content of `path` to it up to `max_bytes`. Otherwise, move
the current file to `<path>.1` (if none exists) or truncate `path`.
Returns (success: bool, message: str).
"""
try:
p = Path(path)
if not p.exists():
return False, 'Logfile does not exist'
rotated = Path(str(p) + '.1')
orig_size = p.stat().st_size
# If no rotated file exists, move current to .1 and recreate empty logfile
if not rotated.exists():
try:
p.replace(rotated)
p.open('w').close()
return True, f'Moved logfile to {rotated.name} and recreated {p.name}'
except Exception as e:
return False, f'Failed to move logfile to rotated: {e}'
rot_size = rotated.stat().st_size
# If rotated has room for the whole original, append it
if rot_size < max_bytes and (rot_size + orig_size) <= max_bytes:
try:
with rotated.open('ab') as rf, p.open('rb') as of:
rf.write(of.read())
with p.open('r+') as of:
of.truncate(0)
return True, f'Appended logfile to {rotated.name} and truncated {p.name}'
except Exception as e:
return False, f'Failed to append to rotated logfile: {e}'
# If appending would overflow the rotated file, prefer replacing the
# rotated file with the full current logfile so we don't lose the
# current content. If the current logfile itself is larger than
# max_bytes, write the tail of the current logfile (last max_bytes)
# into the rotated file instead.
try:
if orig_size <= max_bytes:
# Remove existing rotated and move current to rotated
try:
try:
rotated.unlink()
except Exception:
pass
p.replace(rotated)
# recreate empty original file
p.open('w').close()
return True, f'Replaced {rotated.name} with full logfile and recreated {p.name}'
except Exception as e:
return False, f'Failed to replace rotated logfile: {e}'
else:
# Current logfile larger than max_bytes: write only the tail
try:
with p.open('rb') as of:
of.seek(max(0, orig_size - max_bytes))
chunk = of.read()
with rotated.open('wb') as rf:
rf.write(chunk)
with p.open('r+') as of:
of.truncate(0)
return True, f'Wrote last {max_bytes} bytes of logfile to {rotated.name} and truncated {p.name}'
except Exception as e:
return False, f'Failed to write tail to rotated logfile: {e}'
except Exception as e:
return False, f'Failed to manage rotated logfile: {e}'
except Exception as e:
return False, f'Failed to purge logfile: {e}'
def human_log(service: str, msg: str, user: str = None, tags=None, level: str = None, code: str = None, meta: dict = None, logfile: str = None):
"""Write a canonical human-readable log line.
Format:
2025-11-20T12:55:50.123Z [SERVICE] [tag1] [tag2] [User:mani] message... {json_meta}
Leading bracket tokens inside `msg` are treated as tags if they appear
at the very start of `msg`. If one of those tokens matches `User:...`,
it is extracted as the `user` field.
"""
try:
if tags is None:
tags = []
# Extract leading brackets from msg (if any)
leading, rest = _extract_leading_brackets(msg or '')
# Recognize explicit level tokens among leading brackets
recognized_levels = {'DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'}
level_from_msg = None
for lt in leading:
up = lt.upper()
if up in recognized_levels and level_from_msg is None and not level:
level_from_msg = up
# do not add this token to tags
elif lt.lower().startswith('user:') and not user:
user = lt.split(':', 1)[1].strip()
else:
tags.append(lt)
# sanitize tags
tags = [_sanitize_tag(t) for t in tags if t]
# Simple normalization: prefer `user.name` for User objects, otherwise
# accept a string user or fall back to str(user).
try:
if isinstance(user, str):
user_name = user
elif user is None:
user_name = None
else:
user_name = getattr(user, 'name', None)
if user_name is None:
user_name = str(user)
except Exception:
try:
user_name = str(user)
except Exception:
user_name = None
parts = []
parts.append(_now_isoz())
parts.append(f'[{service}]')
# Determine final level (explicit param > message token > heuristic > default INFO)
if level:
lev = str(level).upper()
elif level_from_msg:
lev = level_from_msg
else:
# Heuristic: if caller didn't pass a level, infer from message content
try:
low = (rest or '').lower()
warn_tokens = ('warn', 'demot', 'backoff', 'timeout', 'could not', "couldn't", 'cannot', 'requesttimeout', 'rate limit')
if any(k in low for k in ('error', 'failed', 'exception', 'traceback')):
lev = 'ERROR'
elif any(k in low for k in warn_tokens) or re.search(r"\b429\b", low):
lev = 'WARNING'
elif any(k in low for k in ('debug', 'payload', 'preview')):
lev = 'DEBUG'
else:
lev = 'INFO'
except Exception:
lev = 'INFO'
parts.append(f'[{lev}]')
# Respect per-service/global minimum levels: if the message level is lower
# than configured minimum, skip writing the log.
try:
msg_level_num = _LEVEL_MAP.get(lev, 20)
min_lvl = _service_min_levels.get(service, _global_min_level)
if msg_level_num < min_lvl:
return
except Exception:
pass
for t in tags:
parts.append(f'[{t}]')
if user_name:
# sanitize user_name for safety and length
try:
u = _sanitize_tag(str(user_name))
except Exception:
u = str(user_name)
parts.append(f'[User:{u}]')
# main message
line = ' '.join(parts) + ' ' + (rest or '')
if code:
line = line + ' ' + str(code)
# append meta as JSON if present
if meta is not None:
try:
j = json.dumps(meta, ensure_ascii=False)
line = line + ' ' + j
except Exception:
# ignore meta serialization errors
pass
# Determine logfile path
if not logfile:
p = Path.cwd() / 'data' / 'logs'
p.mkdir(parents=True, exist_ok=True)
log_stem = LOG_GROUPS.get(service, service)
logfile = str(p / f'{log_stem}.log')
# Rotate if oversize (keep only current + one rotated generation)
try:
rotate_max_bytes, rotate_backup_count = get_rotate_settings(service=service, logfile=logfile)
rotate_logfile_if_oversize(logfile, rotate_max_bytes, rotate_backup_count)
except Exception:
pass
# Append line atomically (no fsync)
with open(logfile, 'a', encoding='utf-8') as f:
f.write(line.rstrip() + '\n')
f.flush()
except Exception:
# Best-effort; do not raise from logging
try:
print(f"{_now_isoz()} [{service}] {msg}")
except Exception:
pass