2 # Copyright 2019 Telefonica Investigacion y Desarrollo, S.A.U.
3 # This file is part of OSM
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
10 # http://www.apache.org/licenses/LICENSE-2.0
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
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
19 # For those usages not covered by the Apache License, Version 2.0 please
20 # contact with: nfvlabs@tid.es
28 import threading
# only for logging purposes (not for using threads)
33 def __init__(self
, log
, log_to_console
: bool = False, prefix
: str = ""):
34 self
._last
_log
_time
= None # used for time increment in logging
35 self
._log
_to
_console
= log_to_console
40 self
.log
= logging
.getLogger(__name__
)
42 def debug(self
, msg
: str):
43 self
._log
_msg
(log_level
="DEBUG", msg
=msg
)
45 def info(self
, msg
: str):
46 self
._log
_msg
(log_level
="INFO", msg
=msg
)
48 def warning(self
, msg
: str):
49 self
._log
_msg
(log_level
="WARNING", msg
=msg
)
51 def error(self
, msg
: str):
52 self
._log
_msg
(log_level
="ERROR", msg
=msg
)
54 def critical(self
, msg
: str):
55 self
._log
_msg
(log_level
="CRITICAL", msg
=msg
)
57 ####################################################################################
59 def _log_msg(self
, log_level
: str, msg
: str):
60 """Generic log method"""
61 msg
= self
._format
_log
(
68 include_coroutine
=True,
70 if self
._log
_to
_console
:
73 if self
.log
is not None:
74 if log_level
== "DEBUG":
76 elif log_level
== "INFO":
78 elif log_level
== "WARNING":
80 elif log_level
== "ERROR":
82 elif log_level
== "CRITICAL":
83 self
.log
.critical(msg
)
91 include_path
: bool = False,
92 include_thread
: bool = False,
93 include_coroutine
: bool = True,
95 # time increment from last log
96 now
= time
.perf_counter()
97 if self
._last
_log
_time
is None:
98 time_str
= " (+0.000)"
100 diff
= round(now
- self
._last
_log
_time
, 3)
101 time_str
= " (+{})".format(diff
)
102 self
._last
_log
_time
= now
108 fi
= inspect
.stack()[level
]
109 filename
= fi
.filename
112 # filename without path
114 i
= filename
.rfind("/")
116 filename
= filename
[i
+ 1 :]
119 dt
= datetime
.datetime
.now().strftime("%Y-%m-%d %H:%M:%S.%f")
121 # dt = time_str # logger already shows datetime
125 thread_name
= "th:{}".format(threading
.current_thread().getName())
132 if include_coroutine
:
134 if asyncio
.Task
.current_task() is not None:
136 def print_cor_name(c
):
140 for m
in inspect
.getmembers(c
):
141 if m
[0] == "__name__":
146 coro
= asyncio
.Task
.current_task()._coro
147 coroutine_id
= "coro-{} {}()".format(
148 hex(id(coro
))[2:], print_cor_name(coro
)
155 obj_type
= obj
.__class
__.__name
__ # type: str
156 log_msg
= "{} {} {} {} {}::{}.{}():{}\n{}".format(
168 log_msg
= "{} {} {} {} {}::{}():{}\n{}".format(