473,326 Members | 2,128 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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

Jul 29 '07 #1
4 1856
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Robert.Schmitt | last post by:
I found that the configuration system of the new logging package of Python 2.3 has some unintuitive idiosyncracies that are worth mentioning because they can cost you quite some development time...
3
by: Frantisek Fuka | last post by:
I am using the standard "logging" module included with Python and it seems it doesn't correctly identify the filename (and thus module name) from where the logging method was called. In fact, no...
6
by: Ville Vainio | last post by:
Just posting this for the sake of google: Like everyone else, I figured it's time to start using the 'logging' module. I typically want to dump "info" level (and up) log information to...
2
by: Simon Dahlbacka | last post by:
Hi, I'm currently using python 2.3.4 and I'm having problem with the logging module. Occasionally when logging something with exc_info=True it just hangs, nothing is logged, and software...
2
by: rh0dium | last post by:
Hi all, So I have a slice of code which calls other python code. I have started to take a real liking to the logging module, but I want to extend this into the called python code. I have no...
23
by: Rotem | last post by:
Hi, while working on something in my current project I have made several improvements to the logging package in Python, two of them are worth mentioning: 1. addition of a logging record field...
0
by: robert | last post by:
As more and more python packages are starting to use the bloomy (Java-ish) 'logging' module in a mood of responsibility and as I am not overly happy with the current "thickener" style of usage, I...
6
by: rh0dium | last post by:
Hi Experts!! I am trying to get the following little snippet to push my data to the function func(). What I would expect to happen is it to print out the contents of a and loglevel. But it's...
3
by: Lowell Alleman | last post by:
Here is the situation: I wrote my own log handler class (derived from logging.Handler) and I want to be able to use it from a logging config file, that is, a config file loaded with the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.