Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DM-17741: Factor out code to convert between logging levels #38

Merged
merged 2 commits into from
Feb 7, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 24 additions & 12 deletions python/lsst/log/log/logContinued.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,8 @@ def _log(self, level, use_format, fmt, *args, **kwargs):
msg = fmt % args if args else fmt
if self.UsePythonLogging:
pylog = logging.getLogger(self.getName())
# Python logging level is 1000 times smaller than log4cxx level
record = logging.LogRecord(self.getName(), int(level/1000), filename,
frame.f_lineno, msg, None, False, func=funcname)
record = logging.LogRecord(self.getName(), LevelTranslator.lsstLog2logging(level),
filename, frame.f_lineno, msg, None, False, func=funcname)
pylog.handle(record)
else:
self.logMsg(level, filename, funcname, frame.f_lineno, msg)
Expand Down Expand Up @@ -297,6 +296,26 @@ def __exit__(self, exc_type, exc_value, traceback):
Log.UsePythonLogging = self.current


class LevelTranslator:
"""Helper class to translate levels between lsst.log and Python logging.
"""
@staticmethod
def lsstLog2logging(level):
"""Translates from lsst.log/log4cxx levels to logging module levels.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use numpydoc here please and use backtick markup around logging so it goes to the logging docs. We should start trying to switch this module to sphinx.

"""
# Python logging levels are same as lsst.log divided by 1000,
# logging does not have TRACE level by default but it is OK to use
# that numeric level and we may even add TRACE later.
return level//1000

@staticmethod
def logging2lsstLog(level):
"""Translates from standard python logging module levels
to standard lsst.log/log4cxx levels.
"""
return level*1000


class LogHandler(logging.Handler):
"""Handler for Python logging module that emits to LSST logging.

Expand All @@ -320,7 +339,7 @@ def __init__(self, level=logging.NOTSET):

def handle(self, record):
logger = Log.getLogger(record.name)
if logger.isEnabledFor(self.translateLevel(record.levelno)):
if logger.isEnabledFor(LevelTranslator.logging2lsstLog(record.levelno)):
logging.Handler.handle(self, record)

def emit(self, record):
Expand Down Expand Up @@ -355,13 +374,6 @@ def emit(self, record):
# Use standard formatting class to format message part of the record
message = self.formatter.format(record)

logger.logMsg(self.translateLevel(record.levelno),
logger.logMsg(LevelTranslator.logging2lsstLog(record.levelno),
record.filename, record.funcName,
record.lineno, message)

def translateLevel(self, levelno):
"""
Translates from standard python logging module levels
to standard log4cxx levels.
"""
return levelno*1000
15 changes: 15 additions & 0 deletions tests/test_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,21 @@ def testForwardToPythonContextManager(self):
# Verify that forwarding is disabled
self.assertFalse(log.Log.UsePythonLogging)

def testLevelTranslator(self):
"""Test LevelTranslator class
"""
# correspondence between levels, logging has no TRACE but we accept
# small integer in its place
levelMap = ((log.TRACE, 5),
(log.DEBUG, logging.DEBUG),
(log.INFO, logging.INFO),
(log.WARN, logging.WARNING),
(log.ERROR, logging.ERROR),
(log.FATAL, logging.FATAL))
for logLevel, loggingLevel in levelMap:
self.assertEqual(log.LevelTranslator.lsstLog2logging(logLevel), loggingLevel)
self.assertEqual(log.LevelTranslator.logging2lsstLog(loggingLevel), logLevel)


if __name__ == "__main__":
unittest.main()