Logger / I get all messages 2 times

A

ASh

Hi,

I have this source:

import logging
import logging.config

logging.config.fileConfig("logging.properties")
log = logging.getLogger("qname")
log.debug("message")

------------------- OUTPUT
DEBUG logger_test:8: message
DEBUG logger_test:8: message

------- FILE CONFIG
[formatters]
keys: detailed

[handlers]
keys: console

[loggers]
keys: root, engine

[formatter_detailed]
format: %(levelname)s %(module)s:%(lineno)d: %(message)s

[handler_console]
class: StreamHandler
args: []
formatter: detailed

[logger_root]
level: ERROR
handlers: console

[logger_engine]
level: DEBUG
qualname: qname
handlers: console
 
D

Diez B. Roggisch

ASh said:
Hi,

I have this source:

import logging
import logging.config

logging.config.fileConfig("logging.properties")
log = logging.getLogger("qname")
log.debug("message")

------------------- OUTPUT
DEBUG logger_test:8: message
DEBUG logger_test:8: message

------- FILE CONFIG
[formatters]
keys: detailed

[handlers]
keys: console

[loggers]
keys: root, engine

[formatter_detailed]
format: %(levelname)s %(module)s:%(lineno)d: %(message)s

[handler_console]
class: StreamHandler
args: []
formatter: detailed

[logger_root]
level: ERROR
handlers: console

[logger_engine]
level: DEBUG
qualname: qname
handlers: console

Because you add the handler "console" two times, to logger_engine and
logger_root. You should only add it to root, or set propagate to false.

Diez
 
A

ASh

ASh said:
I have this source:
import logging
import logging.config
logging.config.fileConfig("logging.properties")
log = logging.getLogger("qname")
log.debug("message")
------------------- OUTPUT
DEBUG logger_test:8:  message
DEBUG logger_test:8:  message
------- FILE CONFIG
[formatters]
keys: detailed
[handlers]
keys: console
[loggers]
keys: root, engine
[formatter_detailed]
format: %(levelname)s %(module)s:%(lineno)d:  %(message)s
[handler_console]
class: StreamHandler
args: []
formatter: detailed
[logger_root]
level: ERROR
handlers: console
[logger_engine]
level: DEBUG
qualname: qname
handlers: console
---------------

Why do I get the log 2 times?

Because you add the handler "console" two times, to logger_engine and
logger_root. You should only add it to root, or set propagate to false.

Diez

What if I want to output only the specific logger to console and
ignore every other?
 
V

Vinay Sajip

ASh said:
Hi,
I have this source:
importlogging
importlogging.config
logging.config.fileConfig("logging.properties")
log =logging.getLogger("qname")
log.debug("message")
------------------- OUTPUT
DEBUG logger_test:8: message
DEBUG logger_test:8: message
------- FILE CONFIG
[formatters]
keys: detailed
[handlers]
keys: console
[loggers]
keys: root, engine
[formatter_detailed]
format: %(levelname)s %(module)s:%(lineno)d: %(message)s
[handler_console]
class: StreamHandler
args: []
formatter: detailed
[logger_root]
level: ERROR
handlers: console
[logger_engine]
level: DEBUG
qualname: qname
handlers: console
Because you add the handler "console" two times, to logger_engine and
logger_root. You should only add it to root, or set propagate to false.

What if I want to output only the specific logger to console and
ignore every other?

Just add the handler to the logger you want and to no other logger.
Then, events logged with the logger you want (and its child loggers)
will be sent to the console. If you don't want the child loggers to
log to console, set their propagate value to 0.

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

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,072
Latest member
trafficcone

Latest Threads

Top