blob: 1d1c1f1b098ab66a35293299aed70a4264cc7acd [file] [log] [blame]
Dennis Dmitriev6f59add2016-10-18 13:45:27 +03001# Copyright 2016 Mirantis, Inc.
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may
4# not use this file except in compliance with the License. You may obtain
5# a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations
13# under the License.
14import functools
15import logging
16import os
17import traceback
18
19from tcp_tests import settings
20
21if not os.path.exists(settings.LOGS_DIR):
22 os.makedirs(settings.LOGS_DIR)
23
24logging.basicConfig(level=logging.DEBUG,
25 format='%(asctime)s - %(levelname)s %(filename)s:'
26 '%(lineno)d -- %(message)s',
Dennis Dmitriev27007322019-05-03 19:21:44 +030027 filename=os.path.join(settings.LOGS_DIR,
28 settings.LOG_NAME),
Dennis Dmitriev6f59add2016-10-18 13:45:27 +030029 filemode='w')
30
31console = logging.StreamHandler()
32console.setLevel(logging.INFO)
33formatter = logging.Formatter('%(asctime)s - %(levelname)s %(filename)s:'
34 '%(lineno)d -- %(message)s')
35console.setFormatter(formatter)
36
37logger = logging.getLogger(__name__)
38logger.addHandler(console)
39
40
41# suppress iso8601 and paramiko debug logging
42class NoDebugMessageFilter(logging.Filter):
43 def filter(self, record):
44 return not record.levelno <= logging.DEBUG
45
Dina Belovae6fdffb2017-09-19 13:58:34 -070046
Dennis Dmitriev6f59add2016-10-18 13:45:27 +030047logging.getLogger('paramiko.transport').addFilter(NoDebugMessageFilter())
48logging.getLogger('paramiko.hostkeys').addFilter(NoDebugMessageFilter())
49logging.getLogger('iso8601.iso8601').addFilter(NoDebugMessageFilter())
50
51
52def debug(logger):
53 def wrapper(func):
54 @functools.wraps(func)
55 def wrapped(*args, **kwargs):
56 logger.debug(
57 "Calling: {} with args: {} {}".format(
58 func.__name__, args, kwargs
59 )
60 )
61 try:
62 result = func(*args, **kwargs)
63 logger.debug(
64 "Done: {} with result: {}".format(func.__name__, result))
65 except BaseException as e:
66 logger.error(
67 '{func} raised: {exc!r}\n'
68 'Traceback: {tb!s}'.format(
69 func=func.__name__, exc=e, tb=traceback.format_exc()))
70 raise
71 return result
72 return wrapped
73 return wrapper
74
Dina Belovae6fdffb2017-09-19 13:58:34 -070075
Dennis Dmitriev6f59add2016-10-18 13:45:27 +030076logwrap = debug(logger)