Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow using custom Logger #439

Merged
merged 6 commits into from
Oct 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,26 @@ from webdriver_manager.chrome import ChromeDriverManager
ChromeDriverManager("2.26", cache_valid_range=1).install()
```

---

### Custom Logger

If you need to use a custom logger, you can create a logger and set it with `set_logger()`.

```python
import logging
from webdriver_manager.core.logger import set_logger

logger = logging.getLogger("custom_logger")
logger.setLevel(logging.DEBUG)
logger.addHandler(logging.StreamHandler())
logger.addHandler(logging.FileHandler("custom.log"))

set_logger(logger)
```

---

### Custom HTTP Client
If you need to add custom HTTP logic like session or proxy you can define your custom HttpClient implementation.

Expand Down Expand Up @@ -298,6 +318,8 @@ def test_can_get_chrome_driver_with_custom_http_client():
assert os.path.exists(path)
```

---

This will make your test automation more elegant and robust!

Cheers
41 changes: 41 additions & 0 deletions tests/test_custom_logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"""tests.test_custom_logger.py"""

import logging

import pytest

from webdriver_manager.core.logger import log, set_logger, __logger

# Cache the old logger to restore it later
__old_logger = __logger


@pytest.fixture
def create_logger():
"""Create a logger."""

# Create a Custom Debug Logger
logger = logging.getLogger("WDM-DEBUG")
logger.setLevel(logging.DEBUG)
logger.addHandler(logging.StreamHandler())

return logger


def test_custom_logger(capsys, create_logger):
"""Test the custom logger."""

# Set the custom logger
set_logger(create_logger)

# send a log message
log_msg = "This is a test log message from the custom logger"
log(log_msg)

# Check if the log message is in the output
captured = capsys.readouterr()
assert log_msg in captured.err
capsys.close()

# Restore the old logger
set_logger(__old_logger)
21 changes: 21 additions & 0 deletions webdriver_manager/core/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,24 @@
def log(text):
"""Emitting the log message."""
__logger.log(wdm_log_level(), text)


def set_logger(logger):
"""
Set the global logger.

Parameters
----------
logger : logging.Logger
The custom logger to use.

Returns None
"""

# Check if the logger is a valid logger
if not isinstance(logger, logging.Logger):
raise ValueError("The logger must be an instance of logging.Logger")

# Bind the logger input to the global logger
global __logger
__logger = logger