forked from openhab-scripters/openhab-helper-libraries
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.py
42 lines (36 loc) · 1.26 KB
/
log.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import logging
import functools
import traceback
from org.slf4j import Logger, LoggerFactory
LOG_PREFIX = "org.eclipse.smarthome.automation.rules"
def log_traceback(fn):
"""Decorator to provide better Jython stack traces"""
functools.wraps(fn)
def wrapper(*args, **kwargs):
try:
return fn(*args, **kwargs)
except Exception as ex:
if len(args) > 0 and hasattr(args[0], "log"):
args[0].log.error(traceback.format_exc())
else:
print traceback.format_exc()
return wrapper
class Slf4jHandler(logging.Handler):
def emit(self, record):
message = self.format(record)
logger_name = record.name
if record.name == "root":
logger_name = Logger.ROOT_LOGGER_NAME
logger = LoggerFactory.getLogger(logger_name)
level = record.levelno
if level == logging.DEBUG:
logger.debug(message)
elif level == logging.INFO:
logger.info(message)
elif level == logging.WARN:
logger.warn(message)
elif level in [logging.ERROR, logging.CRITICAL] :
logger.error(message)
handler = Slf4jHandler()
logging.root.setLevel(logging.DEBUG)
logging.root.handlers = [handler]