Skip to content

Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.

safe info and debug logs to files

Magnus Müller146b7ff423ff
diff --git a/.env.example b/.env.example
index 3fe667fd6..ef0fe4736 100644
--- a/.env.example
+++ b/.env.example
@@ -1,64 +1,51 @@
-# Browser Use Environment Configuration
-# Copy this file to .env and configure your API keys and settings
+# Browser Use Configuration
+# Copy this file to .env and fill in your values
-# =============================================================================
-# API Keys for Language Models
-# =============================================================================
-OPENAI_API_KEY=
-ANTHROPIC_API_KEY=
-GOOGLE_API_KEY=
-DEEPSEEK_API_KEY=
-GROK_API_KEY=
-NOVITA_API_KEY=
-
-# Azure OpenAI Configuration
-AZURE_OPENAI_ENDPOINT=
-AZURE_OPENAI_KEY=
-
-# =============================================================================
# Logging Configuration
-# =============================================================================
-# Browser Use logging level (debug, info, warning, error)
+# Set the logging level (debug, info, warning, error)
BROWSER_USE_LOGGING_LEVEL=info
-# CDP (Chrome DevTools Protocol) logging level for cdp_use library
-# Controls logging verbosity of Chrome DevTools Protocol interactions
-# Recommended: WARNING to reduce noise (debug, info, warning, error)
+# Log file paths (optional)
+# Save debug level logs to this file
+BROWSER_USE_DEBUG_LOG_FILE=debug.log
+
+# Save info level logs to this file
+BROWSER_USE_INFO_LOG_FILE=info.log
+
+# CDP (Chrome DevTools Protocol) logging level
CDP_LOGGING_LEVEL=WARNING
-# =============================================================================
-# Telemetry and Cloud Configuration
-# =============================================================================
-# Enable anonymous telemetry collection
+# Telemetry and Analytics
+# Enable/disable anonymous telemetry
ANONYMIZED_TELEMETRY=true
-# Browser Use Cloud Configuration
-BROWSER_USE_CLOUD_SYNC=
-BROWSER_USE_CLOUD_API_URL=https://api.browser-use.com
-BROWSER_USE_CLOUD_UI_URL=
+# Browser Use Cloud Configuration (optional)
+# Your Browser Use Cloud API key - get it from: https://cloud.browser-use.com/billing
+# BROWSER_USE_API_KEY=your_api_key_here
+
+# Custom API base URL (for enterprise installations)
+# BROWSER_USE_CLOUD_API_URL=https://api.browser-use.com
+
+# Cloud sync settings
+# BROWSER_USE_CLOUD_SYNC=false
-# =============================================================================
-# Development and Runtime Configuration
-# =============================================================================
-# Skip LLM API key verification during initialization
-SKIP_LLM_API_KEY_VERIFICATION=false
+# Model Configuration
+# Default LLM model to use
+# OPENAI_API_KEY=your_openai_api_key_here
+# ANTHROPIC_API_KEY=your_anthropic_api_key_here
-# Runtime environment flags
-IN_DOCKER=
-IS_IN_EVALS=false
+# Browser Configuration
+# Path to Chrome/Chromium executable (optional)
+# BROWSER_USE_EXECUTABLE_PATH=/path/to/chrome
-# Path configuration
-XDG_CACHE_HOME=~/.cache
-XDG_CONFIG_HOME=~/.config
-BROWSER_USE_CONFIG_DIR=
+# Run browser in headless mode
+# BROWSER_USE_HEADLESS=false
-# Windows font directory (Windows only)
-WIN_FONT_DIR=C:\Windows\Fonts
+# User data directory for browser profile
+# BROWSER_USE_USER_DATA_DIR=./browser_data
-# =============================================================================
-# MCP (Model Context Protocol) Configuration
-# =============================================================================
-BROWSER_USE_CONFIG_PATH=
-BROWSER_USE_HEADLESS=
-BROWSER_USE_ALLOWED_DOMAINS=
-BROWSER_USE_LLM_MODEL=
+# Proxy Configuration (optional)
+# BROWSER_USE_PROXY_SERVER=http://proxy.example.com:8080
+# BROWSER_USE_NO_PROXY=localhost,127.0.0.1,*.internal
+# BROWSER_USE_PROXY_USERNAME=username
+# BROWSER_USE_PROXY_PASSWORD=password
diff --git a/browser_use/__init__.py b/browser_use/__init__.py
index 848e4f8e9..2a3c6dbac 100644
--- a/browser_use/__init__.py
+++ b/browser_use/__init__.py
@@ -5,7 +5,14 @@ from browser_use.logging_config import setup_logging
# Only set up logging if not in MCP mode or if explicitly requested
if os.environ.get('BROWSER_USE_SETUP_LOGGING', 'true').lower() != 'false':
- logger = setup_logging()
+ from browser_use.config import CONFIG
+
+ # Get log file paths from config/environment
+ debug_log_file = getattr(CONFIG, 'BROWSER_USE_DEBUG_LOG_FILE', None)
+ info_log_file = getattr(CONFIG, 'BROWSER_USE_INFO_LOG_FILE', None)
+
+ # Set up logging with file handlers if specified
+ logger = setup_logging(debug_log_file=debug_log_file, info_log_file=info_log_file)
else:
import logging
diff --git a/browser_use/config.py b/browser_use/config.py
index 3a2d75292..4114ab93b 100644
--- a/browser_use/config.py
+++ b/browser_use/config.py
@@ -181,6 +181,8 @@ class FlatEnvConfig(BaseSettings):
# Logging and telemetry
BROWSER_USE_LOGGING_LEVEL: str = Field(default='info')
CDP_LOGGING_LEVEL: str = Field(default='WARNING')
+ BROWSER_USE_DEBUG_LOG_FILE: str | None = Field(default=None)
+ BROWSER_USE_INFO_LOG_FILE: str | None = Field(default=None)
ANONYMIZED_TELEMETRY: bool = Field(default=True)
BROWSER_USE_CLOUD_SYNC: bool | None = Field(default=None)
BROWSER_USE_CLOUD_API_URL: str = Field(default='https://api.browser-use.com')
diff --git a/browser_use/logging_config.py b/browser_use/logging_config.py
index 34bb5bacf..dc3658c5d 100644
--- a/browser_use/logging_config.py
+++ b/browser_use/logging_config.py
@@ -61,13 +61,15 @@ def addLoggingLevel(levelName, levelNum, methodName=None):
setattr(logging, methodName, logToRoot)
-def setup_logging(stream=None, log_level=None, force_setup=False):
+def setup_logging(stream=None, log_level=None, force_setup=False, debug_log_file=None, info_log_file=None):
"""Setup logging configuration for browser-use.
Args:
stream: Output stream for logs (default: sys.stdout). Can be sys.stderr for MCP mode.
log_level: Override log level (default: uses CONFIG.BROWSER_USE_LOGGING_LEVEL)
force_setup: Force reconfiguration even if handlers already exist
+ debug_log_file: Path to log file for debug level logs only
+ info_log_file: Path to log file for info level logs only
"""
# Try to add RESULT level, but ignore if it already exists
try:
@@ -125,25 +127,50 @@ def setup_logging(stream=None, log_level=None, force_setup=False):
console.setLevel('RESULT')
console.setFormatter(BrowserUseFormatter('%(message)s', log_level))
else:
+ console.setLevel(log_level) # Keep console at original log level (e.g., INFO)
console.setFormatter(BrowserUseFormatter('%(levelname)-8s [%(name)s] %(message)s', log_level))
# Configure root logger only
root.addHandler(console)
- # Configure root logger
- root.setLevel(log_level)
+ # Add file handlers if specified
+ file_handlers = []
+
+ # Create debug log file handler
+ if debug_log_file:
+ debug_handler = logging.FileHandler(debug_log_file)
+ debug_handler.setLevel(logging.DEBUG)
+ debug_handler.setFormatter(BrowserUseFormatter('%(asctime)s - %(levelname)-8s [%(name)s] %(message)s', logging.DEBUG))
+ file_handlers.append(debug_handler)
+ root.addHandler(debug_handler)
+
+ # Create info log file handler
+ if info_log_file:
+ info_handler = logging.FileHandler(info_log_file)
+ info_handler.setLevel(logging.INFO)
+ info_handler.setFormatter(BrowserUseFormatter('%(asctime)s - %(levelname)-8s [%(name)s] %(message)s', logging.INFO))
+ file_handlers.append(info_handler)
+ root.addHandler(info_handler)
+
+ # Configure root logger - use DEBUG if debug file logging is enabled
+ effective_log_level = logging.DEBUG if debug_log_file else log_level
+ root.setLevel(effective_log_level)
# Configure browser_use logger
browser_use_logger = logging.getLogger('browser_use')
browser_use_logger.propagate = False # Don't propagate to root logger
browser_use_logger.addHandler(console)
- browser_use_logger.setLevel(log_level)
+ for handler in file_handlers:
+ browser_use_logger.addHandler(handler)
+ browser_use_logger.setLevel(effective_log_level)
# Configure bubus logger to allow INFO level logs
bubus_logger = logging.getLogger('bubus')
bubus_logger.propagate = False # Don't propagate to root logger
bubus_logger.addHandler(console)
- bubus_logger.setLevel(logging.INFO if log_type == 'result' else log_level)
+ for handler in file_handlers:
+ bubus_logger.addHandler(handler)
+ bubus_logger.setLevel(logging.INFO if log_type == 'result' else effective_log_level)
# Configure CDP logging using cdp_use's setup function
# This enables the formatted CDP output using CDP_LOGGING_LEVEL environment variable
diff --git a/examples/simple_with_logs.py b/examples/simple_with_logs.py
new file mode 100644
index 000000000..cd4266cb0
--- /dev/null
+++ b/examples/simple_with_logs.py
@@ -0,0 +1,7 @@
+from browser_use import Agent
+from browser_use.logging_config import setup_logging
+
+# Set up logging to files
+setup_logging(debug_log_file='debug.log', info_log_file='info.log')
+
+Agent('Find the founders of browser-use').run_sync()