blob: 2c733642ef58a1eaa82e167195bd6a20cd4ea047 [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
Dina Belovae6fdffb2017-09-19 13:58:34 -070045
Dennis Dmitriev6f59add2016-10-18 13:45:27 +030046logging.getLogger('paramiko.transport').addFilter(NoDebugMessageFilter())
47logging.getLogger('paramiko.hostkeys').addFilter(NoDebugMessageFilter())
48logging.getLogger('iso8601.iso8601').addFilter(NoDebugMessageFilter())
49
50
51def debug(logger):
52 def wrapper(func):
53 @functools.wraps(func)
54 def wrapped(*args, **kwargs):
55 logger.debug(
56 "Calling: {} with args: {} {}".format(
57 func.__name__, args, kwargs
58 )
59 )
60 try:
61 result = func(*args, **kwargs)
62 logger.debug(
63 "Done: {} with result: {}".format(func.__name__, result))
64 except BaseException as e:
65 logger.error(
66 '{func} raised: {exc!r}\n'
67 'Traceback: {tb!s}'.format(
68 func=func.__name__, exc=e, tb=traceback.format_exc()))
69 raise
70 return result
71 return wrapped
72 return wrapper
73
Dina Belovae6fdffb2017-09-19 13:58:34 -070074
Dennis Dmitriev6f59add2016-10-18 13:45:27 +030075logwrap = debug(logger)