wayfare
wayfare

Reputation: 1850

python logger loggin twice

I have a simple flask app which is supposed to log to console. My issue is that it is logging twice to console.

I have following logging config:

root = logging.getLogger()
root.setLevel(logging.INFO)
ch = logging.StreamHandler(sys.stdout)
formatter = logging.Formatter('%(asctime)s | src="%(name)s" | lvl="%(levelname)s" | msg="%(message)s"')
ch.setFormatter(formatter)
ch.setLevel(logging.INFO)
root.addHandler(ch)
#werkzeug disable werkzeug logs
werkzeug_logger = logging.getLogger('werkzeug')
werkzeug_logger.setLevel(logging.ERROR)

Console output:

INFO:auth:Starting oauth flow
2017-06-08 14:16:08,494 | src="auth" | lvl="INFO" | msg="Starting oauth flow"

Upvotes: 0

Views: 591

Answers (1)

Szabolcs Dombi
Szabolcs Dombi

Reputation: 5783

Try to determine how many handlers do root has:

print(len(root.handlers))

I guess you have more then one handlers. To avoid this you can add a handler if there are no other handlers:

if not root.handlers:
    root.addHandler(...)

You can remove all the handlers from a logger:

for x in root.handlers.copy():
    root.removeHandler(x)

Upvotes: 1

Related Questions