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

Fix duplicate logging #19

Merged
merged 2 commits into from
May 15, 2024
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
4 changes: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"type": "debugpy",
"request": "launch",
"module": "uvicorn",
"justMyCode": true,
"args": ["tad.main:app"],
"justMyCode": false,
"args": [ "--log-level", "warning" ,"tad.main:app"],
"cwd": "${workspaceFolder}/",
"env": {
"PYTHONPATH": "${workspaceFolder}"
Expand Down
2 changes: 1 addition & 1 deletion sonar-project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ sonar.python.version=3.10,3.11,3.12

sonar.python.coverage.reportPaths=coverage.xml

sonar.coverage.exclusions=tad/migrations/
sonar.coverage.exclusions=tad/migrations/*
2 changes: 1 addition & 1 deletion tad/core/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
},
},
"loggers": {
"tad": {"handlers": ["console", "file"], "level": "DEBUG", "propagate": True},
"tad": {"handlers": ["console", "file"], "level": "DEBUG", "propagate": False},
},
}

Expand Down
45 changes: 30 additions & 15 deletions tests/core/test_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
from tad.core.log import configure_logging


def test_module_logging_setup(caplog: pytest.LogCaptureFixture):
configure_logging()
def test_logging_tad_module(caplog: pytest.LogCaptureFixture):
config = {"loggers": {"tad": {"propagate": True}}}

configure_logging(config=config)

logger = logging.getLogger("tad")

Expand All @@ -22,7 +24,7 @@ def test_module_logging_setup(caplog: pytest.LogCaptureFixture):
assert caplog.records[0].message == message


def test_root_logging_setup(caplog: pytest.LogCaptureFixture):
def test_logging_root(caplog: pytest.LogCaptureFixture):
configure_logging()

logger = logging.getLogger("")
Expand All @@ -38,8 +40,10 @@ def test_root_logging_setup(caplog: pytest.LogCaptureFixture):
assert caplog.records[0].message == message


def test_module_main_logging_setup(caplog: pytest.LogCaptureFixture):
configure_logging()
def test_logging_submodule(caplog: pytest.LogCaptureFixture):
config = {"loggers": {"tad": {"propagate": True}}}

configure_logging(config=config)

logger = logging.getLogger("tad.main")

Expand All @@ -54,35 +58,46 @@ def test_module_main_logging_setup(caplog: pytest.LogCaptureFixture):
assert caplog.records[0].message == message


def test_module_main_logging_with_custom_logging_setup(caplog: pytest.LogCaptureFixture):
configure_logging(level="ERROR")
def test_logging_config(caplog: pytest.LogCaptureFixture, monkeypatch: pytest.MonkeyPatch):
monkeypatch.setenv(
"LOGGING_CONFIG",
'{"loggers": { "tad": { "propagate": "True" }},"formatters": { "generic": { "fmt": "{name}: {message}"}}}',
)

logger = logging.getLogger("tad.main")
settings = Settings() # type: ignore

message = "This is a test log message"
configure_logging(config=settings.LOGGING_CONFIG)

logger = logging.getLogger("tad")

message = "This is a test log message with other formatting"
logger.debug(message)
logger.info(message)
logger.warning(message)
logger.error(message)
logger.critical(message)

assert len(caplog.records) == 2
assert len(caplog.records) == 4


def test_logging_loglevel(caplog: pytest.LogCaptureFixture):
config = {"loggers": {"tad": {"propagate": True}}}

def test_enviroment_setup(caplog: pytest.LogCaptureFixture):
os.environ["LOGGING_CONFIG"] = '{"formatters": { "generic": { "fmt": "{name}: {message}"}}}'
configure_logging(config=config)

os.environ["LOGGING_LEVEL"] = "ERROR"

settings = Settings() # type: ignore

configure_logging(config=settings.LOGGING_CONFIG)
configure_logging(config=config, level=settings.LOGGING_LEVEL)

logger = logging.getLogger("tad.main")

message = "This is a test log message with other formatting"
message = "This is a test log message with different logging level"
logger.debug(message)
logger.info(message)
logger.warning(message)
logger.error(message)
logger.critical(message)

assert len(caplog.records) == 4
assert len(caplog.records) == 2
Loading