import logging
from os import PathLike
from typing import Optional, Union


def setup_logger(
    log_file: Optional[Union[str, PathLike[str]]] = None,
    level: int = logging.INFO,
    overwrite_handler: bool = False,
) -> logging.Logger:
    # Create a logger and set the logging level
    logger = logging.getLogger()
    logger.setLevel(level)

    # if logger already has handlers, just return it
    if logger.hasHandlers() and not overwrite_handler:
        return logger

    # Define the log message format
    log_format = "%(asctime)s [%(levelname)s] %(message)s"
    date_format = "%Y-%m-%d %H:%M:%S"
    formatter = logging.Formatter(log_format, datefmt=date_format)

    # Create a file handler and set the file path and format
    if log_file:
        file_handler = logging.FileHandler(log_file)
        file_handler.setLevel(level)
        file_handler.setFormatter(formatter)
        logger.addHandler(file_handler)

    # Create a console handler and set the format
    console_handler = logging.StreamHandler()
    console_handler.setLevel(level)

    # Set the formatter for the handlers
    console_handler.setFormatter(formatter)

    # Add the handlers to the logger
    logger.addHandler(console_handler)

    return logger
