forked from EmergenceAI/Agent-E
-
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.
Merge pull request EmergenceAI#88 from EmergenceAI/logging_to_files_o…
…ptional_and_structured_logging structured logging and optional file logging
- Loading branch information
Showing
7 changed files
with
102 additions
and
33 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
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,15 @@ | ||
|
||
def str_to_bool(s: str | bool) -> bool: | ||
""" | ||
Convert a string representation of truth to True or False. | ||
Parameters: | ||
s (str | bool): The string to convert, or a boolean. | ||
Returns: | ||
bool: True if the string represents a truth value, False otherwise. | ||
""" | ||
if isinstance(s, bool): | ||
return s | ||
return s.lower() in ['true', '1', 't', 'y', 'yes'] | ||
|
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 |
---|---|---|
@@ -1,29 +1,59 @@ | ||
import logging | ||
import os | ||
|
||
from dotenv import load_dotenv | ||
from pythonjsonlogger import jsonlogger | ||
|
||
# Load environment variables from a .env file | ||
load_dotenv() | ||
|
||
logger = logging.getLogger(__name__) | ||
'''logging.basicConfig( | ||
level=logging.DEBUG, # change level here or use set_log_level() to change it | ||
format="[%(asctime)s] %(levelname)s {%(filename)s:%(lineno)d} - %(message)s", filename='app.log', filemode='a' | ||
)''' | ||
logging.basicConfig(level=logging.INFO) | ||
logging.getLogger("httpcore").setLevel(logging.WARNING) | ||
logging.getLogger("httpx").setLevel(logging.WARNING) | ||
logging.getLogger("matplotlib.pyplot").setLevel(logging.WARNING) | ||
logging.getLogger("PIL.PngImagePlugin").setLevel(logging.WARNING) | ||
logging.getLogger("PIL.Image").setLevel(logging.WARNING) | ||
|
||
def set_log_level(level: str | int) -> None: | ||
# Custom function to configure the logger | ||
def configure_logger(level: str = "INFO") -> None: | ||
log_format = os.getenv("LOG_MESSAGES_FORMAT", "text").lower() | ||
|
||
logger.setLevel(level.upper()) | ||
|
||
# Create a handler for logging | ||
handler = logging.StreamHandler() | ||
|
||
if log_format == "json": | ||
# JSON format | ||
formatter = jsonlogger.JsonFormatter( | ||
fmt='%(asctime)s %(name)s %(levelname)s %(message)s %(filename)s %(lineno)d', | ||
datefmt='%Y-%m-%d %H:%M:%S' | ||
) | ||
else: | ||
# Text format | ||
formatter = logging.Formatter( | ||
fmt='[%(asctime)s] %(levelname)s {%(filename)s:%(lineno)d} - %(message)s', | ||
datefmt='%Y-%m-%d %H:%M:%S' | ||
) | ||
|
||
handler.setFormatter(formatter) | ||
logger.handlers = [] # Clear existing handlers | ||
logger.addHandler(handler) | ||
|
||
# Call the configure logger function to set up the logger initially | ||
configure_logger(level="INFO") | ||
|
||
# Function to set log level | ||
def set_log_level(level: str) -> None: | ||
""" | ||
Set the log level for the logger. | ||
Parameters: | ||
- level (Union[str, int]): A string or logging level such as 'debug', 'info', 'warning', 'error', or 'critical', or the corresponding logging constants like logging.DEBUG, logging.INFO, etc. | ||
- level (str): A logging level such as 'debug', 'info', 'warning', 'error', or 'critical'. | ||
""" | ||
if isinstance(level, str): | ||
level = level.upper() | ||
numeric_level = getattr(logging, level, None) | ||
if not isinstance(numeric_level, int): | ||
raise ValueError(f'Invalid log level: {level}') | ||
logger.setLevel(numeric_level) | ||
else: | ||
logger.setLevel(level) | ||
configure_logger(level) | ||
|
||
# Set default log levels for other libraries | ||
logging.getLogger("httpcore").setLevel(logging.WARNING) | ||
logging.getLogger("httpx").setLevel(logging.WARNING) | ||
logging.getLogger("matplotlib.pyplot").setLevel(logging.WARNING) | ||
logging.getLogger("PIL.PngImagePlugin").setLevel(logging.WARNING) | ||
logging.getLogger("PIL.Image").setLevel(logging.WARNING) | ||
|
||
# Re-export the logger for ease of use | ||
__all__ = ["logger", "set_log_level"] |
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