-
Notifications
You must be signed in to change notification settings - Fork 2
/
logger_config.py
39 lines (31 loc) · 1.12 KB
/
logger_config.py
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
import logging
import sys
import os
from datetime import datetime
def setup_logger():
# Crear directorio para logs si no existe
log_dir = 'logs'
if not os.path.exists(log_dir):
os.makedirs(log_dir)
# Crear nombre de archivo con timestamp
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
log_file = os.path.join(log_dir, f'app_{timestamp}.log')
# Configurar logger principal
logger = logging.getLogger('GestionExpediente')
logger.setLevel(logging.DEBUG)
# Formato detallado para los logs
formatter = logging.Formatter(
'%(asctime)s - %(name)s - %(levelname)s - %(filename)s:%(lineno)d - %(message)s'
)
# Handler para archivo
file_handler = logging.FileHandler(log_file, encoding='utf-8')
file_handler.setLevel(logging.DEBUG)
file_handler.setFormatter(formatter)
# Handler para consola
console_handler = logging.StreamHandler(sys.stdout)
console_handler.setLevel(logging.INFO)
console_handler.setFormatter(formatter)
# Agregar handlers al logger
logger.addHandler(file_handler)
logger.addHandler(console_handler)
return logger