| quilesj | a049b7c | 2019-10-28 18:08:00 +0100 | [diff] [blame] | 1 | ## |
| 2 | # Copyright 2019 Telefonica Investigacion y Desarrollo, S.A.U. |
| 3 | # This file is part of OSM |
| 4 | # All Rights Reserved. |
| 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | # you may not use this file except in compliance with the License. |
| 8 | # You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 15 | # implied. |
| 16 | # See the License for the specific language governing permissions and |
| 17 | # limitations under the License. |
| 18 | # |
| 19 | # For those usages not covered by the Apache License, Version 2.0 please |
| 20 | # contact with: nfvlabs@tid.es |
| 21 | ## |
| 22 | |
| 23 | |
| quilesj | a049b7c | 2019-10-28 18:08:00 +0100 | [diff] [blame] | 24 | import asyncio |
| quilesj | a049b7c | 2019-10-28 18:08:00 +0100 | [diff] [blame] | 25 | import datetime |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame^] | 26 | import inspect |
| 27 | import logging |
| 28 | import threading # only for logging purposes (not for using threads) |
| 29 | import time |
| quilesj | a049b7c | 2019-10-28 18:08:00 +0100 | [diff] [blame] | 30 | |
| 31 | |
| 32 | class Loggable: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame^] | 33 | def __init__(self, log, log_to_console: bool = False, prefix: str = ""): |
| quilesj | a049b7c | 2019-10-28 18:08:00 +0100 | [diff] [blame] | 34 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame^] | 35 | self._last_log_time = None # used for time increment in logging |
| quilesj | a049b7c | 2019-10-28 18:08:00 +0100 | [diff] [blame] | 36 | self._log_to_console = log_to_console |
| 37 | self._prefix = prefix |
| 38 | if log is not None: |
| 39 | self.log = log |
| 40 | else: |
| 41 | self.log = logging.getLogger(__name__) |
| 42 | |
| 43 | def debug(self, msg: str): |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame^] | 44 | self._log_msg(log_level="DEBUG", msg=msg) |
| quilesj | a049b7c | 2019-10-28 18:08:00 +0100 | [diff] [blame] | 45 | |
| 46 | def info(self, msg: str): |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame^] | 47 | self._log_msg(log_level="INFO", msg=msg) |
| quilesj | a049b7c | 2019-10-28 18:08:00 +0100 | [diff] [blame] | 48 | |
| 49 | def warning(self, msg: str): |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame^] | 50 | self._log_msg(log_level="WARNING", msg=msg) |
| quilesj | a049b7c | 2019-10-28 18:08:00 +0100 | [diff] [blame] | 51 | |
| 52 | def error(self, msg: str): |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame^] | 53 | self._log_msg(log_level="ERROR", msg=msg) |
| quilesj | a049b7c | 2019-10-28 18:08:00 +0100 | [diff] [blame] | 54 | |
| 55 | def critical(self, msg: str): |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame^] | 56 | self._log_msg(log_level="CRITICAL", msg=msg) |
| quilesj | a049b7c | 2019-10-28 18:08:00 +0100 | [diff] [blame] | 57 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame^] | 58 | #################################################################################### |
| quilesj | a049b7c | 2019-10-28 18:08:00 +0100 | [diff] [blame] | 59 | |
| 60 | def _log_msg(self, log_level: str, msg: str): |
| 61 | """Generic log method""" |
| 62 | msg = self._format_log( |
| 63 | log_level=log_level, |
| 64 | msg=msg, |
| 65 | obj=self, |
| 66 | level=3, |
| 67 | include_path=False, |
| 68 | include_thread=False, |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame^] | 69 | include_coroutine=True, |
| quilesj | a049b7c | 2019-10-28 18:08:00 +0100 | [diff] [blame] | 70 | ) |
| 71 | if self._log_to_console: |
| 72 | print(msg) |
| 73 | else: |
| 74 | if self.log is not None: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame^] | 75 | if log_level == "DEBUG": |
| quilesj | a049b7c | 2019-10-28 18:08:00 +0100 | [diff] [blame] | 76 | self.log.debug(msg) |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame^] | 77 | elif log_level == "INFO": |
| quilesj | a049b7c | 2019-10-28 18:08:00 +0100 | [diff] [blame] | 78 | self.log.info(msg) |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame^] | 79 | elif log_level == "WARNING": |
| quilesj | a049b7c | 2019-10-28 18:08:00 +0100 | [diff] [blame] | 80 | self.log.warning(msg) |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame^] | 81 | elif log_level == "ERROR": |
| quilesj | a049b7c | 2019-10-28 18:08:00 +0100 | [diff] [blame] | 82 | self.log.error(msg) |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame^] | 83 | elif log_level == "CRITICAL": |
| quilesj | a049b7c | 2019-10-28 18:08:00 +0100 | [diff] [blame] | 84 | self.log.critical(msg) |
| 85 | |
| 86 | def _format_log( |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame^] | 87 | self, |
| 88 | log_level: str, |
| 89 | msg: str = "", |
| 90 | obj: object = None, |
| 91 | level: int = None, |
| 92 | include_path: bool = False, |
| 93 | include_thread: bool = False, |
| 94 | include_coroutine: bool = True, |
| quilesj | a049b7c | 2019-10-28 18:08:00 +0100 | [diff] [blame] | 95 | ) -> str: |
| 96 | |
| 97 | # time increment from last log |
| 98 | now = time.perf_counter() |
| 99 | if self._last_log_time is None: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame^] | 100 | time_str = " (+0.000)" |
| quilesj | a049b7c | 2019-10-28 18:08:00 +0100 | [diff] [blame] | 101 | else: |
| 102 | diff = round(now - self._last_log_time, 3) |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame^] | 103 | time_str = " (+{})".format(diff) |
| quilesj | a049b7c | 2019-10-28 18:08:00 +0100 | [diff] [blame] | 104 | self._last_log_time = now |
| 105 | |
| 106 | if level is None: |
| 107 | level = 1 |
| 108 | |
| 109 | # stack info |
| 110 | fi = inspect.stack()[level] |
| 111 | filename = fi.filename |
| 112 | func = fi.function |
| 113 | lineno = fi.lineno |
| 114 | # filename without path |
| 115 | if not include_path: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame^] | 116 | i = filename.rfind("/") |
| quilesj | a049b7c | 2019-10-28 18:08:00 +0100 | [diff] [blame] | 117 | if i > 0: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame^] | 118 | filename = filename[i + 1 :] |
| quilesj | a049b7c | 2019-10-28 18:08:00 +0100 | [diff] [blame] | 119 | |
| 120 | # datetime |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame^] | 121 | dt = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f") |
| quilesj | a049b7c | 2019-10-28 18:08:00 +0100 | [diff] [blame] | 122 | dt = dt + time_str |
| quilesj | ac4e0de | 2019-11-27 16:12:02 +0000 | [diff] [blame] | 123 | # dt = time_str # logger already shows datetime |
| quilesj | a049b7c | 2019-10-28 18:08:00 +0100 | [diff] [blame] | 124 | |
| 125 | # current thread |
| 126 | if include_thread: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame^] | 127 | thread_name = "th:{}".format(threading.current_thread().getName()) |
| quilesj | a049b7c | 2019-10-28 18:08:00 +0100 | [diff] [blame] | 128 | else: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame^] | 129 | thread_name = "" |
| quilesj | a049b7c | 2019-10-28 18:08:00 +0100 | [diff] [blame] | 130 | |
| 131 | # current coroutine |
| 132 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame^] | 133 | coroutine_id = "" |
| quilesj | a049b7c | 2019-10-28 18:08:00 +0100 | [diff] [blame] | 134 | if include_coroutine: |
| 135 | try: |
| 136 | if asyncio.Task.current_task() is not None: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame^] | 137 | |
| quilesj | a049b7c | 2019-10-28 18:08:00 +0100 | [diff] [blame] | 138 | def print_cor_name(c): |
| 139 | import inspect |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame^] | 140 | |
| quilesj | a049b7c | 2019-10-28 18:08:00 +0100 | [diff] [blame] | 141 | try: |
| 142 | for m in inspect.getmembers(c): |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame^] | 143 | if m[0] == "__name__": |
| quilesj | a049b7c | 2019-10-28 18:08:00 +0100 | [diff] [blame] | 144 | return m[1] |
| 145 | except Exception: |
| 146 | pass |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame^] | 147 | |
| quilesj | a049b7c | 2019-10-28 18:08:00 +0100 | [diff] [blame] | 148 | coro = asyncio.Task.current_task()._coro |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame^] | 149 | coroutine_id = "coro-{} {}()".format( |
| 150 | hex(id(coro))[2:], print_cor_name(coro) |
| 151 | ) |
| quilesj | a049b7c | 2019-10-28 18:08:00 +0100 | [diff] [blame] | 152 | except Exception: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame^] | 153 | coroutine_id = "" |
| quilesj | a049b7c | 2019-10-28 18:08:00 +0100 | [diff] [blame] | 154 | |
| 155 | # classname |
| 156 | if obj is not None: |
| 157 | obj_type = obj.__class__.__name__ # type: str |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame^] | 158 | log_msg = "{} {} {} {} {}::{}.{}():{}\n{}".format( |
| 159 | self._prefix, |
| 160 | dt, |
| 161 | thread_name, |
| 162 | coroutine_id, |
| 163 | filename, |
| 164 | obj_type, |
| 165 | func, |
| 166 | lineno, |
| 167 | str(msg), |
| 168 | ) |
| quilesj | a049b7c | 2019-10-28 18:08:00 +0100 | [diff] [blame] | 169 | else: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame^] | 170 | log_msg = "{} {} {} {} {}::{}():{}\n{}".format( |
| 171 | self._prefix, |
| 172 | dt, |
| 173 | thread_name, |
| 174 | coroutine_id, |
| 175 | filename, |
| 176 | func, |
| 177 | lineno, |
| 178 | str(msg), |
| 179 | ) |
| quilesj | a049b7c | 2019-10-28 18:08:00 +0100 | [diff] [blame] | 180 | |
| 181 | return log_msg |