forked from langchain-ai/langchain-aws
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added `logger_util` to enable package and class wide logging in `langchain-aws`. Added logging for `invoke` and `ainvoke`.
- Loading branch information
Vishal Patil
committed
Jan 15, 2025
1 parent
94cb00b
commit 61356da
Showing
3 changed files
with
80 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import logging | ||
import os | ||
import sys | ||
|
||
__DEBUG = True if os.environ.get('LANGCHAIN_AWS_DEBUG') else False | ||
__ROOT_DEBUG = __DEBUG if os.environ.get('LANGCHAIN_AWS_DEBUG_ROOT') else False | ||
|
||
if __DEBUG: | ||
DEFAULT_LOG_LEVEL: int = logging.DEBUG | ||
else: | ||
DEFAULT_LOG_LEVEL: int = logging.ERROR | ||
|
||
DEFAULT_LOG_FILE = os.environ.get('LANGCHAIN_AWS_LOG_OUTPUT', '-') | ||
if DEFAULT_LOG_FILE == '-': | ||
DEFAULT_LOG_HANDLER: logging.Handler = None | ||
else: | ||
DEFAULT_LOG_HANDLER: logging.Handler = logging.FileHandler(DEFAULT_LOG_FILE) | ||
|
||
if __DEBUG: | ||
DEFAULT_LOG_FORMAT: str = '%(asctime)s %(levelname)s | [%(filename)s:%(lineno)s] | - %(name)s - %(message)s' | ||
else: | ||
DEFAULT_LOG_FORMAT: str = '%(asctime)s %(levelname)s | %(name)s - %(message)s' | ||
|
||
try: | ||
import colorama | ||
import coloredlogs | ||
|
||
colorama.init() | ||
|
||
if DEFAULT_LOG_HANDLER: | ||
DEFAULT_LOG_FORMATTER: logging.Formatter = logging.Formatter(DEFAULT_LOG_FORMAT) | ||
else: | ||
DEFAULT_LOG_FORMATTER: logging.Formatter = coloredlogs.ColoredFormatter(DEFAULT_LOG_FORMAT) | ||
|
||
except ImportError: | ||
colorama = None | ||
coloredlogs = None | ||
|
||
DEFAULT_LOG_FORMATTER: logging.Formatter = logging.Formatter(DEFAULT_LOG_FORMAT) | ||
|
||
def get_logger(logger_name: str=None, log_handler: logging.Handler = DEFAULT_LOG_HANDLER, | ||
log_formatter: logging.Formatter = DEFAULT_LOG_FORMATTER, | ||
log_level: int = DEFAULT_LOG_LEVEL) -> logging.Logger: | ||
''' | ||
Define a logger with the passed module_name at module level or function level | ||
''' | ||
logger = logging.getLogger(logger_name) | ||
|
||
# Do not customize root logger. | ||
if logger_name or __ROOT_DEBUG: | ||
if not log_handler: | ||
log_handler = logging.StreamHandler() | ||
|
||
# add formatter to handler | ||
log_handler.setFormatter(log_formatter) | ||
|
||
if log_handler: | ||
try: | ||
logger.removeHandler(log_handler) | ||
except: | ||
pass | ||
logger.addHandler(log_handler) | ||
|
||
if logger_name: | ||
logger.propagate = False | ||
|
||
logger.setLevel(log_level) | ||
return logger | ||
|
||
ROOT_LOGGER = get_logger() |