Skip to content

Commit

Permalink
Read the logger logging level from environment variable (#874)
Browse files Browse the repository at this point in the history
* read logging level from env

* fix lint
  • Loading branch information
misko authored Oct 23, 2024
1 parent da93cfe commit 7e805d4
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions src/fairchem/core/common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import copy
import datetime
import errno
import functools
import importlib
import itertools
import json
Expand Down Expand Up @@ -956,21 +957,33 @@ def filter(self, record) -> bool:
return self.min_level <= record.levelno < self.max_level


def debug_log_entry_exit(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
logging.debug(f"{func.__name__}...")
result = func(*args, **kwargs)
logging.debug(f"{func.__name__} done")
return result

return wrapper


def setup_logging() -> None:
root = logging.getLogger()

# Perform setup only if logging has not been configured
target_logging_level = getattr(logging, os.environ.get("LOGLEVEL", "INFO").upper())
root.setLevel(target_logging_level)
if not root.hasHandlers():
root.setLevel(logging.INFO)

log_formatter = logging.Formatter(
"%(asctime)s (%(levelname)s): %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)

# Send INFO to stdout
# Send INFO (or target) to stdout
handler_out = logging.StreamHandler(sys.stdout)
handler_out.addFilter(SeverityLevelBetween(logging.INFO, logging.WARNING))
handler_out.addFilter(
SeverityLevelBetween(target_logging_level, logging.WARNING)
)
handler_out.setFormatter(log_formatter)
root.addHandler(handler_out)

Expand Down

0 comments on commit 7e805d4

Please sign in to comment.