
July 29th, 2007, 10:35 PM
| | | problems with logging module
I've been struggling with the logging module in the stdlib which seems
to me rather counter-intuitive:
For some reason it refuses to recognize configuration options when
they are set inside a class so I have had to initialize logging and
set the configuration options in the global scope of my module with
logging.basicConfig.
Here's what I did within the class setup method:
<snip>
self.log = logging.getLogger()
# format
log_format= self.local.format
date_format = self.local.date_format or "%d.%m.%y %H:%M:%S"
self.logfile= self.local.logfile if self.local.log_to_file else None
if self.logfile:
handler = logging.FileHandler(
self.logfile, self.local.logfile_mode)
else:
stream = None # can be sys.st something or other stream
handler = logging.StreamHandler()
format = logging.Formatter(log_format, date_format)
handler.setFormatter(format)
self.log.addHandler(handler)
self.log.setLevel(self.local.log_level or logging.DEBUG)
</snip>
self.log gets initialized but the formatting options do not get
recognized... this is a pain...
What I do want is something like the log4r module in Ruby:
e.g.
require 'log4r'
require 'getoptlong'
require 'pathname'
class Common
def init_log
@log = Log4r::Logger.new(self.class.name)
@log.add Log4r::Outputter.stdout
@log.info 'initialized'
end
end
class Builder < Common
def initialize(path, options)
init_log
if File.exist?(path)
@path = Pathname.new(path)
@options = options
else
@log.error "not a valid file or directory"
exit
end
end
def build()
case @path.ftype
when 'file'
filehandlers = {
'.txt' =TxtHandler,
'.java' =JavaHandler,
'.c' =CHandler,
'.cpp' =CppHandler,
'.py' =PyHandler,
'.pyx' =PyxHandler,
'.exe' =ExeHandler,
'.hs' =HaskellHandler,
'.rb' =RubyHandler,
'.dot' =DotHandler,
'.mp3' =MP3Handler,
'.wav' =WavHandler,
'.csd' =CSoundHandler,
'.orc' =CSoundHandler,
'.sco' =CSoundHandler,
}[@path.extname].new(@path, @options).handle()
when 'directory'
@log.info "#{@path} is a directory"
end
end
end
etc...
still to prefer to code in python though....
Just my 2c...
AK | 
July 30th, 2007, 06:05 AM
| | | Re: problems with logging module
En Sun, 29 Jul 2007 18:29:58 -0300, Alia Khouri <alia_khouri@yahoo.com>
escribió: Quote:
I've been struggling with the logging module in the stdlib which seems
to me rather counter-intuitive:
>
For some reason it refuses to recognize configuration options when
they are set inside a class so I have had to initialize logging and
set the configuration options in the global scope of my module with
logging.basicConfig.
| That looks very strange. Quote: |
Here's what I did within the class setup method:
| Do you mean in its __init__ method? Are you sure an instance of that class
is actually created? Quote:
self.log = logging.getLogger()
>
# format
log_format= self.local.format
| If you are saying that, later, using self.local.format is different that
using log_format, that's hard to believe (unless self/local/format are
insane objects...) Quote:
self.log gets initialized but the formatting options do not get
recognized... this is a pain...
| To see what's going on, try using this old-fashioned debug technique: a
few print statements.
--
Gabriel Genellina | 
July 30th, 2007, 07:55 AM
| | | Re: problems with logging module
On Jul 30, 8:01 am, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote: Quote:
To see what's going on, try using this old-fashioned debug technique: a
few print statements.
| My bad, problem was solved, the shortcoming was one of my variables
pointing to nothingness (-;
But I'm not letting the logging module off, it's still not the easiest
or most intuitive module to work with.
For example: a basic Log class that can be programatically configured
would be quicker to work with then messing around with three different
classes types if you want to get beyond basicConfig functionality:
(Logger, handlers, formatters) To be able to do this would be nice:
from logging import Log
class MyClass:
def __init__(self, x):
self.x = x
self.log = Log(
level=logging.DEBUG,
format="%(asctime)s %(levelname)s: %(message)s",
datefmt = '%H:%M:%S',
filename='app.log',
filemode='a'
)
Thanks for your help and time.
AK | 
July 30th, 2007, 07:15 PM
| | | Re: problems with logging module
En Mon, 30 Jul 2007 03:50:52 -0300, Alia Khouri <alia_khouri@yahoo.com>
escribió: Quote:
But I'm not letting the logging module off, it's still not the easiest
or most intuitive module to work with.
>
For example: a basic Log class that can be programatically configured
would be quicker to work with then messing around with three different
classes types if you want to get beyond basicConfig functionality:
(Logger, handlers, formatters) To be able to do this would be nice:
>
from logging import Log
>
class MyClass:
def __init__(self, x):
self.x = x
self.log = Log(
level=logging.DEBUG,
format="%(asctime)s %(levelname)s: %(message)s",
datefmt = '%H:%M:%S',
filename='app.log',
filemode='a'
)
| You are not going beyond basicConfig - I'd write the above as:
logging.basicConfig(...)
def __init__(self, x):
self.x = x
self.log = logging.getLogger("a.nice.name")
If you *do* need more than a handler, or different formatters for
different loggers, then you must write your own setup anyway; perhaps
using logging.config files, or perhaps writing your own code. I can't
think of a simple and generic approach (beyond what basicConfig provides)
but if you can write something that other people find useful, feel free to
submit a patch.
--
Gabriel Genellina | 
July 31st, 2007, 12:45 PM
| | | Re: problems with logging module
You are not going beyond basicConfig - I'd write the above as:
Intentionally didn't go beyond basicConfig. The problem is global
level configuration vs. easy local (in function or in class)
configuration. Quote:
logging.basicConfig(...)
>
def __init__(self, x):
self.x = x
self.log = logging.getLogger("a.nice.name")
>
If you *do* need more than a handler, or different formatters for
different loggers, then you must write your own setup anyway; perhaps
using logging.config files, or perhaps writing your own code. I can't
think of a simple and generic approach (beyond what basicConfig provides)
but if you can write something that other people find useful, feel free to
submit a patch.
| Why not indeed... let's see what happens.
AK |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over network members.
|