2016年4月21日 星期四

[python] debug log with logging config, pprint with logging, logging to file and still output at stdout

import pprint
import logging
from source.projectparameter import project_parameter as pp


def logconfig():
    print_format="%(asctime)s,line:(%(lineno)d): %(message)s"

    logging.basicConfig(filename="abspath/xxxx_debug.log",
                        filemode='a',
                        format=print_format,
                        datefmt='%H:%M:%S',
                        level=logging.DEBUG)
    stderrLogger=logging.StreamHandler()
    stderrLogger.setFormatter(logging.Formatter(print_format))
    logging.getLogger().addHandler(stderrLogger)
    return 0



def show_all_init_paramater():

    for tag in pp:
        #print('\n',tag,'with',len(pp[tag]),'items')
        #logging.debug("\n",tag,"with",len(pp[tag]),"items")
        logging.debug("%s with %s items",tag,len(pp[tag]))
        logging.debug("\n"+pprint.pformat(pp[tag]))
    return 0


def main():
    logconfig()
    logging.debug('=======NEW SECTION======')
    show_all_init_paramater()


    return

main()

沒有留言:

張貼留言