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

Revise the logger instanciation/initial handlers #135

Merged
merged 17 commits into from Nov 24, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 25 additions & 0 deletions charset_normalizer/__init__.py
Expand Up @@ -19,6 +19,8 @@
:copyright: (c) 2021 by Ahmed TAHRI
:license: MIT, see LICENSE for more details.
"""
import logging

from .api import from_bytes, from_fp, from_path, normalize
from .legacy import (
CharsetDetector,
Expand All @@ -45,3 +47,26 @@
"__version__",
"VERSION",
)


def set_logging_handler(
nmaynes marked this conversation as resolved.
Show resolved Hide resolved
name: str = "charset_normalizer",
level: int = logging.INFO,
format_string: str = None,
nmaynes marked this conversation as resolved.
Show resolved Hide resolved
) -> None:

if format_string is None:
format_string = "%(asctime)s | %(levelname)s | %(message)s"
logger = logging.getLogger(name)
logger.setLevel(level)

handler = logging.StreamHandler()
handler.setFormatter(logging.Formatter(format_string))
logger.addHandler(handler)


# Attach a NullHandler to the top level logger by default
# https://docs.python.org/3.3/howto/logging.html#configuring-logging-for-a-library


logging.getLogger(__name__).addHandler(logging.NullHandler())
14 changes: 5 additions & 9 deletions charset_normalizer/api.py
@@ -1,3 +1,4 @@
import logging
from os.path import basename, splitext
from typing import BinaryIO, List, Optional, Set

Expand All @@ -6,7 +7,7 @@
except ImportError: # pragma: no cover
PathLike = str # type: ignore

import logging
import charset_normalizer
nmaynes marked this conversation as resolved.
Show resolved Hide resolved

from .cd import (
coherence_ratio,
Expand All @@ -26,12 +27,7 @@
should_strip_sig_or_bom,
)

logger = logging.getLogger("charset_normalizer")
logger.setLevel(logging.DEBUG)

handler = logging.StreamHandler()
handler.setFormatter(logging.Formatter("%(asctime)s | %(levelname)s | %(message)s"))
logger.addHandler(handler)
logger = logging.getLogger(__name__)


def from_bytes(
Expand Down Expand Up @@ -67,9 +63,9 @@ def from_bytes(
)

if not explain:
logger.setLevel(logging.CRITICAL)
charset_normalizer.set_logging_handler(level=logging.CRITICAL)
Ousret marked this conversation as resolved.
Show resolved Hide resolved
else:
logger.setLevel(logging.INFO)
charset_normalizer.set_logging_handler(level=logging.INFO)

length = len(sequences) # type: int

Expand Down
30 changes: 30 additions & 0 deletions tests/test_logging.py
@@ -0,0 +1,30 @@
import pytest
import logging

import charset_normalizer

logger = logging.getLogger("charset_normalizer")


class TestLogBehaviorClass:
def test_set_stream_handler(self, caplog):
charset_normalizer.set_logging_handler(
"charset_normalizer", level=logging.DEBUG
)
logger.debug("log content should log with default format")
for record in caplog.records:
assert record.levelname == "DEBUG"
assert "log content should log with default format" in caplog.text

def test_set_stream_handler_format(self, caplog):
charset_normalizer.set_logging_handler(
"charset_normalizer", format_string="%(message)s"
)
logger.info("log content should only be this message")
assert caplog.record_tuples == [
(
"charset_normalizer",
logging.INFO,
"log content should only be this message",
)
]
nmaynes marked this conversation as resolved.
Show resolved Hide resolved