python logging

F

Fei

where is default logging file on Mac? I saw lots of app just import
logging, and begins to logging.info(...) etc. I'm not sure where to
look at the logging configuration to figure out the log location.

I just get in touch of python about 1month ago, and I appreciate your
help.
 
I

Ian Kelly

where is default logging file on Mac? I saw lots of app just import
logging, and begins to logging.info(...) etc.  I'm not sure where to
look at the logging configuration to figure out the log location.

There is no default log file. You're seeing that because logging only
needs to be configured by the program once, not on a per-module basis.
Thus most modules will just do import logging on the assumption that
the configuration has already been performed somewhere else.

If no logging configuration is done at all, then the logging
statements will have no effect.
 
F

Fei

There is no default log file.  You're seeing that because logging only
needs to be configured by the program once, not on a per-module basis.
 Thus most modules will just do import logging on the assumption that
the configuration has already been performed somewhere else.

If no logging configuration is done at all, then the logging
statements will have no effect.

Thanks Ian.
 
R

Rafael Durán Castañeda

Thanks Ian.
That's not exactly how it works. You can use logging without any
configuration and the default output will be console. In addition
default logging level is warning, so:

logging.info("Some text")

won't show anything and

logging.warning("Other text")

will show:

WARNING:root:Other text

Please check the link I gave before.

Bye
 
I

Ian Kelly

2011/5/18 Rafael Durán Castañeda said:
That's not exactly how it works. You can use logging without any
configuration and the default output will be console. In addition default
logging level is warning, so:

logging.info("Some text")

won't  show anything and

logging.warning("Other text")

will show:

WARNING:root:Other text

Please check the link I gave before.

Bye

Odd. That seems to work, but it contradicts the documentation at:

http://docs.python.org/howto/logging.html#what-happens-if-no-configuration-is-provided
 
I

Ian Kelly

2011/5/18 Rafael Durán Castañeda said:
I think you are confuse because of you are looking at advanced logging,
where getLogger is being used. Simple logging works without any
configuration, getLogger doesn't.

It seems to work without any configuration just as well as the root logger:
WARNING:foo:test

Or am I misunderstanding you?
 
R

Rafael Durán Castañeda

It seems to work without any configuration just as well as the root logger:

WARNING:foo:test

Or am I misunderstanding you?
Are you using python 2.x or 3.x? At python 2.7 using:

import logging
logging.getLogger('log').warning('test')

I got:

No handlers could be found for logger "log"
 
V

Vinay Sajip

It seems to work without any configuration just as well as the root logger:


WARNING:foo:test

Or am I misunderstanding you?

In general for Python 2.x, the code

import logging
logging.getLogger('foo').warning('test')

will produce

No handlers could be found for logger "foo"

unless loggers have been configured, e.g. by calling logging.warning()
- that call implicitly adds a console handler to the root logger, if
no other handlers have been configured for the root logger.

In Python 3.2 and later, if no handlers have been configured, messages
at level WARNING and greater will be printed to sys.stderr using a
"handler of last resort" - see

http://docs.python.org/py3k/howto/logging.html#what-happens-if-no-configuration-is-provided

Regards,

Vinay Sajip
 
I

Ian Kelly

2011/5/18 Rafael Durán Castañeda said:
Are you using python 2.x or 3.x? At python 2.7 using:

import logging
logging.getLogger('log').warning('test')

I got:

No handlers could be found for logger "log"

Ah, that's it. I was using Python 2.5. Using 2.7 I get the same
result that you do.

Still, it's a surprising change that doesn't seem to be documented as
such. I'm not sure whether it's a regression or an intentional
change.
 
I

Ian Kelly

2011/5/18 Ian Kelly said:
Ah, that's it.  I was using Python 2.5.  Using 2.7 I get the same
result that you do.

Still, it's a surprising change that doesn't seem to be documented as
such.  I'm not sure whether it's a regression or an intentional
change.

I was wrong, it's more complicated than that.

Python 2.7.1 (r271:86832, Nov 27 2010, 18:30:46) [MSC v.1500 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.WARNING:log:test

Apparently, getLogger() is unconfigured by default, but if you just
use the root logger once, then they magically get configured.
 
V

Vinay Sajip

I was wrong, it's more complicated than that.


No handlers could be found for logger "log">>>logging.warning('test')
WARNING:root:test

WARNING:log:test

Apparently, getLogger() is unconfigured by default, but if you just
use the root logger once, then they magically get configured.

The difference is that you called the module-level convenience
function -

logging.warning('test')

The module-level convenience functions call basicConfig(), which
configures a console handler on the root logger if no handlers are
present there. This is documented at

http://docs.python.org/library/logging.html#logging.log

(see para starting "PLEASE NOTE:")

and

http://docs.python.org/howto/logging.html#advanced-logging-tutorial

(search for "If you call the functions")

This is not a behaviour change - it's been like this since logging
appeared in Python, see

http://hg.python.org/cpython/annotate/f72b1f8684a2/Lib/logging/__init__.py#l1145

Regards,

Vinay Sajip
 

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

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top