Skip to content

Instantly share code, notes, and snippets.

@srkama
Created March 15, 2017 04:29
Show Gist options
  • Save srkama/8a57de3fea70779e7e0a3858547308fb to your computer and use it in GitHub Desktop.
Save srkama/8a57de3fea70779e7e0a3858547308fb to your computer and use it in GitHub Desktop.
creating new custom level logger
import logging
DEBUG_LEVELV_NUM = 35
logging.DEBUG_LEVELV_NUM = DEBUG_LEVELV_NUM
logging.addLevelName(DEBUG_LEVELV_NUM, "DEBUGV")
def debugv(self, message, *args, **kws):
# Yes, logger takes its '*args' as 'args'.
if self.isEnabledFor(DEBUG_LEVELV_NUM):
self._log(DEBUG_LEVELV_NUM, message, args, **kws)
logging.Logger.debugv = debugv
def main():
l = logging.getLogger("")
l.setLevel(logging.WARNING)
# create formatter
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
# add formatter to ch
ch = logging.FileHandler("myapp.log")
ch.setFormatter(formatter)
# add ch to logger
l.addHandler(ch)
l.info('info log')
l.debug("debug log")
l.critical("critical log")
l.debugv('debugv log')
l.warning('warning log')
l.error('error log')
l.info('info log')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment