#!/usr/bin/python3
# -*- coding: utf-8 -*-

# -----------------------------------------------------------
# created 02.02.2021, tkaulke
# Thomas Kaulke, kaulkth@gmail.com
# https://github.com/kaulketh
# -----------------------------------------------------------

__author__ = "Thomas Kaulke"
__email__ = "kaulketh@gmail.com"

import errno
import logging
import os
from logging.config import fileConfig

# runtime location
this_folder = os.path.dirname(os.path.abspath(__file__))
# define log folder related to location
log_folder = os.path.join(this_folder, '../logs')

# define ini and log files
ini_file = 'debug.ini'
info_log_file = log_folder + '/info.log'
error_log_file = log_folder + '/error.log'

# check if exists or create log folder
try:
    os.makedirs(log_folder, exist_ok=True)  # Python>3.2
except TypeError:
    try:
        os.makedirs(log_folder)
    except OSError as exc:  # Python >2.5
        if exc.errno == errno.EEXIST and os.path.isdir(log_folder):
            pass
        else:
            raise

# setup configuration
config_file = os.path.join(this_folder, ini_file)
fileConfig(config_file, disable_existing_loggers=True)

# create handlers
handler_info = logging.FileHandler(os.path.join(this_folder, info_log_file))
handler_error = logging.FileHandler(os.path.join(this_folder, error_log_file))
# set levels
handler_info.setLevel(logging.INFO)
handler_error.setLevel(logging.ERROR)

# create formatters and add to handlers
format_info = \
    logging.Formatter('%(asctime)s  %(levelname)s '
                      '[ %(module)s.%(funcName)s  linenr.%(lineno)s ] '
                      '%(message).180s', datefmt='%Y-%m-%d %H:%M:%S')
format_error = \
    logging.Formatter(
        '%(asctime)s  %(levelname)s '
        '[ %(module)s.%(funcName)s  linenr.%(lineno)s ] '
        '[ thread: %(threadName)s ] %(message)s')
handler_info.setFormatter(format_info)
handler_error.setFormatter(format_error)


def get_logger(name: str = __name__):
    logger = logging.getLogger(name)
    # add handler
    logger.addHandler(handler_info)
    logger.addHandler(handler_error)
    return logger


if __name__ == '__main__':
    pass

[((533, 569), 'os.path.join', 'os.path.join', (['this_folder', '"""../logs"""'], {}), "(this_folder, '../logs')\n", (545, 569), False, 'import os\n'), ((1062, 1097), 'os.path.join', 'os.path.join', (['this_folder', 'ini_file'], {}), '(this_folder, ini_file)\n', (1074, 1097), False, 'import os\n'), ((1098, 1152), 'logging.config.fileConfig', 'fileConfig', (['config_file'], {'disable_existing_loggers': '(True)'}), '(config_file, disable_existing_loggers=True)\n', (1108, 1152), False, 'from logging.config import fileConfig\n'), ((1476, 1625), 'logging.Formatter', 'logging.Formatter', (['"""%(asctime)s  %(levelname)s [ %(module)s.%(funcName)s  linenr.%(lineno)s ] %(message).180s"""'], {'datefmt': '"""%Y-%m-%d %H:%M:%S"""'}), "(\n    '%(asctime)s  %(levelname)s [ %(module)s.%(funcName)s  linenr.%(lineno)s ] %(message).180s'\n    , datefmt='%Y-%m-%d %H:%M:%S')\n", (1493, 1625), False, 'import logging\n'), ((1687, 1830), 'logging.Formatter', 'logging.Formatter', (['"""%(asctime)s  %(levelname)s [ %(module)s.%(funcName)s  linenr.%(lineno)s ] [ thread: %(threadName)s ] %(message)s"""'], {}), "(\n    '%(asctime)s  %(levelname)s [ %(module)s.%(funcName)s  linenr.%(lineno)s ] [ thread: %(threadName)s ] %(message)s'\n    )\n", (1704, 1830), False, 'import logging\n'), ((453, 478), 'os.path.abspath', 'os.path.abspath', (['__file__'], {}), '(__file__)\n', (468, 478), False, 'import os\n'), ((754, 792), 'os.makedirs', 'os.makedirs', (['log_folder'], {'exist_ok': '(True)'}), '(log_folder, exist_ok=True)\n', (765, 792), False, 'import os\n'), ((1207, 1247), 'os.path.join', 'os.path.join', (['this_folder', 'info_log_file'], {}), '(this_folder, info_log_file)\n', (1219, 1247), False, 'import os\n'), ((1285, 1326), 'os.path.join', 'os.path.join', (['this_folder', 'error_log_file'], {}), '(this_folder, error_log_file)\n', (1297, 1326), False, 'import os\n'), ((1985, 2008), 'logging.getLogger', 'logging.getLogger', (['name'], {}), '(name)\n', (2002, 2008), False, 'import logging\n'), ((842, 865), 'os.makedirs', 'os.makedirs', (['log_folder'], {}), '(log_folder)\n', (853, 865), False, 'import os\n'), ((949, 974), 'os.path.isdir', 'os.path.isdir', (['log_folder'], {}), '(log_folder)\n', (962, 974), False, 'import os\n')]