I have the following code setting up the logger:
import logging
logging.basicConfig(format='%(asctime)s %(levelname)s: %(message)s', level=logging.INFO)
log = logging.getLogger()
handler = logging.StreamHandler(sys.stdout)
log.addHandler(handler)
log.info('abc')
When I run it, I get output like:
2020-06-10 13:32:16,245 INFO: abc
abc
I presume the first one is the console output? How do I get rid of the duplicate one?
UPDATE
Thanks guys for the answers, now I know why I get duplicates, the reason I did it this way because the default stream handler does not output to stdout, so I googled and saw someone adding a stdout handler, hence I got 2 stream handlers (console and stdout).
I have now read again the documentation about the basicConfig() and I have worked out the easiest way to achieve what I want:
import sys
import logging
log = logging.getLogger(__name__)
logging.basicConfig(format='%(asctime)s %(levelname)s: %(message)s', level=logging.INFO, stream=sys.stdout)
log.info('abc')