A suggestion for an easy logger
TheSaint
nobody at nowhere.net.no
Sat May 7 07:40:35 EDT 2011
Hello,
I've resumed my hold project and I like to convert it for py3k2.
My knowledge has stagnated at version 2.4, then I found some improvements,
but it will take me some time before I get used to.
I was using this logger >>
=========================================================================
class log:
""" It outputs lists and strings on file or any possible device that
allows write mode. Options are: output file writing mode and special end
of line (EOL)"""
def __init__(self, output= None, mode= 'w', lsp= EOL):
""" Can instantiate even no output is given. Choose to build an open file
object to append to, or new and give a different line separator"""
self.lsp = lsp
self.output= output
self.mode= mode
try: self.output= open(output, self.mode)
except (IOError, TypeError): self.output = None
def logger(self, strng, allowed=None):
# without any output will simply send log to nowhere :-)
if not allowed and not self.output: return
# no allowed to write the output
# keep silent even temporary skip writing
# if allowed, it will go to stderr
if isinstance(strng,list):
a= EOL.join(strng)+ EOL
strng = a
strng= strng.replace(EOL,self.lsp)
# when not allowed just return
if allowed is None: return
if strng == '?close?':
try: # try to close the file
self.output.close()
return
except IOError: return # silently ignore the failure
if self.output and Exists(self.output):
self.output.write(strng)
else:
sys.stderr.write(strng)
sys.stderr.flush()
return
=========================================================================
It is somehow convulitive, isn't it?
I do believe the builtin *print* would do more better than my logger.
Basically I would like a class which shoudl do:
1) print to stdout either with or without carriage return
2) writing to a file
3) Skip some output
Everything should do according to the caller.
I didn't picked up the logging module, a way of confusion on my point of
view. Some small example might easy my aberration :P
--
goto /dev/null
More information about the Python-list
mailing list