36 lines
1.1 KiB
Python
36 lines
1.1 KiB
Python
|
|
import logging
|
||
|
|
import sys
|
||
|
|
|
||
|
|
def setup_logger():
|
||
|
|
"""Sets up a custom logger for the application."""
|
||
|
|
# Get the logger
|
||
|
|
logger = logging.getLogger("CityBuilder")
|
||
|
|
logger.setLevel(logging.DEBUG)
|
||
|
|
|
||
|
|
# Prevent adding duplicate handlers if this function is called multiple times
|
||
|
|
if logger.hasHandlers():
|
||
|
|
logger.handlers.clear()
|
||
|
|
|
||
|
|
# Create a formatter to define the log message format
|
||
|
|
formatter = logging.Formatter(
|
||
|
|
'%(asctime)s - %(name)s - [%(levelname)s] - %(message)s',
|
||
|
|
datefmt='%Y-%m-%d %H:%M:%S'
|
||
|
|
)
|
||
|
|
|
||
|
|
# Create a handler to output log messages to the console (stdout)
|
||
|
|
stream_handler = logging.StreamHandler(sys.stdout)
|
||
|
|
stream_handler.setFormatter(formatter)
|
||
|
|
|
||
|
|
# Add the handler to the logger
|
||
|
|
logger.addHandler(stream_handler)
|
||
|
|
|
||
|
|
# Optional: To log to a file, uncomment the following lines
|
||
|
|
# file_handler = logging.FileHandler("server.log")
|
||
|
|
# file_handler.setFormatter(formatter)
|
||
|
|
# logger.addHandler(file_handler)
|
||
|
|
|
||
|
|
return logger
|
||
|
|
|
||
|
|
# Create and export the logger instance
|
||
|
|
logger = setup_logger()
|