d588a1d2a9fb36982dacb1b1a1347bec1fb009f5
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 = ""):
35 self
._last
_log
_time
= None # used for time increment in logging
36 self
._log
_to
_console
= log_to_console
41 self
.log
= logging
.getLogger(__name__
)
43 def debug(self
, msg
: str):
44 self
._log
_msg
(log_level
="DEBUG", msg
=msg
)
46 def info(self
, msg
: str):
47 self
._log
_msg
(log_level
="INFO", msg
=msg
)
49 def warning(self
, msg
: str):
50 self
._log
_msg
(log_level
="WARNING", msg
=msg
)
52 def error(self
, msg
: str):
53 self
._log
_msg
(log_level
="ERROR", msg
=msg
)
55 def critical(self
, msg
: str):
56 self
._log
_msg
(log_level
="CRITICAL", msg
=msg
)
58 ####################################################################################
60 def _log_msg(self
, log_level
: str, msg
: str):
61 """Generic log method"""
62 msg
= self
._format
_log
(
69 include_coroutine
=True,
71 if self
._log
_to
_console
:
74 if self
.log
is not None:
75 if log_level
== "DEBUG":
77 elif log_level
== "INFO":
79 elif log_level
== "WARNING":
81 elif log_level
== "ERROR":
83 elif log_level
== "CRITICAL":
84 self
.log
.critical(msg
)
92 include_path
: bool = False,
93 include_thread
: bool = False,
94 include_coroutine
: bool = True,
97 # time increment from last log
98 now
= time
.perf_counter()
99 if self
._last
_log
_time
is None:
100 time_str
= " (+0.000)"
102 diff
= round(now
- self
._last
_log
_time
, 3)
103 time_str
= " (+{})".format(diff
)
104 self
._last
_log
_time
= now
110 fi
= inspect
.stack()[level
]
111 filename
= fi
.filename
114 # filename without path
116 i
= filename
.rfind("/")
118 filename
= filename
[i
+ 1 :]
121 dt
= datetime
.datetime
.now().strftime("%Y-%m-%d %H:%M:%S.%f")
123 # dt = time_str # logger already shows datetime
127 thread_name
= "th:{}".format(threading
.current_thread().getName())
134 if include_coroutine
:
136 if asyncio
.Task
.current_task() is not None:
138 def print_cor_name(c
):
142 for m
in inspect
.getmembers(c
):
143 if m
[0] == "__name__":
148 coro
= asyncio
.Task
.current_task()._coro
149 coroutine_id
= "coro-{} {}()".format(
150 hex(id(coro
))[2:], print_cor_name(coro
)
157 obj_type
= obj
.__class
__.__name
__ # type: str
158 log_msg
= "{} {} {} {} {}::{}.{}():{}\n{}".format(
170 log_msg
= "{} {} {} {} {}::{}():{}\n{}".format(