-
Notifications
You must be signed in to change notification settings - Fork 1k
logging: support for file based logging #277
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
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6975d1e
logging: add support for file logging and testing for it
opreab f6416a1
logging: removing open/close operation on each file log entry
opreab 25e9f1b
logging: moving stress test to examples
opreab 1a1518a
logging: fixed basicConfig stream logging to log to given stream
opreab 9961867
Merge branch 'master' into opreab-logging
opreab File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do you want to put this function in example ? |
||
|
||
|
||
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") | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we write to stream and file in same time? |
||
|
||
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") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it good approach to silently pass error?