Skip to content

Commit

Permalink
test: logging
Browse files Browse the repository at this point in the history
  • Loading branch information
meakbiyik committed Aug 28, 2023
1 parent 8af8478 commit ee5e138
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions tests/test_logging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"""Tests for the logging module."""
import logging
import os
import sys

from torchcache.logging import set_logger_config


def test_set_logger_config() -> None:
"""Test the set_logger_config function."""
set_logger_config()
assert logging.getLogger().level == logging.WARN
assert len(logging.getLogger().handlers) == 1
assert isinstance(logging.getLogger().handlers[0], logging.StreamHandler)
assert logging.getLogger().handlers[0].stream == sys.stdout
assert (
logging.getLogger().handlers[0].formatter._fmt
== "[torchcache] - %(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
assert logging.getLogger().handlers[0].formatter.datefmt == "%Y-%m-%d %H:%M:%S"
# Also test various environment variable configurations
os.environ["TORCHCACHE_LOG_LEVEL"] = "DEBUG"
os.environ[
"TORCHCACHE_LOG_FMT"
] = "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
os.environ["TORCHCACHE_LOG_DATEFMT"] = "%Y-%m-%d %H:%M:%S"
os.environ["TORCHCACHE_LOG_FILE"] = "test.log"
set_logger_config()
assert logging.getLogger().level == logging.DEBUG
assert len(logging.getLogger().handlers) == 2
assert isinstance(logging.getLogger().handlers[0], logging.StreamHandler)
assert logging.getLogger().handlers[0].stream == sys.stdout
assert (
logging.getLogger().handlers[0].formatter._fmt
== "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
assert logging.getLogger().handlers[0].formatter.datefmt == "%Y-%m-%d %H:%M:%S"
assert isinstance(logging.getLogger().handlers[1], logging.FileHandler)
assert (
logging.getLogger().handlers[1].formatter._fmt
== "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
assert logging.getLogger().handlers[1].formatter.datefmt == "%Y-%m-%d %H:%M:%S"
assert logging.getLogger().handlers[1].baseFilename.endswith("test.log")
# Test invalid log level
os.environ["TORCHCACHE_LOG_LEVEL"] = "INVALID"
set_logger_config()
assert logging.getLogger().level == logging.WARN

0 comments on commit ee5e138

Please sign in to comment.