Skip to content

Commit

Permalink
[INDY-1102] Added logging configure (#555)
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Nikitin <[email protected]>
  • Loading branch information
anikitinDSR authored and ashcherbakov committed Mar 7, 2018
1 parent a51f6bc commit b60f16f
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions stp_core/common/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,7 @@ def __init__(self, config=None):
self._config = config or getConfig()
self._addTraceToLogging()
self._addDisplayToLogging()

self._handlers = {}
self._format = logging.Formatter(fmt=self._config.logFormat,
style=self._config.logFormatStyle)

if self._config.enableStdOutLogging:
self.enableStdLogging()

logLevel = logging.INFO
if hasattr(self._config, "logLevel"):
logLevel = self._config.logLevel
self.setLogLevel(logLevel)
self.apply_config(self._config)

@staticmethod
def getlogger(name=None):
Expand All @@ -59,6 +48,23 @@ def getlogger(name=None):
def setLogLevel(log_level):
logging.root.setLevel(log_level)

def apply_config(self, config):
assert config

self._config = config
self._handlers = {}
self._clearAllHandlers()
self._format = logging.Formatter(fmt=self._config.logFormat,
style=self._config.logFormatStyle)

if self._config.enableStdOutLogging:
self.enableStdLogging()

logLevel = logging.INFO
if hasattr(self._config, "logLevel"):
logLevel = self._config.logLevel
self.setLogLevel(logLevel)

def enableStdLogging(self):
# only enable if CLI is not
if 'cli' in self._handlers:
Expand Down Expand Up @@ -101,6 +107,10 @@ def _clearHandler(self, typ: str):
if old:
logging.root.removeHandler(old)

def _clearAllHandlers(self):
for hdlr in logging.root.handlers:
logging.root.removeHandler(hdlr)

@staticmethod
def _addTraceToLogging():
logging.addLevelName(TRACE_LOG_LEVEL, "TRACE")
Expand Down

0 comments on commit b60f16f

Please sign in to comment.