• Python logging 模块,日志


    logging 模块,计算机的日志记录。

    它是一个线程安全的记录模块。

    当多个计算机来对服务器写入日志的时候。每台计算机都需要对服务器上的文件做, 打开文件 ,写入文件 ,保存文件 的操作。由于每台计算机的记录时间是不同的。这就需要保证线程的安全。

    logging 模块在内部就保证了这一功能。

    一,单个文件的记录。

     1 import logging
     2 
     3 """
     4 CRITICAL = 50
     5 FATAL = CRITICAL
     6 ERROR = 40
     7 WARNING = 30
     8 WARN = WARNING
     9 INFO = 20
    10 DEBUG = 10
    11 NOTSET = 0
    12 """
    13 
    14 logging.basicConfig(filename='logname.log',
    15                     format='%(asctime)s - %(name)s - %(levelname)s - %(module)s: %(message)s',
    16                     datefmt='%Y-%m-%d %H:%M:%S %p',
    17                     level=logging.INFO)
    18 logging.critical("this is a critical issue")
    19 logging.log(10, 'this is noset issue')
    20 logging.debug("ABC")

    二,多个文件的日志:

    # !/usr/bin/env python
    # -*- coding:utf8 -*-
    import logging
    
    # 创建文件
    file_1_1 = logging.FileHandler('1-1.log', 'a')
    # 创建格式
    fmt = logging.Formatter(fmt="%(asctime)s - %(name)s - %(levelname)s - %(module)s:%(message)s",
                            datefmt='%Y-%m-%d %H:%M:%S %p')
    # 文件应用格式
    file_1_1.setFormatter(fmt)
    
    file_2_2 = logging.FileHandler('2-2.log', 'a')
    # fmt = logging.Formatter()
    file_2_2.setFormatter(fmt)
    
    lgger1 = logging.Logger('Simon', level=logging.ERROR)
    
    lgger1.addHandler(file_1_1)
    lgger1.addHandler(file_2_2)
    
    # 写日志
    lgger1.critical('1111')
    lgger1.log(200,'这是一个level 200 的错误')
  • 相关阅读:
    MySQL中的事务
    MySQL中的锁
    MySQL查询更新所有满足条件的数据
    MySQL存储引擎
    MySQL架构
    MySQL中存储json格式数据
    Java反射破坏单例模式
    合唱队(华为OJ)
    Java实现生产者消费者问题
    Spring IOC + AOP 的实现
  • 原文地址:https://www.cnblogs.com/xuwenwei/p/14408239.html
Copyright © 2020-2023  润新知