Author xtreak
Recipients eamanu, porton, steven.daprano, vinay.sajip, xtreak
Date 2018-12-19.10:30:21
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1545215421.77.0.788709270274.issue35530@psf.upfronthosting.co.za>
In-reply-to
Content
The output is the same in 3.8. I think this is due to propagation to the root logger after logging.error call. When logger.error is called with no handler attached to it then root logger handler is called and root handler's format is used. The fix would be to do logger.propagate = False after logger initialization. In your code in first call for logger.error just prints the message, root handler is initialized during logging.error and thus the earlier call works fine but the next call to logger calls the root logger's handler.

The fix in the reported case would be to add logger.propagate = False after calling logging.error. I think logging.error has does something to define root logger handler? 

Hopefully Vinay has a better explanation of this behavior.

Sample code : 

import logging

logging.basicConfig(format="root logger handler: %(message)s")

logger = logging.getLogger(name='main')
logger.setLevel(logging.INFO)

logger1 = logging.getLogger(name='main1')
logger1.setLevel(logging.INFO)
ch = logging.StreamHandler()
logger1_formatter = logging.Formatter('logger 1 handler : %(message)s')
ch.setFormatter(logger1_formatter)
logger1.addHandler(ch)

logger.error('logger XXX') # calls root logger's handler
logging.error('root logger XXX') # calls root logger's handler
logger1.error('logger 1 XXX') # Calls ch and then root logger's handler

logger1.propagate = False
logger1.error('logger 1 XXX') # Calls only ch since propagation is set to False and root handler is not called

Output on 3.8 : 


root logger handler: logger XXX
root logger handler: root logger XXX
logger 1 handler : logger 1 XXX
root logger handler: logger 1 XXX
logger 1 handler : logger 1 XXX
History
Date User Action Args
2018-12-19 10:30:21xtreaksetrecipients: + xtreak, vinay.sajip, steven.daprano, porton, eamanu
2018-12-19 10:30:21xtreaksetmessageid: <1545215421.77.0.788709270274.issue35530@psf.upfronthosting.co.za>
2018-12-19 10:30:21xtreaklinkissue35530 messages
2018-12-19 10:30:21xtreakcreate