change cloner info path
[sdnvpn.git] / odl-pipeline / lib / utils / utils_log.py
1 #
2 # Copyright (c) 2015 All rights reserved
3 # This program and the accompanying materials
4 # are made available under the terms of the Apache License, Version 2.0
5 # which accompanies this distribution, and is available at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 #
10 import logging
11 import datetime
12 import os
13 import sys
14
15 LOG = logging.getLogger(__name__)
16 LOG_LEVEL = logging.DEBUG
17 LOG_PATH = "./tmp/%s.log" % os.path.basename(sys.argv[0])
18 logging.basicConfig(format='%(asctime)s %(levelname)s: %(message)s',
19                     filename=LOG_PATH, level=LOG_LEVEL)
20 #                    datefmt='%Y-%m-%dT:%H:%M:%s', level=LOG_LEVEL)
21 console = logging.StreamHandler()
22 console.setLevel(logging.INFO)
23 formatter = logging.Formatter('%(asctime)s %(levelname)s: %(message)s')
24 console.setFormatter(formatter)
25 LOG.addHandler(console)
26
27
28 def log_enter_exit(func):
29
30     def inner(self, *args, **kwargs):
31         LOG.debug(("Entering %(cls)s.%(method)s "
32                    "args: %(args)s, kwargs: %(kwargs)s") %
33                   {'cls': self.__class__.__name__,
34                    'method': func.__name__,
35                    'args': args,
36                    'kwargs': kwargs})
37         start = datetime.datetime.now()
38         ret = func(self, *args, **kwargs)
39         end = datetime.datetime.now()
40         LOG.debug(("Exiting %(cls)s.%(method)s. "
41                    "Spent %(duration)s sec. "
42                    "Return %(return)s") %
43                   {'cls': self.__class__.__name__,
44                    'duration': end - start,
45                    'method': func.__name__,
46                    'return': ret})
47         return ret
48     return inner
49
50
51 def for_all_methods(decorator):
52     # @for_all_methods(log_enter_exit)
53     # class ...
54
55     def decorate(cls):
56         for attr in cls.__dict__:
57             if callable(getattr(cls, attr)):
58                 setattr(cls, attr, decorator(getattr(cls, attr)))
59         return cls
60     return decorate
61
62
63 def dict_to_nice_string(dict):
64     return_string = []
65     for key, value in dict.iteritems():
66         return_string.append('%s: %s' % (key, value))
67     return ', '.join(return_string)