#The levels of logging:
#print all levels:Critical>error>warning>info>debug
#----------------------------------logging.basicConfig
1 # import logging 2 # logging.basicConfig( 3 # level=logging.DEBUG, 4 # #put them into files 5 # filename="logger.log", 6 # filemode="w", 7 # format=("%(asctime)s [%(lineno)d] %(message)s") 8 # 9 # ) 10 11 # logging.debug('hello') #you can change the contents 12 # logging.info('info message') #*****uaually used 13 # logging.warning('warning message') 14 # logging.error('error message') 15 # logging.critical('critical message')
#The result:
# 2020-04-02 16:14:26,878 [13] hello
# 2020-04-02 16:14:26,878 [14] info message
# 2020-04-02 16:14:26,879 [15] warning message
# 2020-04-02 16:14:26,879 [16] error message
# 2020-04-02 16:14:26,879 [17] critical message
#----------------------------------logger,show in files and the screan:
1 import logging 2 # def logger(): 3 # logger=logging.getLogger() 4 # 5 # fh = logging.FileHandler("test_log") 6 # ch = logging.StreamHandler() 7 # 8 # fm = logging.Formatter("%(asctime)s %(message)s") 9 # 10 # fh.setFormatter(fm) 11 # ch.setFormatter(fm) 12 # 13 # logger.addHandler(fh) 14 # logger.addHandler(ch) 15 # logger.setLevel("DEBUG") 16 # 17 # return logger 18 # # #--------------------------- 19 # logger = logger() 20 # 21 # logger.debug("hello") 22 # logger.info("hello") 23 # logger.warning("hello") 24 # logger.error("hello") 25 # logger.critical("critical")
#########################################the relationship with parent and son level:
1 import logging 2 logger = logging.getLogger() 3 4 logger1 = logging.getLogger('mylogger') #mylogger the same 5 logger1.setLevel(logging.DEBUG) 6 7 # logger2 = logging.getLogger('mylogger.sontree') #do not be the same because mylogger the same,will set as the laest one as print the info. 8 # logger2.setLevel(logging.INFO) 9 10 fh = logging.FileHandler("test_log-new") 11 ch = logging.StreamHandler() 12 13 logger.addHandler(fh) 14 logger.addHandler(ch) 15 16 logger1.addHandler(fh) 17 logger1.addHandler(ch) 18 19 # logger2.addHandler(fh) 20 # logger2.addHandler(ch) 21 22 23 logger.debug('logger debug message') 24 logger.info('logger info message') 25 logger.warning('logger warning message') 26 logger.error('logger error message') 27 logger.critical('logger critical message') 28 29 logger1.debug('logger1 debug message') 30 logger1.info('logger1 info message') 31 logger1.warning('logger1 warning message') 32 logger1.error('logger1 error message') 33 logger1.critical('logger1 critical message') 34 35 # logger2.debug('logger2 debug message') 36 # logger2.info('logger2 info message') 37 # logger2.warning('logger2 warning message') 38 # logger2.error('logger2 error message') 39 # logger2.critical('logger2 critical message')
#-----------result:Not true
# logger1 info message
# logger1 warning message
# logger1 error message
# logger1 critical message
# logger2 info message
# logger2 warning message
# logger2 error message
# logger2 critical message
#---------------after add logger the results:when the parent have outlet the son level will print twice .
# logger warning message
# logger error message
# logger critical message
# logger1 debug message
# logger1 debug message
# logger1 info message
# logger1 info message
# logger1 warning message
# logger1 warning message
# logger1 error message
# logger1 error message
# logger1 critical message
# logger1 critical message
#method:close the parent then the son level can print normally.