# Buggy code
def get_logger(
    module=None,
    with_more_info=False,
    allow_site=True,
    filter=None,
    max_size=100_000,
    file_count=20,
):
    """Application Logger for your given module

    Args:
            module (str, optional): Name of your logger and consequently your log file. Defaults to None.
            with_more_info (bool, optional): Will log the form dict using the SiteContextFilter. Defaults to False.
            allow_site ((str, bool), optional): Pass site name to explicitly log under it's logs. If True and unspecified, guesses which site the logs would be saved under. Defaults to True.
            filter (function, optional): Add a filter function for your logger. Defaults to None.
            max_size (int, optional): Max file size of each log file in bytes. Defaults to 100_000.
            file_count (int, optional): Max count of log files to be retained via Log Rotation. Defaults to 20.

    Returns:
            <class 'logging.Logger'>: Returns a Python logger object with Site and Bench level logging capabilities.
    """

    if allow_site is True:
        site = getattr(frappe.local, "site", None)
    elif allow_site:
        site = allow_site
    else:
        site = False

    logger_name = "{0}-{1}".format(module, site or "all")

    try:
        return frappe.loggers[logger_name]
    except KeyError:
        pass

    if not module:
        module = "frappe"
        with_more_info = True

    logfile = module + ".log"
    log_filename = os.path.join("..", "logs", logfile)

    logger = logging.getLogger(logger_name)
    logger.setLevel(frappe.log_level or default_log_level)
    logger.propagate = False

    formatter = logging.Formatter(
        "%(asctime)s %(levelname)s {0} %(message)s".format(module)
    )
    handler = RotatingFileHandler(
        log_filename, maxBytes=max_size, backupCount=file_count
    )
    handler.setFormatter(formatter)
    logger.addHandler(handler)

    if site:
        sitelog_filename = os.path.join(site, "logs", logfile)
        site_handler = RotatingFileHandler(
            sitelog_filename, maxBytes=max_size, backupCount=file_count
        )
        site_handler.setFormatter(formatter)
        logger.addHandler(site_handler)

    if with_more_info:
        handler.addFilter(SiteContextFilter())

    if filter:
        logger.addFilter(filter)

    frappe.loggers[logger_name] = logger

    return logger


# Traceback (FileNotFoundError)
127.0.0.1 - - [03/Aug/2020 10:17:14] "GET / HTTP/1.0" 500 -
10:17:14 web.1            | INFO:werkzeug:127.0.0.1 - - [03/Aug/2020 10:17:14] "GET / HTTP/1.0" 500 -
10:17:14 web.1            | Traceback (most recent call last):
10:17:14 web.1            |   File "/home/frappe/frappe-bench/apps/frappe/frappe/middlewares.py", line 16, in __call__
10:17:14 web.1            |     return super(StaticDataMiddleware, self).__call__(environ, start_response)
10:17:14 web.1            |   File "/home/frappe/frappe-bench/env/lib/python3.6/site-packages/werkzeug/middleware/shared_data.py", line 220, in __call__
10:17:14 web.1            |     return self.app(environ, start_response)
10:17:14 web.1            |   File "/home/frappe/frappe-bench/env/lib/python3.6/site-packages/werkzeug/middleware/shared_data.py", line 220, in __call__
10:17:14 web.1            |     return self.app(environ, start_response)
10:17:14 web.1            |   File "/home/frappe/frappe-bench/env/lib/python3.6/site-packages/werkzeug/local.py", line 231, in application
10:17:14 web.1            |     return ClosingIterator(app(environ, start_response), self.cleanup)
10:17:14 web.1            |   File "/home/frappe/frappe-bench/env/lib/python3.6/site-packages/werkzeug/wrappers/base_request.py", line 237, in application
10:17:14 web.1            |     resp = f(*args[:-2] + (request,))
10:17:14 web.1            |   File "/home/frappe/frappe-bench/apps/frappe/frappe/app.py", line 103, in application
10:17:14 web.1            |     frappe.logger("frappe.web", allow_site=_site).info({
10:17:14 web.1            |   File "/home/frappe/frappe-bench/apps/frappe/frappe/__init__.py", line 1565, in logger
10:17:14 web.1            |     return get_logger(module=module, with_more_info=with_more_info, allow_site=allow_site, filter=filter, max_size=max_size, file_count=file_count)
10:17:14 web.1            |   File "/home/frappe/frappe-bench/apps/frappe/frappe/utils/logger.py", line 66, in get_logger
10:17:14 web.1            |     site_handler = RotatingFileHandler(sitelog_filename, maxBytes=max_size, backupCount=file_count)
10:17:14 web.1            |   File "/usr/lib/python3.6/logging/handlers.py", line 150, in __init__
10:17:14 web.1            |     BaseRotatingHandler.__init__(self, filename, mode, encoding, delay)
10:17:14 web.1            |   File "/usr/lib/python3.6/logging/handlers.py", line 57, in __init__
10:17:14 web.1            |     logging.FileHandler.__init__(self, filename, mode, encoding, delay)
10:17:14 web.1            |   File "/usr/lib/python3.6/logging/__init__.py", line 1032, in __init__
10:17:14 web.1            |     StreamHandler.__init__(self, self._open())
10:17:14 web.1            |   File "/usr/lib/python3.6/logging/__init__.py", line 1061, in _open
10:17:14 web.1            |     return open(self.baseFilename, self.mode, encoding=self.encoding)
10:17:14 web.1            | FileNotFoundError: [Errno 2] No such file or directory: '/home/frappe/frappe-bench/sites/167.71.179.108/logs/frappe.web.log'
10:17:15 web.1            | 127.0.0.1 - - [03/Aug/2020 10:17:15] "GET /?__debugger__=yes&amp;cmd=resource&amp;f=style.css HTTP/1.0" 200 -
10:17:15 web.1            | INFO:werkzeug:127.0.0.1 - - [03/Aug/2020 10:17:15] "GET /?__debugger__=yes&amp;cmd=resource&amp;f=style.css HTTP/1.0" 200 -
10:17:15 web.1            | 127.0.0.1 - - [03/Aug/2020 10:17:15] "GET /?__debugger__=yes&amp;cmd=resource&amp;f=jquery.js HTTP/1.0" 200 -
10:17:15 web.1            | INFO:werkzeug:127.0.0.1 - - [03/Aug/2020 10:17:15] "GET /?__debugger__=yes&amp;cmd=resource&amp;f=jquery.js HTTP/1.0" 200 -
10:17:15 web.1            | 127.0.0.1 - - [03/Aug/2020 10:17:15] "GET /?__debugger__=yes&amp;cmd=resource&amp;f=debugger.js HTTP/1.0" 200 -
10:17:15 web.1            | INFO:werkzeug:127.0.0.1 - - [03/Aug/2020 10:17:15] "GET /?__debugger__=yes&amp;cmd=resource&amp;f=debugger.js HTTP/1.0" 200 -
10:17:15 web.1            | 127.0.0.1 - - [03/Aug/2020 10:17:15] "GET /?__debugger__=yes&amp;cmd=resource&amp;f=console.png HTTP/1.0" 200 -
10:17:15 web.1            | INFO:werkzeug:127.0.0.1 - - [03/Aug/2020 10:17:15] "GET /?__debugger__=yes&amp;cmd=resource&amp;f=console.png HTTP/1.0" 200 -
10:17:15 web.1            | 127.0.0.1 - - [03/Aug/2020 10:17:15] "GET /?__debugger__=yes&amp;cmd=resource&amp;f=ubuntu.ttf HTTP/1.0" 200 -
10:17:15 web.1            | INFO:werkzeug:127.0.0.1 - - [03/Aug/2020 10:17:15] "GET /?__debugger__=yes&amp;cmd=resource&amp;f=ubuntu.ttf HTTP/1.0" 200 -

# Fixed code
def get_logger(
    module=None,
    with_more_info=False,
    allow_site=True,
    filter=None,
    max_size=100_000,
    file_count=20,
):
    """Application Logger for your given module

    Args:
            module (str, optional): Name of your logger and consequently your log file. Defaults to None.
            with_more_info (bool, optional): Will log the form dict using the SiteContextFilter. Defaults to False.
            allow_site ((str, bool), optional): Pass site name to explicitly log under it's logs. If True and unspecified, guesses which site the logs would be saved under. Defaults to True.
            filter (function, optional): Add a filter function for your logger. Defaults to None.
            max_size (int, optional): Max file size of each log file in bytes. Defaults to 100_000.
            file_count (int, optional): Max count of log files to be retained via Log Rotation. Defaults to 20.

    Returns:
            <class 'logging.Logger'>: Returns a Python logger object with Site and Bench level logging capabilities.
    """

    if allow_site is True:
        site = getattr(frappe.local, "site", None)
    elif allow_site in get_sites():
        site = allow_site
    else:
        site = False

    logger_name = "{0}-{1}".format(module, site or "all")

    try:
        return frappe.loggers[logger_name]
    except KeyError:
        pass

    if not module:
        module = "frappe"
        with_more_info = True

    logfile = module + ".log"
    log_filename = os.path.join("..", "logs", logfile)

    logger = logging.getLogger(logger_name)
    logger.setLevel(frappe.log_level or default_log_level)
    logger.propagate = False

    formatter = logging.Formatter(
        "%(asctime)s %(levelname)s {0} %(message)s".format(module)
    )
    handler = RotatingFileHandler(
        log_filename, maxBytes=max_size, backupCount=file_count
    )
    handler.setFormatter(formatter)
    logger.addHandler(handler)

    if site:
        sitelog_filename = os.path.join(site, "logs", logfile)
        site_handler = RotatingFileHandler(
            sitelog_filename, maxBytes=max_size, backupCount=file_count
        )
        site_handler.setFormatter(formatter)
        logger.addHandler(site_handler)

    if with_more_info:
        handler.addFilter(SiteContextFilter())

    if filter:
        logger.addFilter(filter)

    frappe.loggers[logger_name] = logger

    return logger