Reworked logging to optionally log to file as well
parent
63256b8984
commit
1a97a0a78e
51
bots.py
51
bots.py
|
@ -31,6 +31,13 @@ except json.JSONDecodeError as e:
|
||||||
print(f"Error parsing config.json: {e}")
|
print(f"Error parsing config.json: {e}")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
# Initiate logfile
|
||||||
|
logfile_path = config_data["logfile_path"]
|
||||||
|
logfile = open(logfile_path, "a")
|
||||||
|
if not config_data["log_to_terminal"] and not config_data["log_to_file"]:
|
||||||
|
print(f"!!! WARNING !!! CONSOLE AND LOGFILE OUTPUT DISABLED !!!\n!!! NO LOGS WILL BE PROVIDED !!!")
|
||||||
|
first_log_entry = True
|
||||||
|
|
||||||
###############################
|
###############################
|
||||||
# Simple Logging System
|
# Simple Logging System
|
||||||
###############################
|
###############################
|
||||||
|
@ -40,35 +47,49 @@ def log(message, level="INFO", exec_info=False):
|
||||||
A simple logging function with adjustable log levels.
|
A simple logging function with adjustable log levels.
|
||||||
Logs messages in a structured format.
|
Logs messages in a structured format.
|
||||||
|
|
||||||
Available levels:
|
Available levels:\n
|
||||||
DEBUG, INFO, WARNING, ERROR, CRITICAL, FATAL
|
DEBUG = Information useful for debugging\n
|
||||||
|
INFO = Informational messages\n
|
||||||
|
WARNING = Something happened that may lead to issues\n
|
||||||
|
ERROR = A non-critical error has happened\n
|
||||||
|
CRITICAL = A critical, but non-fatal, error\n
|
||||||
|
FATAL = Fatal error. Program exits after logging this\n\n
|
||||||
See 'config.json' for disabling/enabling logging levels
|
See 'config.json' for disabling/enabling logging levels
|
||||||
"""
|
"""
|
||||||
from modules import utility
|
from modules import utility
|
||||||
log_levels = ["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL", "FATAL"]
|
log_levels = ["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL", "FATAL"]
|
||||||
|
|
||||||
if level not in log_levels:
|
if level not in log_levels:
|
||||||
level = "INFO" # Default to INFO if an invalid level is provided
|
level = "INFO" # Default to INFO if an invalid level is provided
|
||||||
|
|
||||||
if level in config_data["log_levels"]:
|
if level in config_data["log_levels"] or first_log_entry:
|
||||||
|
|
||||||
elapsed = time.time() - globals.get_bot_start_time()
|
elapsed = time.time() - globals.get_bot_start_time()
|
||||||
uptime_str, _ = utility.format_uptime(elapsed)
|
uptime_str, _ = utility.format_uptime(elapsed)
|
||||||
timestamp = time.strftime('%Y-%m-%d %H:%M:%S')
|
timestamp = time.strftime('%Y-%m-%d %H:%M:%S')
|
||||||
log_message = f"[{timestamp} - {uptime_str}] [{level}] {message}"
|
log_message = f"[{timestamp} - {uptime_str}] [{level}] {message}"
|
||||||
|
|
||||||
# If traceback is requested (e.g., for errors)
|
# Include traceback for certain error levels
|
||||||
if exec_info or level == "CRITICAL" or level == "FATAL":
|
if exec_info or level in ["CRITICAL", "FATAL"]:
|
||||||
log_message += f"\n{traceback.format_exc()}"
|
log_message += f"\n{traceback.format_exc()}"
|
||||||
|
|
||||||
try:
|
# Print to terminal if enabled
|
||||||
print(log_message) # Print to terminal
|
if config_data["log_to_terminal"]:
|
||||||
if level == "FATAL":
|
print(log_message)
|
||||||
print(f"!!! FATAL ERROR LOGGED, SHUTTING DOWN !!!")
|
|
||||||
sys.exit(1)
|
|
||||||
except Exception:
|
|
||||||
pass # Prevent logging failures from crashing the bot
|
|
||||||
|
|
||||||
# Placeholder for future expansions (e.g., file logging, Discord alerts, etc.)
|
# Write to file if enabled
|
||||||
|
if config_data["log_to_file"]:
|
||||||
|
try:
|
||||||
|
with open(config_data["logfile_path"], "a", encoding="utf-8") as logfile:
|
||||||
|
logfile.write(f"{log_message}\n")
|
||||||
|
logfile.flush() # Ensure it gets written immediately
|
||||||
|
except Exception as e:
|
||||||
|
print(f"[WARNING] Failed to write to logfile: {e}")
|
||||||
|
|
||||||
|
# Handle fatal errors with shutdown
|
||||||
|
if level == "FATAL":
|
||||||
|
if config_data["log_to_terminal"]:
|
||||||
|
print(f"!!! FATAL ERROR LOGGED, SHUTTING DOWN !!!")
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
###############################
|
###############################
|
||||||
# Main Event Loop
|
# Main Event Loop
|
||||||
|
|
|
@ -2,6 +2,9 @@
|
||||||
"discord_guilds": [896713616089309184],
|
"discord_guilds": [896713616089309184],
|
||||||
"twitch_channels": ["OokamiKunTV", "ookamipup"],
|
"twitch_channels": ["OokamiKunTV", "ookamipup"],
|
||||||
"command_modules": ["cmd_discord", "cmd_twitch", "cmd_common"],
|
"command_modules": ["cmd_discord", "cmd_twitch", "cmd_common"],
|
||||||
"log_levels": ["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL", "FATAL"]
|
"log_levels": ["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL", "FATAL"],
|
||||||
|
"log_to_file": true,
|
||||||
|
"log_to_terminal": true,
|
||||||
|
"logfile_path": "logfile.log"
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue