Logger
Setup Logging
This module sets up logging for the application.
setup_logging: Sets up logging for the application.
from emotion_detective.logger.logger import setup_logging
# Example usage
logger = setup_logging()
The setup_logging function configures a logger to output log messages to both a file and the console. The log messages include a timestamp, the log level, and the message. The log file is saved as ‘logs/emotion_detective.txt’.
- emotion_detective.logger.logger.setup_logging()
Sets up logging for the application.
This function configures a logger to output log messages to both a file and the console. The log messages include a timestamp, the log level, and the message.
The log file is saved as ‘logs/emotion_detective.txt’.
- Returns:
Configured logger instance.
- Return type:
logging.Logger
Author: Andrea Tosheva
Troubleshooting
If you encounter issues with the logger setup, consider the following troubleshooting steps:
Check Log Directory: Ensure that the logs directory exists and is writable. If it doesn’t exist, create it manually:
mkdir logs
File Permissions: Verify that the file logs/emotion_detective.txt has the correct permissions and is writable by the application.
Logger Configuration: Make sure the logger is not being configured multiple times, which can lead to duplicate log entries. You can add a check to see if the logger has already been configured:
if not logger.handlers: # Add handlers only if they haven't been added yet logger.addHandler(file_handler) logger.addHandler(console_handler)
Logging Level: Adjust the logging level as needed. The current setup is configured for logging.INFO. For more detailed logs, use logging.DEBUG.
Console Output: If console output is not visible, ensure the console handler is properly configured and not being suppressed by the logging level.
Example Troubleshooting Code:
import logging
import os
def setup_logging():
"""
Sets up logging for the application.
This function configures a logger to output log messages to both a file and the console.
The log messages include a timestamp, the log level, and the message.
The log file is saved as 'logs/emotion_detective.txt'.
Returns:
logging.Logger: Configured logger instance.
Author: Andrea Tosheva
"""
# Set up logger
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
# Create log directory if it doesn't exist
if not os.path.exists('logs'):
os.makedirs('logs')
# Create file handler and set formatter
file_handler = logging.FileHandler("logs/emotion_detective.txt")
formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
file_handler.setFormatter(formatter)
# Create console handler and set formatter
console_handler = logging.StreamHandler()
console_handler.setFormatter(formatter)
# Add handlers to logger if they haven't been added yet
if not logger.handlers:
logger.addHandler(file_handler)
logger.addHandler(console_handler)
return logger