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
7 # http://www.apache.org/licenses/LICENSE-2.0
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)
28 def log_enter_exit(func):
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__,
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__,
51 def for_all_methods(decorator):
52 # @for_all_methods(log_enter_exit)
56 for attr in cls.__dict__:
57 if callable(getattr(cls, attr)):
58 setattr(cls, attr, decorator(getattr(cls, attr)))
63 def dict_to_nice_string(dict):
65 for key, value in dict.iteritems():
66 return_string.append('%s: %s' % (key, value))
67 return ', '.join(return_string)