GaetanoParente commited on
Commit
07a01b5
·
1 Parent(s): 0c6fe9c

fix logger

Browse files
Files changed (1) hide show
  1. modules/utilities/logger.py +13 -13
modules/utilities/logger.py CHANGED
@@ -11,33 +11,31 @@ import gradio as gr
11
  DATASET_REPO_ID = "NextGenTech/ngt-ai-platform-logs"
12
  LOG_DIR = Path("data/logs")
13
  LOG_FILE = LOG_DIR / "access_logs.csv"
14
- TIME = 5 #intervallo di scrittura dei log sul dataset
15
 
16
  LOG_DIR.mkdir(parents=True, exist_ok=True)
17
 
18
- if not LOG_FILE.exists():
 
19
  with open(LOG_FILE, "w", newline="", encoding="utf-8") as f:
20
- writer = csv.writer(f)
21
  writer.writerow([
22
  "timestamp", "session_id", "module", "action",
23
  "ip_address", "user_agent", "language", "input_size", "processing_time"
24
  ])
25
 
 
26
  scheduler = CommitScheduler(
27
  repo_id=DATASET_REPO_ID,
28
  repo_type="dataset",
29
  folder_path=LOG_DIR,
30
  path_in_repo="logs",
31
- every=TIME,
32
- token=os.environ.get("HF_TOKEN_WRITE")
33
  )
34
 
35
  def log_interaction(request: gr.Request, module_name: str, action: str, input_data=None, execution_time=0.0):
36
- """
37
- Registra un evento di analytics in modo invisibile.
38
- """
39
  try:
40
-
41
  if request:
42
  headers = request.headers
43
  ip = headers.get("x-forwarded-for", request.client.host)
@@ -52,25 +50,27 @@ def log_interaction(request: gr.Request, module_name: str, action: str, input_da
52
  input_meta = "0"
53
  if isinstance(input_data, str):
54
  input_meta = f"{len(input_data)} chars"
55
- elif hasattr(input_data, 'shape'): # Immagini numpy
56
  input_meta = f"{input_data.shape}"
57
  elif input_data is not None:
58
  input_meta = "Binary/File"
59
 
60
  with scheduler.lock:
61
  with open(LOG_FILE, "a", newline="", encoding="utf-8") as f:
62
- writer = csv.writer(f)
63
  writer.writerow([
64
  datetime.now().isoformat(),
65
  session_id,
66
  module_name,
67
  action,
68
- ip,
69
  user_agent,
70
  language,
71
  input_meta,
72
  f"{execution_time:.4f}s"
73
  ])
74
 
 
 
75
  except Exception as e:
76
- print(f"⚠️ Errore durante il logging: {e}")
 
11
  DATASET_REPO_ID = "NextGenTech/ngt-ai-platform-logs"
12
  LOG_DIR = Path("data/logs")
13
  LOG_FILE = LOG_DIR / "access_logs.csv"
14
+ TIME = 5 #minuti di frequenza di caricamento dei log nel dataset
15
 
16
  LOG_DIR.mkdir(parents=True, exist_ok=True)
17
 
18
+ if not LOG_FILE.exists() or LOG_FILE.stat().st_size == 0:
19
+ print("🆕 Creazione nuovo file di log con intestazioni...")
20
  with open(LOG_FILE, "w", newline="", encoding="utf-8") as f:
21
+ writer = csv.writer(f, lineterminator='\n')
22
  writer.writerow([
23
  "timestamp", "session_id", "module", "action",
24
  "ip_address", "user_agent", "language", "input_size", "processing_time"
25
  ])
26
 
27
+ # Configura lo Scheduler
28
  scheduler = CommitScheduler(
29
  repo_id=DATASET_REPO_ID,
30
  repo_type="dataset",
31
  folder_path=LOG_DIR,
32
  path_in_repo="logs",
33
+ every=1,
34
+ token=os.environ.get("HF_TOKEN")
35
  )
36
 
37
  def log_interaction(request: gr.Request, module_name: str, action: str, input_data=None, execution_time=0.0):
 
 
 
38
  try:
 
39
  if request:
40
  headers = request.headers
41
  ip = headers.get("x-forwarded-for", request.client.host)
 
50
  input_meta = "0"
51
  if isinstance(input_data, str):
52
  input_meta = f"{len(input_data)} chars"
53
+ elif hasattr(input_data, 'shape'):
54
  input_meta = f"{input_data.shape}"
55
  elif input_data is not None:
56
  input_meta = "Binary/File"
57
 
58
  with scheduler.lock:
59
  with open(LOG_FILE, "a", newline="", encoding="utf-8") as f:
60
+ writer = csv.writer(f, lineterminator='\n')
61
  writer.writerow([
62
  datetime.now().isoformat(),
63
  session_id,
64
  module_name,
65
  action,
66
+ ip,
67
  user_agent,
68
  language,
69
  input_meta,
70
  f"{execution_time:.4f}s"
71
  ])
72
 
73
+ print(f"✅ LOG SCRITTO: {module_name} (Size: {LOG_FILE.stat().st_size})")
74
+
75
  except Exception as e:
76
+ print(f" ERRORE LOGGING: {e}")