By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,965 Members | 1,450 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,965 IT Pros & Developers. It's quick & easy.

problems with logging module

P: n/a
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

Jul 29 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
En Sun, 29 Jul 2007 18:29:58 -0300, Alia Khouri <al*********@yahoo.com>
escribió:
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.
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?
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...)
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

Jul 30 '07 #2

P: n/a
On Jul 30, 8:01 am, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
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
Jul 30 '07 #3

P: n/a
En Mon, 30 Jul 2007 03:50:52 -0300, Alia Khouri <al*********@yahoo.com>
escribió:
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

Jul 30 '07 #4

P: n/a
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.

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

Jul 31 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.