infra-2026-10-045: WatchedRotatingFileHandler resistant au remplacement d inode
This commit is contained in:
1 parent
67f33fc81e
commit
a152e5a33f
2 files changed
+109
-2
No files matched your search
@@ -15,6 +15,7 @@ import logging.handlers
|
||||
import os
|
||||
import re
|
||||
import sqlite3
|
||||
import stat
|
||||
import sys
|
||||
import threading
|
||||
import time
|
||||
@@ -42,13 +43,69 @@ RATE_LIMIT_ASK_PER_MIN = int(os.environ.get("RATE_LIMIT_ASK_PER_MIN", "10"))
|
||||
TIMEOUT_SECONDS = float(os.environ.get("TIMEOUT_SECONDS", "60.0"))
|
||||
RETRY_DELAY_SECONDS = float(os.environ.get("RETRY_DELAY_SECONDS", "2.0"))
|
||||
|
||||
class WatchedRotatingFileHandler(logging.handlers.RotatingFileHandler):
|
||||
"""Sous-classe de RotatingFileHandler qui surveille le couple (périphérique, inode)
|
||||
et rouvre le fichier s'il a changé ou disparu avant chaque écriture (comportement
|
||||
de WatchedFileHandler avec conservation de la rotation maxBytes et backupCount).
|
||||
"""
|
||||
|
||||
def __init__(self, filename, mode='a', maxBytes=0, backupCount=0,
|
||||
encoding=None, delay=False, errors=None):
|
||||
super().__init__(filename, mode=mode, maxBytes=maxBytes,
|
||||
backupCount=backupCount, encoding=encoding,
|
||||
delay=delay, errors=errors)
|
||||
self.dev, self.ino = -1, -1
|
||||
self._statstream()
|
||||
|
||||
def _statstream(self):
|
||||
if self.stream:
|
||||
try:
|
||||
sres = os.fstat(self.stream.fileno())
|
||||
self.dev, self.ino = sres[stat.ST_DEV], sres[stat.ST_INO]
|
||||
except Exception:
|
||||
self.dev, self.ino = -1, -1
|
||||
|
||||
def reopen_if_needed(self):
|
||||
try:
|
||||
sres = os.stat(self.baseFilename)
|
||||
except OSError:
|
||||
sres = None
|
||||
if not sres or sres[stat.ST_DEV] != self.dev or sres[stat.ST_INO] != self.ino:
|
||||
if self.stream is not None:
|
||||
try:
|
||||
self.stream.flush()
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
self.stream.close()
|
||||
except Exception:
|
||||
pass
|
||||
self.stream = None
|
||||
try:
|
||||
os.makedirs(os.path.dirname(os.path.abspath(self.baseFilename)), exist_ok=True)
|
||||
except Exception:
|
||||
pass
|
||||
self.stream = self._open()
|
||||
self._statstream()
|
||||
|
||||
def emit(self, record):
|
||||
try:
|
||||
self.reopen_if_needed()
|
||||
if self.shouldRollover(record):
|
||||
self.doRollover()
|
||||
self._statstream()
|
||||
logging.FileHandler.emit(self, record)
|
||||
except Exception:
|
||||
self.handleError(record)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- Journalisation des accès
|
||||
os.makedirs(LOGS_DIR, exist_ok=True)
|
||||
access_logger = logging.getLogger("notebook_mcp.access")
|
||||
access_logger.setLevel(logging.INFO)
|
||||
access_logger.propagate = False
|
||||
if not access_logger.handlers:
|
||||
rfh = logging.handlers.RotatingFileHandler(
|
||||
rfh = WatchedRotatingFileHandler(
|
||||
ACCESS_LOG_PATH,
|
||||
maxBytes=5 * 1024 * 1024,
|
||||
backupCount=3,
|
||||
|
||||
Reference in new issue
Block a user