# Buggy code
def setup_logging(app):
    del app.logger.handlers[:]

    # for key in logging.Logger.manager.loggerDict:
    #     print(key)

    loggers = [
        app.logger,
        logging.getLogger("alerta"),  # ??
        # logging.getLogger('flask'),  # ??
        logging.getLogger("flask_compress"),  # ??
        # logging.getLogger('flask_cors'),  # ??
        logging.getLogger("pymongo"),  # ??
        logging.getLogger("raven"),  # ??
        logging.getLogger("requests"),  # ??
        logging.getLogger("sentry"),  # ??
        logging.getLogger("urllib3"),  # ??
        logging.getLogger("werkzeug"),  # ??
    ]

    if app.debug:
        log_level = logging.DEBUG
    else:
        log_level = logging.INFO

    if app.config["LOG_FILE"]:
        from logging.handlers import RotatingFileHandler

        handler = RotatingFileHandler(
            filename=app.config["LOG_FILE"],
            maxBytes=app.config["LOG_MAX_BYTES"],
            backupCount=app.config["LOG_BACKUP_COUNT"],
        )
        handler.setLevel(log_level)
        handler.setFormatter(logging.Formatter(app.config["LOG_FORMAT"]))
    else:
        handler = logging.StreamHandler()
        handler.setLevel(log_level)
        handler.setFormatter(logging.Formatter(app.config["LOG_FORMAT"]))

    for logger in loggers:
        logger.addHandler(handler)
        logger.setLevel(log_level)
        logger.propagate = True


# Traceback (UnicodeEncodeError)
--- Logging error ---
Traceback (most recent call last):
File "/usr/lib64/python3.5/logging/__init__.py", line 988, in emit
stream.write(msg)
UnicodeEncodeError: 'ascii' codec can't encode character '\\u5e74' in position 1710: ordinal not in range(128)
Call stack:
File "/usr/lib/python3.5/site-packages/flask/app.py", line 2309, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/lib/python3.5/site-packages/flask/app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "/usr/lib/python3.5/site-packages/flask/app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/lib/python3.5/site-packages/flask/app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/usr/lib/python3.5/site-packages/flask_cors/decorator.py", line 128, in wrapped_function
resp = make_response(f(*args, **kwargs))
File "/usr/lib/python3.5/site-packages/alerta/auth/utils.py", line 123, in wrapped
return f(*args, **kwargs)
File "/usr/lib/python3.5/site-packages/alerta/models/metrics.py", line 259, in wrapped
response = f(*args, **kwargs)
File "/usr/lib/python3.5/site-packages/alerta/utils/api.py", line 32, in decorated
return func(*args, **kwargs)
File "/usr/lib/python3.5/site-packages/alerta/views/alerts.py", line 42, in receive
alert = process_alert(incomingAlert)
File "/usr/lib/python3.5/site-packages/alerta/utils/api.py", line 103, in process_alert
updated = plugin.post_receive(alert)
File "/usr/lib/python3.5/site-packages/alerta_amqp.py", line 52, in post_receive
LOG.debug('Message: %s', body)
Message: 'Message: %s'
Arguments: {'createTime': '2018-07-26T06:39:04.653Z', 'duplicateCount': 2, 'type': 'zabbixAlert', 'repeat': True, 'severity': 'major', 'correlate': [], 'text': 'PROBLEM: log trigger zbx.log', 'history': [{'text': 'PROBLEM: log trigger zbx.log', 'type': 'severity', 'status': None, 'value': 'hogehoge', 'event': 'log[/tmp/zbx.log]', 'severity': 'major', 'updateTime': datetime.datetime(2018, 7, 26, 6, 39, 4, 653000), 'href': 'http://dhcp-175-92/alert/3013a7bb-1213-47fa-9135-8845908abd1a', 'id': '3013a7bb-1213-47fa-9135-8845908abd1a'}, {'text': 'new alert status change', 'type': 'status', 'status': 'open', 'value': None, 'event': 'log[/tmp/zbx.log]', 'severity': None, 'updateTime': datetime.datetime(2018, 7, 26, 6, 39, 4, 676000), 'href': 'http://dhcp-175-92/alert/3013a7bb-1213-47fa-9135-8845908abd1a', 'id': '3013a7bb-1213-47fa-9135-8845908abd1a'}, {'text': 'duplicate alert with value change', 'type': 'value', 'status': None, 'value': 'hogehoge1', 'event': 'log[/tmp/zbx.log]', 'severity': None, 'updateTime': datetime.datetime(2018, 7, 26, 6, 39, 54, 592000), 'href': 'http://dhcp-175-92/alert/5376a8d9-29ee-4313-9cab-cb6b9b5998c5', 'id': '5376a8d9-29ee-4313-9cab-cb6b9b5998c5'}, {'text': 'status change via console by Yoshiharu Mori (acknowledged in Zabbix)', 'type': 'action', 'status': 'ack', 'value': None, 'event': 'log[/tmp/zbx.log]', 'severity': 'major', 'updateTime': datetime.datetime(2018, 7, 26, 6, 44, 36, 456000), 'href': 'http://dhcp-175-92/alert/3013a7bb-1213-47fa-9135-8845908abd1a', 'id': '3013a7bb-1213-47fa-9135-8845908abd1a'}, {'text': 'duplicate alert with value change', 'type': 'value', 'status': None, 'value': '2018\xe5\xb9\xb4 7\xe6\x9c\x88 27\xe6\x97\xa5 \xe9\x87\x91\xe6\x9b\x9c\xe6\x97\xa5 16:48:49 JST hogehoge', 'event': 'log[/tmp/zbx.log]', 'severity': None, 'updateTime': datetime.datetime(2018, 7, 27, 7, 49, 10, 102000), 'href': 'http://dhcp-175-92/alert/8a5fdc71-90ab-4439-967a-ca34ede2bafb', 'id': '8a5fdc71-90ab-4439-967a-ca34ede2bafb'}], 'customer': None, 'status': 'ack', 'value': '2018\xe5\xb9\xb4 7\xe6\x9c\x88 27\xe6\x97\xa5 \xe9\x87\x91\xe6\x9b\x9c\xe6\x97\xa5 16:48:49 JST hogehoge', 'event': 'log[/tmp/zbx.log]', 'attributes': {'triggerID': '13563', 'eventId': '981', 'thresholdInfo': '*UNKNOWN*: {Zabbix server:log[/tmp/zbx.log].strlen()}>0', 'ip': '133.137.175.143'}, 'previousSeverity': 'indeterminate', 'timeout': 86400, 'resource': 'Zabbix server', 'lastReceiveId': '8a5fdc71-90ab-4439-967a-ca34ede2bafb', 'trendIndication': 'moreSevere', 'tags': ['{EVENT.TAGS}'], 'receiveTime': '2018-07-26T06:39:04.676Z', 'group': 'Zabbix', 'id': '3013a7bb-1213-47fa-9135-8845908abd1a', 'service': ['Zabbix servers'], 'href': 'http://dhcp-175-92/alert/3013a7bb-1213-47fa-9135-8845908abd1a', 'lastReceiveTime': '2018-07-27T07:49:10.102Z', 'environment': 'Zabbix 1st', 'rawData': None, 'origin': 'zabbix/dhcp-175-143'}

# Fixed code
def setup_logging(app):
    del app.logger.handlers[:]

    # for key in logging.Logger.manager.loggerDict:
    #     print(key)

    loggers = [
        app.logger,
        logging.getLogger("alerta"),  # ??
        # logging.getLogger('flask'),  # ??
        logging.getLogger("flask_compress"),  # ??
        # logging.getLogger('flask_cors'),  # ??
        logging.getLogger("pymongo"),  # ??
        logging.getLogger("raven"),  # ??
        logging.getLogger("requests"),  # ??
        logging.getLogger("sentry"),  # ??
        logging.getLogger("urllib3"),  # ??
        logging.getLogger("werkzeug"),  # ??
    ]

    if app.debug:
        log_level = logging.DEBUG
    else:
        log_level = logging.INFO

    if app.config["LOG_FILE"]:
        from logging.handlers import RotatingFileHandler

        handler = RotatingFileHandler(
            filename=app.config["LOG_FILE"],
            maxBytes=app.config["LOG_MAX_BYTES"],
            backupCount=app.config["LOG_BACKUP_COUNT"],
            encoding="utf-8",
        )
        handler.setLevel(log_level)
        handler.setFormatter(logging.Formatter(app.config["LOG_FORMAT"]))
    else:
        handler = logging.StreamHandler()
        handler.setLevel(log_level)
        handler.setFormatter(logging.Formatter(app.config["LOG_FORMAT"]))

    for logger in loggers:
        logger.addHandler(handler)
        logger.setLevel(log_level)
        logger.propagate = True