summaryrefslogtreecommitdiffstats
path: root/odl-pipeline/lib/utils/utils_log.py
diff options
context:
space:
mode:
authorNikolas Hermanns <nikolas.hermanns@ericsson.com>2016-12-13 10:26:11 +0000
committerGerrit Code Review <gerrit@opnfv.org>2016-12-13 10:26:11 +0000
commit2dd53e9153a37a5a9f14a8772224dbdaa091bbfa (patch)
tree7e083f4959878d78f943c07cc32575c66a4ba05d /odl-pipeline/lib/utils/utils_log.py
parent2445ba0a0325611e0b927ecc7ed4d361426e5fc4 (diff)
parent416f9cf177801cecfe39f5249d73e3a4a85f0bbd (diff)
Merge "adding odl-pipeline"
Diffstat (limited to 'odl-pipeline/lib/utils/utils_log.py')
-rwxr-xr-xodl-pipeline/lib/utils/utils_log.py67
1 files changed, 67 insertions, 0 deletions
diff --git a/odl-pipeline/lib/utils/utils_log.py b/odl-pipeline/lib/utils/utils_log.py
new file mode 100755
index 0000000..e49434c
--- /dev/null
+++ b/odl-pipeline/lib/utils/utils_log.py
@@ -0,0 +1,67 @@
+#
+# Copyright (c) 2015 All rights reserved
+# This program and the accompanying materials
+# are made available under the terms of the Apache License, Version 2.0
+# which accompanies this distribution, and is available at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+#
+import logging
+import datetime
+import os
+import sys
+
+LOG = logging.getLogger(__name__)
+LOG_LEVEL = logging.DEBUG
+LOG_PATH = "./tmp/%s.log" % os.path.basename(sys.argv[0])
+logging.basicConfig(format='%(asctime)s %(levelname)s: %(message)s',
+ filename=LOG_PATH, level=LOG_LEVEL)
+# datefmt='%Y-%m-%dT:%H:%M:%s', level=LOG_LEVEL)
+console = logging.StreamHandler()
+console.setLevel(logging.INFO)
+formatter = logging.Formatter('%(asctime)s %(levelname)s: %(message)s')
+console.setFormatter(formatter)
+LOG.addHandler(console)
+
+
+def log_enter_exit(func):
+
+ def inner(self, *args, **kwargs):
+ LOG.debug(("Entering %(cls)s.%(method)s "
+ "args: %(args)s, kwargs: %(kwargs)s") %
+ {'cls': self.__class__.__name__,
+ 'method': func.__name__,
+ 'args': args,
+ 'kwargs': kwargs})
+ start = datetime.datetime.now()
+ ret = func(self, *args, **kwargs)
+ end = datetime.datetime.now()
+ LOG.debug(("Exiting %(cls)s.%(method)s. "
+ "Spent %(duration)s sec. "
+ "Return %(return)s") %
+ {'cls': self.__class__.__name__,
+ 'duration': end - start,
+ 'method': func.__name__,
+ 'return': ret})
+ return ret
+ return inner
+
+
+def for_all_methods(decorator):
+ # @for_all_methods(log_enter_exit)
+ # class ...
+
+ def decorate(cls):
+ for attr in cls.__dict__:
+ if callable(getattr(cls, attr)):
+ setattr(cls, attr, decorator(getattr(cls, attr)))
+ return cls
+ return decorate
+
+
+def dict_to_nice_string(dict):
+ return_string = []
+ for key, value in dict.iteritems():
+ return_string.append('%s: %s' % (key, value))
+ return ', '.join(return_string)