From 6975d1e5e8963bfaa2822086d8a816b4ed499b75 Mon Sep 17 00:00:00 2001 From: bogdan-oprea Date: Sat, 5 May 2018 21:06:59 +0300 Subject: [PATCH 1/4] logging: add support for file logging and testing for it --- logging/logging.py | 18 ++++++++----- logging/test_logging.py | 58 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 6 deletions(-) create mode 100644 logging/test_logging.py diff --git a/logging/logging.py b/logging/logging.py index 1c3ef0d84..d9bfab5bf 100644 --- a/logging/logging.py +++ b/logging/logging.py @@ -30,7 +30,12 @@ def _level_str(self, level): def log(self, level, msg, *args): if level >= (self.level or _level): - print(("%s:%s:" + msg) % ((self._level_str(level), self.name) + args), file=_stream) + if _log_file: + # esp8266 requires that the file be closed + with open(_log_file, 'a') as log: + log.write(("%s:%s:" + msg + "\n") % ((self._level_str(level), self.name) + args)) + else: + print(("%s:%s:" + msg) % ((self._level_str(level), self.name) + args)) def debug(self, msg, *args): self.log(DEBUG, msg, *args) @@ -50,6 +55,7 @@ def critical(self, msg, *args): _level = INFO _loggers = {} +_log_file = None def getLogger(name): if name in _loggers: @@ -65,11 +71,11 @@ 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") + _log_file = filename + if not filename: + 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..7582b44cb --- /dev/null +++ b/logging/test_logging.py @@ -0,0 +1,58 @@ +import logging +import os +import time + + +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") + + +def stress_test(): + sample = "All work and no play, makes Jack a dull boy!" + log_file = 'file.log' + iterations = 100 + 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)) + +tests = [test_basicConfig] + + +def test(): + print("Running %i tests." % len(tests)) + for test_function in tests: + test_function() + + +if __name__ == '__main__': + test() From f6416a1f03a707c521c5e937f78e022dacf207a8 Mon Sep 17 00:00:00 2001 From: bogdan-oprea Date: Sun, 6 May 2018 14:44:22 +0300 Subject: [PATCH 2/4] logging: removing open/close operation on each file log entry --- logging/logging.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/logging/logging.py b/logging/logging.py index d9bfab5bf..02c63d323 100644 --- a/logging/logging.py +++ b/logging/logging.py @@ -31,9 +31,8 @@ def _level_str(self, level): def log(self, level, msg, *args): if level >= (self.level or _level): if _log_file: - # esp8266 requires that the file be closed - with open(_log_file, 'a') as log: - log.write(("%s:%s:" + msg + "\n") % ((self._level_str(level), self.name) + args)) + _log_file.write(("%s:%s:" + msg + "\n") % ((self._level_str(level), self.name) + args)) + _log_file.flush() else: print(("%s:%s:" + msg) % ((self._level_str(level), self.name) + args)) @@ -73,8 +72,9 @@ def debug(msg, *args): def basicConfig(level=INFO, filename=None, stream=None, format=None): global _level, _stream, _log_file _level = level - _log_file = filename - if not filename: + if filename: + _log_file = open(filename, 'a') + else: if stream: _stream = stream if format is not None: From 25e9f1baeacf2285dc3ad35b68b6ef4e4cafaa3d Mon Sep 17 00:00:00 2001 From: bogdan-oprea Date: Sun, 6 May 2018 16:20:14 +0300 Subject: [PATCH 3/4] logging: moving stress test to examples --- logging/example_logging.py | 32 ++++++++++++++++++++++++++++++++ logging/test_logging.py | 21 --------------------- 2 files changed, 32 insertions(+), 21 deletions(-) diff --git a/logging/example_logging.py b/logging/example_logging.py index 0fefb8898..177ba0dac 100644 --- a/logging/example_logging.py +++ b/logging/example_logging.py @@ -1,6 +1,38 @@ +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") + +#stress_test(100) \ No newline at end of file diff --git a/logging/test_logging.py b/logging/test_logging.py index 7582b44cb..8ff6736a2 100644 --- a/logging/test_logging.py +++ b/logging/test_logging.py @@ -1,6 +1,5 @@ import logging import os -import time def reset_log(log_file): @@ -25,26 +24,6 @@ def test_basicConfig(): print("Success: Testing logging.basicConfig") -def stress_test(): - sample = "All work and no play, makes Jack a dull boy!" - log_file = 'file.log' - iterations = 100 - 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)) - tests = [test_basicConfig] From 1a1518af7d704ccd9dedab01fe68b54e24208cf8 Mon Sep 17 00:00:00 2001 From: bogdan-oprea Date: Sun, 6 May 2018 16:57:37 +0300 Subject: [PATCH 4/4] logging: fixed basicConfig stream logging to log to given stream --- logging/logging.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/logging/logging.py b/logging/logging.py index 02c63d323..c1ea0f1cf 100644 --- a/logging/logging.py +++ b/logging/logging.py @@ -34,7 +34,7 @@ def log(self, level, msg, *args): _log_file.write(("%s:%s:" + msg + "\n") % ((self._level_str(level), self.name) + args)) _log_file.flush() else: - print(("%s:%s:" + msg) % ((self._level_str(level), self.name) + args)) + print(("%s:%s:" + msg) % ((self._level_str(level), self.name) + args), file=_stream) def debug(self, msg, *args): self.log(DEBUG, msg, *args) pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

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:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy