def get_logger(): logging.setLoggerClass(PyLogrus) logger = logging.getLogger(__name__) # type: PyLogrus logger.setLevel(logging.DEBUG) formatter = TextFormatter(datefmt='Z', colorize=True) formatter.override_level_names({ 'CRITICAL': 'CRIT', 'ERROR': 'ERRO', 'WARNING': 'WARN', 'DEBUG': 'DEBU' }) formatter.override_colors({ 'prefix': CL_BLDYLW + CL_BAKPUR, 'debug': CL_BLDWHT + CL_BAKBLU, 'info': CL_TXTWHT + CL_BAKGRN, 'warning': CL_BLDBLK + CL_BAKYLW, 'error': CL_BLDYLW + CL_BAKRED, 'critical': CL_BLDYLW + CL_BAKRED }) ch = logging.StreamHandler() ch.setLevel(logging.DEBUG) ch.setFormatter(formatter) logger.addHandler(ch) return logger
def test_color_overriding(self): formatter = TextFormatter(colorize=True) formatter.override_colors({'info': CL_BLDYLW, 'field': CL_TXTBLU}) log = self.get_logger(formatter) log.withFields({'user': '******'}).info("test message") with open(self.filename) as f: content = f.readlines()[-1] self.assertIn(" {}INFO{} ".format(CL_BLDYLW, CL_TXTRST), content) self.assertIn( "; {}user{}={}John Doe{}".format(CL_TXTBLU, CL_TXTRST, CL_TXTRST, CL_TXTRST), content)