Skip to content

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
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions logging/example_logging.py
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

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?



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))

Choose a reason for hiding this comment

The 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")
Expand Down
25 changes: 16 additions & 9 deletions logging/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Choose a reason for hiding this comment

The 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)
Expand All @@ -69,6 +74,7 @@ def exception(self, msg, *args):

_level = INFO
_loggers = {}
_log_file = None

def getLogger(name):
if name in _loggers:
Expand All @@ -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")
37 changes: 37 additions & 0 deletions logging/test_logging.py
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()