473,372 Members | 933 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,372 software developers and data experts.

strange behavious of the logging module?

Hi,

having the following code:

import logging
logging.basicConfig(level=logging.ERROR,
format='%(levelname)-8s %(message)s',
filename='mc_rigid.log',
filemode='w')
# define a Handler which writes INFO messages or higher to the sys.stderr
console = logging.StreamHandler()
console.setLevel(logging.INFO)
# set a format which is simpler for console use
formatter = logging.Formatter('%(message)s')
# tell the handler to use this format
console.setFormatter(formatter)
# add the handler to the root logger
logging.getLogger('').addHandler(console)

I observe nothing printed on the console. However if I change the level
(just after the import) from logging.ERROR to logging.DEBUG I do see all
info lines on the console as intended.

Can anybody tell my mistake? I thought changing the level in basicConfig
would have no impact on the following StreamHandler instance.
Also, adding %(funcName)-8s to the formatter in basigConfig does not work
for me: instead of the functions name the level in lower case gets inserted
into the logfile.

My python:
Python 2.5.1 (r251:54863, May 2 2007, 16:56:35)
[GCC 4.1.2 (Ubuntu 4.1.2-0ubuntu4)] on linux2

TIA
Christian
Sep 23 '07 #1
10 1605
En Sun, 23 Sep 2007 18:53:53 -0300, Christian Meesters
<me******@uni-mainz.deescribi�:
import logging
logging.basicConfig(level=logging.ERROR,
format='%(levelname)-8s %(message)s',
filename='mc_rigid.log',
filemode='w')
# define a Handler which writes INFO messages or higher to the sys.stderr
console = logging.StreamHandler()
console.setLevel(logging.INFO)
# set a format which is simpler for console use
formatter = logging.Formatter('%(message)s')
# tell the handler to use this format
console.setFormatter(formatter)
# add the handler to the root logger
logging.getLogger('').addHandler(console)

I observe nothing printed on the console. However if I change the level
(just after the import) from logging.ERROR to logging.DEBUG I do see all
info lines on the console as intended.
basicConfig sets the root logger level; if it's ERROR, nothing below ERROR
will pass. Your handler does not have a chance to get the message.
Can anybody tell my mistake? I thought changing the level in basicConfig
would have no impact on the following StreamHandler instance.
basicConfig sets the overall verbosity of your application.
Also, adding %(funcName)-8s to the formatter in basigConfig does not work
for me: instead of the functions name the level in lower case gets
inserted
into the logfile.
Looks like a bug...

--
Gabriel Genellina

Sep 23 '07 #2
Christian Meesters wrote:
Also, adding %(funcName)-8s to the formatter in basigConfig does not work
for me: instead of the functions name the level in lower case gets inserted
into the logfile.
When you call logging.info(...), "info" actually is the function name. As a
workaround use logging.getLogger(...).info(...).

It would still be a good idea to file a bug report.

Peter
Sep 24 '07 #3
On Sep 24, 8:03 am, Peter Otten <__pete...@web.dewrote:
>
It would still be a good idea to file a bug report.
Not sure where the bug is. The script below, when called, prints
"Information" to the console and "INFO some_func Information" to
mc_rigid.log, as I would expect. (Python 2.5.1)

Best regards,

Vinay

import logging
logging.basicConfig(level=logging.DEBUG,
format='%(levelname)-8s %(funcName)-8s %
(message)s',
filename='mc_rigid.log',
filemode='w')
# define a Handler which writes INFO messages or higher to the
sys.stderr
console = logging.StreamHandler()
console.setLevel(logging.INFO)
# set a format which is simpler for console use
formatter = logging.Formatter('%(message)s')
# tell the handler to use this format
console.setFormatter(formatter)
# add the handler to the root logger
logging.getLogger('').addHandler(console)

def some_func():
logging.info("Information")

if __name__ == "__main__":
some_func()
Sep 24 '07 #4
Vinay Sajip wrote:
On Sep 24, 8:03 am, Peter Otten <__pete...@web.dewrote:
>>
It would still be a good idea to file a bug report.

Not sure where the bug is. The script below, when called, prints
"Information" to the console and "INFO some_func Information" to
mc_rigid.log, as I would expect. (Python 2.5.1)
I get

$ python vinaj.py
Information
$ cat mc_rigid.log
INFO info Information

Maybe a platform issue (I'm on Ubuntu 7.04)?

It seems Logger.findCaller() gets puzzled by the following discrepancy:
>>logging.info.func_code.co_filename
'logging/__init__.py'
>>logging._srcfile
'/usr/lib/python2.5/logging/__init__.py'

I don't think I have messed with paths manually on that machine, by the
way.

Peter
Sep 24 '07 #5
On Sep 24, 4:29 pm, Peter Otten <__pete...@web.dewrote:
Vinay Sajip wrote:
On Sep 24, 8:03 am, Peter Otten <__pete...@web.dewrote:
It would still be a good idea to file a bug report.
Not sure where the bug is. The script below, when called, prints
"Information" to the console and "INFO some_func Information" to
mc_rigid.log, as I would expect. (Python 2.5.1)

I get

$ python vinaj.py
Information
$ cat mc_rigid.log
INFO info Information

Maybe a platform issue (I'm on Ubuntu 7.04)?

It seems Logger.findCaller() gets puzzled by the following discrepancy:
>>logging.info.func_code.co_filename

'logging/__init__.py'>>>logging._srcfile

'/usr/lib/python2.5/logging/__init__.py'

I don't think I have messed with paths manually on that machine, by the
way.
This is a known bug, and not specifically related to logging, though
it sometimes manifests itself via logging:

http://bugs.python.org/issue1180193

Regards,

Vinay

Sep 24 '07 #6
Vinay Sajip wrote:
This is a known bug, and not specifically related to logging, though
it sometimes manifests itself via logging:

http://bugs.python.org/issue1180193
Yup, thanks for looking into it.

Peter
Sep 25 '07 #7
Peter Otten wrote:
Vinay Sajip wrote:
>This is a known bug, and not specifically related to logging, though
it sometimes manifests itself via logging:

http://bugs.python.org/issue1180193

Yup, thanks for looking into it.

Peter
Thanks Vinay, but I am curious how issue 1180193 is related to the "bug"
Peter and me described: I do not have any *.pyc file around. As I kept
changing the file during development I used to call the program directly
using a link in my PATH. Hence no *.pyc file got created at all.

However, does anybody know a suggestion for a work-around?

Christian
Sep 25 '07 #8
Christian Meesters wrote:
Peter Otten wrote:
>Vinay Sajip wrote:
>>This is a known bug, and not specifically related to logging, though
it sometimes manifests itself via logging:

http://bugs.python.org/issue1180193

Yup, thanks for looking into it.

Peter
Thanks Vinay, but I am curious how issue 1180193 is related to the "bug"
Peter and me described: I do not have any *.pyc file around. As I kept
changing the file during development I used to call the program directly
using a link in my PATH. Hence no *.pyc file got created at all.
The problem is not caused by the pyc-file for your script, but by
the logging/__init__.pyc file in your python installation. You
can check if you are affected by the bug by typing
>>logging.info.func_code.co_filename
'logging/__init__.py'
>>logging._srcfile
'/usr/lib/python2.5/logging/__init__.py'

in the interpreter. If you get different paths like in "logging" and
"/usr/lib/python2.5/logging" in the above example you are affected.
However, does anybody know a suggestion for a work-around?
You could try setting

logging._srcfile = logging.info.func_code.co_filename

manually, but that might break other things. Or you recreate the pyc-files
of your distribution. Again, I don't know what might break...

Peter
Sep 25 '07 #9
On 25 Sep, 09:40, Peter Otten <__pete...@web.dewrote:
You could try setting

logging._srcfile =logging.info.func_code.co_filename

manually, but that might break other things. Or you recreate the pyc-files
of your distribution. Again, I don't know what might break...
Recreating .pyc files should not break anything - this will get done
automatically, so deleting the .pyc and/or .pyo files in the Python/
Lib directory tree should be sufficient. See the issues linked to by

http://bugs.python.org/msg24988

In these cases, deleting the .pyc/.pyo files solved the problem.

Best regards,

Vinay

Sep 25 '07 #10
Thanks Peter and Vinay,

I finally understood.
And indeed, removing the pyc-file in questions solves the problem - at least
temporarily.

Christian

Sep 27 '07 #11

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...
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: 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...
10
by: sophie_newbie | last post by:
I have written a cgi script that seems to run perfectly from the command line when I simulate some cgi input using os.environ. The thing is that when I run it from the browser, one of my...
7
by: Jed Parsons | last post by:
Hi, I'm using the logging module for the first time. I'm using it from within Zope Extensions. My problem is that, for every event logged, the logger is producing multiple identical entries...
4
by: Frank Aune | last post by:
Hello, I've been playing with the python logging module lately, and I manage to log to both stderr and MySQL database. What I'm wondering, is if its possible to specify the database handler in...
2
by: Frank Aune | last post by:
Hi, Explaining this problem is quite tricky, but please bear with me. Ive got an application which uses MySQL (InnoDB) for two things: 1. Store and retrieve application data (including...
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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.