• logging日志模块


    import logging  # 引入logging模块
    import os.path
    import time


    log = logging.getLogger()
    fh = logging.FileHandler('www.log')
    ch = logging.StreamHandler()
    format = logging.Formatter('%(asctime)s - %(filename)s[line:%(lineno)d] '
    '- %(levelname)s: %(message)s')
    fh.setFormatter(format)

    ch.setFormatter(format)

    log.addHandler(fh)
    log.addHandler(ch)
    log.setLevel(logging.DEBUG)
    log.debug('this is a logger debug message')
    log.info('this is a logger info message')
    log.warning('this is a logger warning message')
    log.error('this is a logger error message')
    log.critical('this is a logger critical message')




    # 将信息打印到控制台上
    # logging.debug(u"苍井空")
    # logging.info(u"麻生希")
    # logging.warning(u"小泽玛利亚")
    # logging.error(u"桃谷绘里香")
    # logging.critical(u"泷泽萝拉")

    # logging.basicConfig(filename='test.log',filemode='w',level=logging.DEBUG,
    # format=' %(levelname)s %(asctime)s %(message)s', datefmt='%m-%d-%Y %I:%M:%S %p')
    # logging.debug('debuggggggggggggg')
    # logging.info('infooooooooooooooooo')
    # logging.warning('warninggggggggggggggg')
    # logging.error('errorrrrrrrrrrrrr')
    # logging.critical('criticalllllllllllll')


    # 第一步,创建一个logger
    logger = logging.getLogger()
    logger.setLevel(logging.INFO) # Log等级总开关
    # 第二步,创建一个handler,用于写入日志文件
    rq = time.strftime('%Y%m%d%H%M', time.localtime(time.time()))
    log_path = os.path.dirname(os.getcwd())
    log_name = 'test.log'
    print(log_name)
    logfile = log_name
    fh = logging.FileHandler(logfile, mode='a')
    fh.setLevel(logging.DEBUG) # 输出到file的log等级的开关
    # 第三步,定义handler的输出格式
    formatter = logging.Formatter("%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s")
    fh.setFormatter(formatter)
    # 第四步,将logger添加到handler里面
    logger.addHandler(fh)
    # 日志
    logger.debug('this is a logger debug message')
    logger.info('this is a logger info message')
    logger.warning('this is a logger warning message')
    logger.error('this is a logger error message')
    logger.critical('this is a logger critical message')
  • 相关阅读:
    来谈谈JAVA面向对象
    手把手的SpringBoot教程,SpringBoot创建web项目(一)
    【Java框架型项目从入门到装逼】第十五节
    印章文字识别
    《图像处理实例》之 曲线之间距离求解
    机器学习常用模块
    Ubutu16.04+Cuda9.2/9.0+Cudnn7.12/7.05+TensorFlow-gpu-1.8/1.6
    滑动平均模型原理+源码分析
    AlexNet实践
    TensorFlow NormLization
  • 原文地址:https://www.cnblogs.com/lokerx/p/10836761.html
Copyright © 2020-2023  润新知