import logging import warnings def simple_format_warning(message, category, *args, **kwargs): return '%s: %s' % (category.__name__, str(message)) warnings.formatwarning = simple_format_warning logging.basicConfig( filename="temp.log", format=( "%(asctime)s:%(msecs)-0.3f - %(levelname)s" " - %(name)s - %(module)s:%(lineno)d" " :: %(message)s" ), datefmt="%Y-%m-%d_%H:%M:%S", ) logger = logging.getLogger(__name__) logger.propagate = False formatter = logging.Formatter( fmt=( "%(asctime)s:%(msecs)-0.3f - %(levelname)s" " - %(name)s - %(module)s:%(lineno)d" " :: %(message)s" ), datefmt="%Y-%m-%d_%H:%M:%S", style="%" ) handler = logging.FileHandler(filename="temp.log") handler.setFormatter(formatter) logger.addHandler(handler) logging.captureWarnings(True) warnings.warn("Warning message !!!") logger.warning("Logging message !!!") """ In 'temp.log', I get these two lines : 2018-08-10_19:47:46:107.400 - WARNING - py.warnings - warnings:99 :: UserWarning: Warning message !!! 2018-08-10_19:47:46:108.400 - WARNING - __main__ - bug_capture_warning:43 :: Logging message !!! If we look at the first line of log, we see 'warnings:99' but in my opinion it should be 'temp_code:42' """