blob: b9d0c5a7255903ed8abe1aaaebf769dfd2d3c0c6 [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',
27 filename=os.path.join(settings.LOGS_DIR, 'tests.log'),
28 filemode='w')
29
30console = logging.StreamHandler()
31console.setLevel(logging.INFO)
32formatter = logging.Formatter('%(asctime)s - %(levelname)s %(filename)s:'
33 '%(lineno)d -- %(message)s')
34console.setFormatter(formatter)
35
36logger = logging.getLogger(__name__)
37logger.addHandler(console)
38
39
40# suppress iso8601 and paramiko debug logging
41class NoDebugMessageFilter(logging.Filter):
42 def filter(self, record):
43 return not record.levelno <= logging.DEBUG
44
45logging.getLogger('paramiko.transport').addFilter(NoDebugMessageFilter())
46logging.getLogger('paramiko.hostkeys').addFilter(NoDebugMessageFilter())
47logging.getLogger('iso8601.iso8601').addFilter(NoDebugMessageFilter())
48
49
50def debug(logger):
51 def wrapper(func):
52 @functools.wraps(func)
53 def wrapped(*args, **kwargs):
54 logger.debug(
55 "Calling: {} with args: {} {}".format(
56 func.__name__, args, kwargs
57 )
58 )
59 try:
60 result = func(*args, **kwargs)
61 logger.debug(
62 "Done: {} with result: {}".format(func.__name__, result))
63 except BaseException as e:
64 logger.error(
65 '{func} raised: {exc!r}\n'
66 'Traceback: {tb!s}'.format(
67 func=func.__name__, exc=e, tb=traceback.format_exc()))
68 raise
69 return result
70 return wrapped
71 return wrapper
72
73logwrap = debug(logger)