Hi,

I get the log messages 'twice' on the console and only once in the log file.

Can any one tell me the reason for this duplication on console?

here is how I have set the logger:

import logging
#--set up logging to file
logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
                    datefmt='%Y-%m-%d %H:%M',
                    filename='linkconfig_log.txt',
                    filemode='a')
                    
#--defines a handler which writes DEBUG messages or higher to sys.stdout
console=logging.StreamHandler()
console.setLevel(logging.DEBUG)


#--setting a format which is simpler for console use
formatter=logging.Formatter('%(levelname)-8s %(message)s')

#--tell the handler to use this format
console.setFormatter(formatter)

#--add handler to the root logger
logging.getLogger('').addHandler(console)

help please

no inputs? :(

where have all my saviours gone? :(

I for one, did not get any error or doubles from your code and it looks almost same as code in Python documentation:

import logging

# create logger
logger = logging.getLogger("simple_example")
logger.setLevel(logging.DEBUG)

# create console handler and set level to debug
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)

# create formatter
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")

# add formatter to ch
ch.setFormatter(formatter)

# add ch to logger
logger.addHandler(ch)

# "application" code
logger.debug("debug message")
logger.info("info message")
logger.warn("warn message")
logger.error("error message")
logger.critical("critical message")

@ tonyjv

thanks. Yes it's the exact code from Python documentation. Yet I am getting the duplication of messages on console, but not in the log file

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.