Spaces:
Sleeping
Sleeping
File size: 1,207 Bytes
6c17133 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
import logging
import os
# Ensure the log directory exists
log_directory = 'logs'
os.makedirs(log_directory, exist_ok=True)
# Define the logging configuration
logging.basicConfig(
filename=os.path.join(log_directory, 'app.log'),
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S'
)
# Get a custom logger
def get_logger(name):
logger = logging.getLogger(name)
logger.setLevel(logging.DEBUG)
if not logger.hasHandlers():
# Create a file handler
file_handler = logging.FileHandler('logs/app.log')
file_handler.setLevel(logging.DEBUG)
# Create a console handler
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.DEBUG)
# Create a logging format
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
file_handler.setFormatter(formatter)
console_handler.setFormatter(formatter)
# Add the handlers to the logger
logger.addHandler(file_handler)
logger.addHandler(console_handler)
return logger
|