Coverage for tests/test_log.py : 96%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
# LSST Data Management System # Copyright 2014-2017 LSST Corporation. # # This product includes software developed by the # LSST Project (http://www.lsst.org/). # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the LSST License Statement and # the GNU General Public License along with this program. If not, # see <http://www.lsstcorp.org/LegalNotices/>.
This tests the logging system in a variety of ways. """
""" Context manager to redirect stdout to a file. """
"""Make a temporary directory and a log file in it."""
"""Remove the temporary directory."""
""" Create a configuration file in the temporary directory and populate it with the provided string. """
"""Compare the log file with the provided reference text.""" # strip everything up to first ] to remove timestamp and thread ID
###############################################################################
"""Check the default root logger name."""
""" Test basic log output with default configuration. Since the default threshold is INFO, the DEBUG or TRACE message is not emitted. """ root INFO: This is INFO root INFO: This is unicode INFO root WARN: This is WARN root ERROR: This is ERROR root FATAL: This is FATAL root WARN: Format 3 2.71828 foo """)
""" Test basic log output with default configuration but using the f variants. Since the default threshold is INFO, the DEBUG or TRACE message is not emitted. """ "This is {{INFO}} Item 1: {item[1]}", item=["a", "b", "c"]) 4, 3, place="LSST") root INFO: This is {INFO} Item 1: b root INFO: This is {unicode} INFO root WARN: This is WARN Tucson root ERROR: This is ERROR 1->2 root FATAL: This is FATAL 3 out of 4 times for LSST root WARN: Format 3 2.71828 foo """)
"""Test the log context/component stack."""
root INFO: This is INFO root DEBUG: This is DEBUG component INFO: This is INFO component DEBUG: This is DEBUG root INFO: This is INFO 2 root DEBUG: This is DEBUG 2 comp INFO: This is INFO 3 comp DEBUG: This is DEBUG 3 comp INFO: This is INFO 3a comp.subcomp TRACE: This is TRACE 4 comp.subcomp INFO: This is INFO 4 comp.subcomp DEBUG: This is DEBUG 4 comp INFO: This is INFO 5 """)
""" Test a complex pattern for log messages, including Mapped Diagnostic Context (MDC). """ log4j.rootLogger=DEBUG, CA log4j.appender.CA=ConsoleAppender log4j.appender.CA.layout=PatternLayout log4j.appender.CA.layout.ConversionPattern=%-5p %c %C %M (%F:%L) %l - %m - %X%n """)
# Use format to make line numbers easier to change. INFO root testPattern (test_log.py:{0[0]}) test_log.py({0[0]}) - This is INFO - {{}} DEBUG root testPattern (test_log.py:{0[1]}) test_log.py({0[1]}) - This is DEBUG - {{}} INFO root testPattern (test_log.py:{0[2]}) test_log.py({0[2]}) - This is INFO 2 - {{{{x,3}}{{y,foo}}{{z,<class '{1}.TestLog'>}}}} DEBUG root testPattern (test_log.py:{0[3]}) test_log.py({0[3]}) - This is DEBUG 2 - {{{{x,3}}{{y,foo}}{{z,<class '{1}.TestLog'>}}}} INFO component testPattern (test_log.py:{0[4]}) test_log.py({0[4]}) - This is INFO 3 - {{{{x,3}}{{y,foo}}}} DEBUG component testPattern (test_log.py:{0[5]}) test_log.py({0[5]}) - This is DEBUG 3 - {{{{x,3}}{{y,foo}}}} INFO component testPattern (test_log.py:{0[6]}) test_log.py({0[6]}) - This is INFO 4 - {{{{y,foo}}}} DEBUG component testPattern (test_log.py:{0[7]}) test_log.py({0[7]}) - This is DEBUG 4 - {{{{y,foo}}}} INFO root testPattern (test_log.py:{0[8]}) test_log.py({0[8]}) - This is INFO 5 - {{{{y,foo}}}} DEBUG root testPattern (test_log.py:{0[9]}) test_log.py({0[9]}) - This is DEBUG 5 - {{{{y,foo}}}} """.format([x + 209 for x in (0, 1, 8, 9, 14, 15, 18, 19, 22, 23)], __name__)) # noqa E501 line too long
""" Test add of PID Mapped Diagnostic Context (MDC). """
log4j.rootLogger=DEBUG, CA log4j.appender.CA=ConsoleAppender log4j.appender.CA.layout=PatternLayout log4j.appender.CA.layout.ConversionPattern=%-5p PID:%X{{PID}} %c %C %M (%F:%L) %l - %m%n """) # noqa E501 line too long
self.tempDir = tempfile.mkdtemp() self.outputFilename = os.path.join(self.tempDir, "log-child.out") msg += " in child process" "Child returns incorrect code")
finally:
# Use format to make line numbers easier to change. INFO PID:{1} root testMDCPutPid (test_log.py:{0}) test_log.py({0}) - {2} """.format(line, os.getpid(), msg))
# don't pass other tests in child process os._exit(0)
"""Test configuring logging to go to a file.""" log4j.rootLogger=DEBUG, FA log4j.appender.FA=FileAppender log4j.appender.FA.file={0} log4j.appender.FA.layout=SimpleLayout """)
INFO - This is INFO DEBUG - This is DEBUG """)
"""Test logging through the Python logging interface."""
root INFO: This is INFO """)
"INFO - main thread {{MDC_INIT,OK}}\n" + \ "INFO - thread 1 {{MDC_INIT,OK}}\n" + \ "INFO - thread 2 {{MDC_INIT,OK}}\n"
log4j.rootLogger=DEBUG, CA log4j.appender.CA=ConsoleAppender log4j.appender.CA.layout=PatternLayout log4j.appender.CA.layout.ConversionPattern=%-5p - %m %X%n """)
"""Test for overwriting MDC. """
"INFO - Message one {}\n" \ "INFO - Message two {{LABEL,123456}}\n" \ "INFO - Message three {{LABEL,654321}}\n" \ "INFO - Message four {}\n"
log4j.rootLogger=DEBUG, CA log4j.appender.CA=ConsoleAppender log4j.appender.CA.layout=PatternLayout log4j.appender.CA.layout.ConversionPattern=%-5p - %m %X%n """)
"""Test log.lwpID() method."""
""" Test log object. """ b INFO: This is INFO b WARN: This is WARN b ERROR: This is ERROR b FATAL: This is FATAL b WARN: Format 3 2.71828 foo """)
""" Test levels of Log objects """ log4j.rootLogger=TRACE, CA log4j.appender.CA=ConsoleAppender log4j.appender.CA.layout=PatternLayout log4j.appender.CA.layout.ConversionPattern=%-5p %c (%F)- %m%n """) log.TRACE)
TRACE a.b (test_log.py)- This is TRACE INFO a.b (test_log.py)- This is INFO FATAL a.b (test_log.py)- Format 3 2.71828 foo WARN a.b.c (test_log.py)- This is WARN ERROR a.b.c (test_log.py)- This is ERROR FATAL a.b.c (test_log.py)- This is FATAL INFO a.b.c (test_log.py)- Format 3 2.71828 foo """)
"""Test logging messages containing %s (DM-7509) """ root INFO: INFO with %s root WARN: WARN with %s root ERROR: ERROR with %s root FATAL: FATAL with %s root DEBUG: DEBUG with %s """)
unittest.main() |