diff --git a/logging/example_logging.py b/logging/example_logging.py index 2ba2f1a18..81e3ecad9 100644 --- a/logging/example_logging.py +++ b/logging/example_logging.py @@ -1,9 +1,40 @@ +import os +import time import logging + +def reset_log(log_file): + try: + os.remove(log_file) + except OSError: + pass + + +def stress_test(iterations): + sample = "All work and no play, makes Jack a dull boy!" + log_file = 'file.log' + reset_log(log_file) + logging.basicConfig(filename=log_file) + start_time = time.time() + for i in range(iterations): + logging.info("%d %s" % (i, sample)) + file_delta = time.time() - start_time + reset_log(log_file) + + logging.basicConfig(filename=None) + start_time = time.time() + for i in range(iterations): + logging.info("%d %s" % (i, sample)) + stdout_delta = time.time() - start_time + print("File logging time %f for %i iterations" % (file_delta, iterations)) + print("Stdout logging time %f for %i iterations" % (stdout_delta, iterations)) + + logging.basicConfig(level=logging.INFO) log = logging.getLogger("test") log.debug("Test message: %d(%s)", 100, "foobar") log.info("Test message2: %d(%s)", 100, "foobar") + log.warning("Test message3: %d(%s)") log.error("Test message4") log.critical("Test message5") diff --git a/logging/logging.py b/logging/logging.py index cea2de031..0cca9ec56 100644 --- a/logging/logging.py +++ b/logging/logging.py @@ -38,11 +38,16 @@ def isEnabledFor(self, level): def log(self, level, msg, *args): if level >= (self.level or _level): - _stream.write("%s:%s:" % (self._level_str(level), self.name)) - if not args: - print(msg, file=_stream) + if _log_file: + _log_file.write(("%s:%s:" + msg + "\n") % ((self._level_str(level), self.name) + args)) + _log_file.flush() else: - print(msg % args, file=_stream) + _stream.write("%s:%s:" % (self._level_str(level), self.name)) + if not args: + print(msg, file=_stream) + else: + print(msg % args, file=_stream) + def debug(self, msg, *args): self.log(DEBUG, msg, *args) @@ -69,6 +74,7 @@ def exception(self, msg, *args): _level = INFO _loggers = {} +_log_file = None def getLogger(name): if name in _loggers: @@ -84,11 +90,12 @@ def debug(msg, *args): getLogger(None).debug(msg, *args) def basicConfig(level=INFO, filename=None, stream=None, format=None): - global _level, _stream + global _level, _stream, _log_file _level = level - if stream: - _stream = stream - if filename is not None: - print("logging.basicConfig: filename arg is not supported") + if filename: + _log_file = open(filename, 'a') + else: + if stream: + _stream = stream if format is not None: print("logging.basicConfig: format arg is not supported") diff --git a/logging/test_logging.py b/logging/test_logging.py new file mode 100644 index 000000000..8ff6736a2 --- /dev/null +++ b/logging/test_logging.py @@ -0,0 +1,37 @@ +import logging +import os + + +def reset_log(log_file): + try: + os.remove(log_file) + except OSError: + pass + + +def test_basicConfig(): + print("Testing logging.basicConfig") + log_file = 'file.log' + log_message = '123' + + reset_log(log_file) + logging.basicConfig(filename=log_file) + logging.info(log_message) + + with open(log_file, 'r') as logf: + assert log_message in logf.read() + + print("Success: Testing logging.basicConfig") + + +tests = [test_basicConfig] + + +def test(): + print("Running %i tests." % len(tests)) + for test_function in tests: + test_function() + + +if __name__ == '__main__': + test()
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: