forked from VRAutomatize/browser-use
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogging_config.py
More file actions
118 lines (94 loc) · 3.99 KB
/
logging_config.py
File metadata and controls
118 lines (94 loc) · 3.99 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
import logging
import os
from logging.handlers import RotatingFileHandler
from datetime import datetime
import json
import traceback
from typing import Any, Dict
class CustomFormatter(logging.Formatter):
"""Formatação personalizada para logs"""
def format(self, record):
# Adiciona timestamp em ISO format
record.iso_timestamp = datetime.utcnow().isoformat()
# Adiciona traceback se houver
if record.exc_info:
record.traceback = traceback.format_exc()
else:
record.traceback = None
# Formata a mensagem como JSON
log_data = {
"timestamp": record.iso_timestamp,
"level": record.levelname,
"module": record.module,
"function": record.funcName,
"line": record.lineno,
"message": record.getMessage(),
"traceback": record.traceback
}
# Adiciona contexto extra se existir
if hasattr(record, 'context'):
log_data['context'] = record.context
return json.dumps(log_data, ensure_ascii=False)
def setup_logging():
"""Configura o sistema de logging"""
# Cria diretório de logs se não existir
log_dir = os.getenv('LOG_DIR', '/var/log/browser-use')
os.makedirs(log_dir, exist_ok=True)
# Configura o logger principal
logger = logging.getLogger('browser-use')
logger.setLevel(logging.DEBUG)
# Remove handlers existentes
logger.handlers = []
# Configura handler para arquivo
log_file = os.path.join(log_dir, 'app.log')
file_handler = RotatingFileHandler(
log_file,
maxBytes=10*1024*1024, # 10MB
backupCount=5,
encoding='utf-8'
)
file_handler.setLevel(logging.DEBUG)
file_handler.setFormatter(CustomFormatter())
# Configura handler para console
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.INFO)
console_handler.setFormatter(CustomFormatter())
# Adiciona handlers ao logger
logger.addHandler(file_handler)
logger.addHandler(console_handler)
# Configura loggers específicos
setup_module_logger('database', log_dir)
setup_module_logger('browser', log_dir)
setup_module_logger('api', log_dir)
setup_module_logger('server', log_dir)
return logger
def setup_module_logger(module_name: str, log_dir: str):
"""Configura logger específico para um módulo"""
logger = logging.getLogger(f'browser-use.{module_name}')
logger.setLevel(logging.DEBUG)
# Handler específico para o módulo
log_file = os.path.join(log_dir, f'{module_name}.log')
handler = RotatingFileHandler(
log_file,
maxBytes=5*1024*1024, # 5MB
backupCount=3,
encoding='utf-8'
)
handler.setLevel(logging.DEBUG)
handler.setFormatter(CustomFormatter())
logger.addHandler(handler)
def log_with_context(logger: logging.Logger, level: int, msg: str, context: Dict[str, Any] = None, exc_info=None):
"""Função auxiliar para log com contexto"""
extra = {'context': context} if context else {}
logger.log(level, msg, exc_info=exc_info, extra=extra)
# Funções de conveniência para diferentes níveis de log
def log_debug(logger: logging.Logger, msg: str, context: Dict[str, Any] = None):
log_with_context(logger, logging.DEBUG, msg, context)
def log_info(logger: logging.Logger, msg: str, context: Dict[str, Any] = None):
log_with_context(logger, logging.INFO, msg, context)
def log_warning(logger: logging.Logger, msg: str, context: Dict[str, Any] = None):
log_with_context(logger, logging.WARNING, msg, context)
def log_error(logger: logging.Logger, msg: str, context: Dict[str, Any] = None, exc_info=None):
log_with_context(logger, logging.ERROR, msg, context, exc_info)
def log_critical(logger: logging.Logger, msg: str, context: Dict[str, Any] = None, exc_info=None):
log_with_context(logger, logging.CRITICAL, msg, context, exc_info)