添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
正直的镜子  ·  GitHub - ...·  4 天前    · 
豪情万千的鸭蛋  ·  Bienvenue — ...·  4 天前    · 
另类的蚂蚁  ·  gmic · PyPI·  4 天前    · 
温暖的雪糕  ·  Python HTML 至 JSON - ...·  16 分钟前    · 
朝气蓬勃的沙发  ·  GitHub - ...·  16 分钟前    · 
坚强的山楂  ·  马化腾·  3 月前    · 
# logging 默认设置的级别 是warning # 设置成哪个级别 ,就会打印这个级别 及以上级别的日志 logging.basicConfig(level=logging.DEBUG) logging.debug("这是debug日志") logging.warning('Watch out!') # will print a message to the console logging.info('I told you so') # will not print anything logging.error("这是一条error级别的日志") 保存日志到文件
logging.basicConfig(filename='myapp.log', level=logging.INFO)
import logging
logging.basicConfig(filename='example.log', level=logging.DEBUG)
logging.debug('This message should go to the log file')
logging.info('So should this')
logging.warning('And this, too')
logging.error('And non-ASCII stuff, too, like Øresund and Malmö')
设置时间格式
logging.basicConfig(filename='myapp.log', level=logging.INFO,format='%(asctime)s [%(levelname)s] %(message)s (%(filename)s:%(lineno)s)', datefmt='%m/%d/%Y %I:%M:%S %p')
import logging
# logging.basicConfig(format='%(asctime)s %(message)s')
# logging.warning('is when this event was logged.')
# logging.basicConfig(format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p')
# logging.warning('is when this event was logged.')
logging.basicConfig(filename='myapp.log',
                    level=logging.INFO,
                    format='%(asctime)s [%(levelname)s] %(message)s (%(filename)s:%(lineno)s)', datefmt='%m/%d/%Y %I:%M:%S %p')
logging.warning('is when this event was logged.')
logging.debug("这是debug日志")
logging.info("这是debug日志")
python 日志进阶用法
  • 日志记录的流程
  • 封装日志公共模块
  • 日志配置文件
  • python 日志进阶 image1536×448 71.1 KB python 日志记录流程 image1170×530 25.3 KB
    import logging
    # create logger
    logger = logging.getLogger('simple_example')
    logger.setLevel(logging.DEBUG)
    # create console handler and set level to debug
    # 流处理器
    ch = logging.StreamHandler()
    # 文件处理器
    # ch = logging.FileHandler("mylog.log",encoding='utf-8')
    ch.setLevel(logging.DEBUG)
    # create formatter
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    # add formatter to ch
    ch.setFormatter(formatter)
    # add ch to logger
    logger.addHandler(ch)
    # 定义一个文件处理器
    # 文件处理器
    ch_file = logging.FileHandler("mylog.log",encoding='utf-8')
    ch_file.setLevel(logging.DEBUG)
    # create formatter
    formatter1 = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    # add formatter to ch
    ch_file.setFormatter(formatter1)
    # add ch to logger
    logger.addHandler(ch_file)
    # 'application' code
    logger.debug('debug message')
    logger.info('info message')
    logger.warning('warn message')
    logger.error('error message')
    logger.critical('critical message')
    python 日志定义
    

    官网:https://docs.python.org/zh-cn/3/howto/logging.html

    import logging
    import os
    # create logger 创建一个logger 记录器
    logger = logging.getLogger(os.path.basename(__file__))
    logger.setLevel(logging.DEBUG)
    # create console handler and set level to debug
    ch = logging.StreamHandler()
    ch.setLevel(logging.DEBUG)
    # create formatter
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    # add formatter to ch
    ch.setFormatter(formatter)
    # add ch to logger
    logger.addHandler(ch)
    # 'application' code
    logger.debug('debug message')
    logger.info('info message')
    logger.warning('warn message')
    logger.error('error message')
    logger.critical('critical message')
    封装日志公共函数
    

    https://ceshiren.com/t/topic/13564

    import logging
    import os
    # 定义一个记录器
    def get_logger():
        # create logger
        print(os.path.basename(__file__))
        # 记录器
        logger = logging.getLogger(os.path.basename(__file__))
        logger.setLevel(logging.DEBUG)
        # create console handler and set level to debug
        # 文件处理器
        ch = logging.FileHandler(filename='mylog.log', encoding="utf-8")
        ch.setLevel(logging.DEBUG)
        # create formatter
        # 定义格式器
        formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
        # add formatter to ch
        ch.setFormatter(formatter)
        # add ch to logger
        logger.addHandler(ch)
        return  logger
    logger = get_logger()
    def log_info(message):
        logger.info(message)
    logger.debug('debug message')
    # logger.info('info message')
    log_info(" log info 方法")
    logger.warning('warn message')
    logger.error('error message')
    logger.critical('critical message')
    

    日志配置文件
    https://ceshiren.com/t/topic/13564