Skip to content
Open
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
18 changes: 18 additions & 0 deletions colorlog/tests/test_wrappers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Test the colorlog.wrappers module."""

import io
import logging

import colorlog
Expand Down Expand Up @@ -41,3 +42,20 @@ def test_wrappers():
assert colorlog.critical is not logging.critical
assert colorlog.log is not logging.log
assert colorlog.exception is not logging.exception


def test_basicConfig_with_no_handlers(monkeypatch):
monkeypatch.setattr(logging.root, "handlers", [])
colorlog.basicConfig(handlers=[])
assert logging.root.handlers == []


def test_basicConfig_with_supplied_handler(monkeypatch):
monkeypatch.setattr(logging.root, "handlers", [])
stream = io.StringIO()
handler = logging.StreamHandler(stream)
colorlog.basicConfig(handlers=[handler], format="%(message)s", reset=False)
assert logging.root.handlers == [handler]
assert isinstance(handler.formatter, colorlog.ColoredFormatter)
logging.root.warning("configured message")
assert stream.getvalue() == "configured message\n"
2 changes: 2 additions & 0 deletions colorlog/wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ def basicConfig(
logging.basicConfig(**kwargs)

def _basicConfig():
if not logging.root.handlers:
return
handler = logging.root.handlers[0]
handler.setFormatter(
colorlog.formatter.ColoredFormatter(
Expand Down