Created
March 15, 2017 04:29
-
-
Save srkama/8a57de3fea70779e7e0a3858547308fb to your computer and use it in GitHub Desktop.
creating new custom level logger
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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