Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 1 | # 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. |
| 14 | import functools |
| 15 | import logging |
| 16 | import os |
| 17 | import traceback |
| 18 | |
| 19 | from tcp_tests import settings |
| 20 | |
| 21 | if not os.path.exists(settings.LOGS_DIR): |
| 22 | os.makedirs(settings.LOGS_DIR) |
| 23 | |
| 24 | logging.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 | |
| 30 | console = logging.StreamHandler() |
| 31 | console.setLevel(logging.INFO) |
| 32 | formatter = logging.Formatter('%(asctime)s - %(levelname)s %(filename)s:' |
| 33 | '%(lineno)d -- %(message)s') |
| 34 | console.setFormatter(formatter) |
| 35 | |
| 36 | logger = logging.getLogger(__name__) |
| 37 | logger.addHandler(console) |
| 38 | |
| 39 | |
| 40 | # suppress iso8601 and paramiko debug logging |
| 41 | class NoDebugMessageFilter(logging.Filter): |
| 42 | def filter(self, record): |
| 43 | return not record.levelno <= logging.DEBUG |
| 44 | |
| 45 | logging.getLogger('paramiko.transport').addFilter(NoDebugMessageFilter()) |
| 46 | logging.getLogger('paramiko.hostkeys').addFilter(NoDebugMessageFilter()) |
| 47 | logging.getLogger('iso8601.iso8601').addFilter(NoDebugMessageFilter()) |
| 48 | |
| 49 | |
| 50 | def 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 | |
| 73 | logwrap = debug(logger) |