Skip to content

Commit 54714a8

Browse files
authored
feat: setting CHATLAS_LOG now enables other relevant, provider specific, logs (#97)
* feat: setting CHATLAS_LOG will enable other relevant, provider specific, logs * Update changelog
1 parent 17e88f5 commit 54714a8

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1515

1616
### Improvements
1717

18+
* The `CHATLAS_LOG` environment variable will now enable logs for the relevant model provider. It now also supports a lovel of `debug` in addition to `info`. (#97)
1819
* `Chat` instances can now be deep copied, which is useful for forking the chat session. (#96)
1920

2021
### Bug fixes

chatlas/_logging.py

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import logging
22
import os
33
import warnings
4+
from typing import Literal
45

56
from rich.logging import RichHandler
67

@@ -12,15 +13,38 @@ def _rich_handler() -> RichHandler:
1213
return handler
1314

1415

15-
logger = logging.getLogger("chatlas")
16-
17-
if os.environ.get("CHATLAS_LOG") == "info":
16+
def setup_logger(x: str, level: Literal["debug", "info"]) -> logging.Logger:
17+
logger = logging.getLogger(x)
18+
if level == "debug":
19+
logger.setLevel(logging.DEBUG)
20+
elif level == "info":
21+
logger.setLevel(logging.INFO)
1822
# By adding a RichHandler to chatlas' logger, we can guarantee that they
1923
# never get dropped, even if the root logger's handlers are not
2024
# RichHandlers.
21-
logger.setLevel(logging.INFO)
22-
logger.addHandler(_rich_handler())
25+
if not any(isinstance(h, RichHandler) for h in logger.handlers):
26+
logger.addHandler(_rich_handler())
2327
logger.propagate = False
28+
return logger
29+
30+
31+
logger = logging.getLogger("chatlas")
32+
log_level = os.environ.get("CHATLAS_LOG")
33+
if log_level:
34+
if log_level != "debug" and log_level != "info":
35+
warnings.warn(
36+
f"CHATLAS_LOG is set to '{log_level}', but the log level must "
37+
"be one of 'debug' or 'info'. Defaulting to 'info'.",
38+
)
39+
log_level = "info"
40+
41+
# Manually setup the logger for each dependency we care about. This way, we
42+
# can ensure that the logs won't get dropped when a rich display is activate
43+
logger = setup_logger("chatlas", log_level)
44+
openai_logger = setup_logger("openai", log_level)
45+
anthropic_logger = setup_logger("anthropic", log_level)
46+
google_logger = setup_logger("google_genai.models", log_level)
47+
httpx_logger = setup_logger("httpx", log_level)
2448

2549
# Add a RichHandler to the root logger if there are no handlers. Note that
2650
# if chatlas is imported before other libraries that set up logging, (like

0 commit comments

Comments
 (0)