"""
Logging facility for the application.
This module is to be imported from userland.
After import, the logger object should be retrieved like that:
log = logging.getLogger("datestemper.<whatever>")
"""

import logging
import logging.handlers as handlers
import os
from pathlib import Path


# Relative to working directory
LOG_NAME = "datestamper"
LOG_DIR = "log"
LOG_FILE = Path(Path.cwd(), LOG_DIR, LOG_NAME + ".log")


# Create log dir & file if does not exists
LOG_FILE.parent.mkdir(parents=True, exist_ok=True)
LOG_FILE.touch(exist_ok=True)


root_logger = logging.getLogger(LOG_NAME)
root_logger.setLevel(logging.DEBUG)

logger_fmt = logging.Formatter(
    fmt="{asctime}.{msecs:03.0f} [{levelname:.1s}] [{name}] {message}",
    datefmt="%Y-%m-%d %H:%M:%S",
    style="{",
)

logger_fmt_lite = logging.Formatter(
    fmt="[{levelname:.1s}] {message}",
    style="{",
)

root_logger_sh = logging.StreamHandler()
root_logger_sh.setLevel(logging.INFO)
root_logger_sh.setFormatter(logger_fmt_lite)

root_logger_fh = logging.handlers.RotatingFileHandler(
    filename=LOG_FILE, encoding="UTF-8", maxBytes=1024
)
root_logger_fh.setLevel(logging.DEBUG)
root_logger_fh.setFormatter(logger_fmt)

root_logger.addHandler(root_logger_sh)
root_logger.addHandler(root_logger_fh)