problems with logging module

A

Alia Khouri

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
 
G

Gabriel Genellina

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.
 
A

Alia Khouri

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
 
G

Gabriel Genellina

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.
 
A

Alia Khouri

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,262
Messages
2,571,056
Members
48,769
Latest member
Clifft

Latest Threads

Top