Commit 5d683d80 by Oleksandr Barabash

logging module added

parent 6fed05f7
......@@ -23,6 +23,7 @@ from config import AppConfig, COSMOS_CLIENT, CosmosDBConfig
from entities.json.notification import Notification, NotificationCosmos
from utils.cosmos_client import ItemNotFound
from utils.json_func import json_loads
from utils.log import Log
app_config = AppConfig()
......@@ -32,6 +33,7 @@ app_settings = BotFrameworkAdapterSettings(app_config.APP_ID,
app_config.APP_PASSWORD)
ADAPTER = BotFrameworkAdapter(app_settings)
TAG = __name__
# noinspection PyShadowingNames
......@@ -102,10 +104,10 @@ async def v1_get_notification(request: Request) -> Response:
))
return Response(body=json.dumps(data), status=HTTPStatus.OK)
except ItemNotFound as e:
print("ItemNotFound:", e)
Log.e(TAG, "v1_get_notification::item not found", e)
return Response(status=HTTPStatus.NOT_FOUND)
except Exception as e:
print("error:", e)
Log.e(TAG, exc_info=e)
return Response(status=HTTPStatus.BAD_REQUEST)
......@@ -154,6 +156,7 @@ async def v1_messages(request: Request) -> Response:
async def v1_health_check(_request: Request) -> Response:
""" Health check """
# TODO(s1z): Add checks here. DB, etc.
Log.i(TAG, "v1_health_check::ok")
return Response(status=HTTPStatus.OK)
......
""" Log helper module """
import logging
from __future__ import absolute_import
from __future__ import unicode_literals
import traceback
import logging
def init_logging(filename=None, level=None):
""" init logging on the app level """
logging_config = {"format": "%(asctime)-23s %(levelname)8s: %(message)s",
"level": level or logging.INFO}
if filename is not None:
logging_config["filename"] = filename
logging.getLogger().handlers = []
logging.basicConfig(**logging_config)
class Log(object):
""" Logger class """
LEVEL = logging.DEBUG
class Log:
""" Logger. This is a pretty useful 'Java style' logger """
logger = logging.getLogger()
logging_config = {"format": "%(asctime)-23s %(levelname)8s: %(message)s",
"level": logging.DEBUG}
logging.basicConfig(**logging_config)
@staticmethod
def log(level, source, message="", exc_info=None):
""" log method """
logger = logging.getLogger()
line = "{source}{message}{exc}"
logger = logging.getLogger(source)
line = "{message}{exc}"
exc = ''
if isinstance(exc_info, (list, tuple)):
ex_type, ex_value, ex_traceback = exc_info
exc = ": " + ''.join(
traceback.format_exception(ex_type, ex_value, ex_traceback)
)
message = "::{}".format(message) if message else ""
logger.log(level, line.format(source=source, message=message, exc=exc))
message = "::{}".format(message) if message else ''
logger.log(level, line.format(message=message, exc=exc))
@staticmethod
def w(source, message="", exc_info=None):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment