用于日志记录的包装器模块,具有开箱即用的日志轮换和严重错误邮件功能
autopylogger的Python项目详细描述
自动定位器
Python线程安全日志包装器模块,提供开箱即用的日志轮换和关键日志邮寄功能,可在任何Python脚本中实现无缝集成。在
要求:日志记录
基本用法:
- 在
通过调用“init\u logging”函数(关闭邮件功能)初始化logger对象
在from autopylogger import init_logging my_logger_obj = init_logging(log_name='my_logs', log_directory='logs_dir')
- 在
通过调用'init\u logging'函数(邮件功能打开)初始化logger对象
^{pr2}$注意:从v2020.2.11以上的版本开始,默认情况下会启用严重错误邮件功能
在 - 在
使用该对象编写调试|信息|警告|错误日志
在my_logger_obj.debug('This is DEBUG log') my_logger_obj.info('This is INFO log') my_logger_obj.warning('This is WARNING log') my_logger_obj.error('This is ERROR log') my_logger_obj.critical('This is CRITICAL log. Mail will be sent with this message')
重要标志:
- 在
设置日志级别:将适当的值传递给“logаlevel”标志。
在log_level = 'DEBUG' | 'INFO' | 'WARNING' | 'ERROR'
- 在
打开/关闭控制台日志:将适当的值传递给“console\ulog”标志。
console_log = True | False
注意:建议在程序作为服务运行时关闭生产环境中的控制台日志记录,以防止使用信息消息刷新系统控制台日志。在linux系统中。
在 - 在
{str{/str}适当的{/str}设置{/str}适当的{/str}标准。
rotation_criteria = 'size' | 'time' | 'timeandsize'
默认情况下,旋转条件“size”用于最大文件大小200 MB。在
- 在
选择基于“size”的旋转条件时,传递参数值'max\u bytes'以指定日志文件的最大大小(以字节为单位)。在
在'rotation_criteria' = 'size' 'max_bytes' = 5*1024*1024
- 在
当选择基于“time”的旋转条件时,请传递参数值“rotate\”When“&;”rotate\\ interval“。在
在'rotation_criteria' = 'time' 'rotate_when' = 'd' | 'h' | 'm' | 's' 'rotate_interval' = 1 (for rotating log every 1 day|hour|minute|second)
- 在
当选择基于“timeandsize”的旋转条件时,传递参数值“rotate_When”,“rotate_interval”&;'max\u bytes'。在
在'rotation_criteria' = 'timeandsize' 'max_bytes' = 5*1024*1024 'rotate_when' = 'd' | 'h' | 'm' | 's' 'rotate_interval' = 1 (for rotating log every 1 day|hour|minute|second)
注意:在“timeandsize”旋转条件中,当满足时间或大小约束时,文件将被旋转。“
在 - 在
- 在
打开/关闭关键邮件:将布尔值(TRUE | FALSE)传递给“启用邮件”标志。默认情况下,对严重错误启用邮件功能。
- 在
当“enable\u mail”设置为True时,需要以下标志。在
在'mailfrom_addr' = '<Sender email address>' 'mailto_addr' = '<Receiver email address>' 'mail_subject' = '<Mail subject>' 'mail_credentials' = ('<username>', '<password>') or None if no authentication is required.
- 在
- 在
正在设置日志格式:将所需的日志格式字符串传递给“log_format”标志
在log_format='[%(asctime)s] -- %(levelname)s - %(filename)s -- %(funcName)s - Line no - %(lineno)d -- %(message)s'
日志格式化程序参数:
Format | Description |
---|---|
%(asctime)s | Human-readable time when the LogRecord was created. By default this is of the form ‘2003-07-08 16:49:45,896’ (the numbers after the comma are millisecond portion of the time). |
%(created)f | Time when the LogRecord was created (as returned by time.time()). |
%(filename)s | Filename portion of pathname. |
%(funcName)s | Name of function containing the logging call. |
%(levelname)s | Text logging level for the message ('DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL') |
%(lineno)d | Source line number where the logging call was issued (if available). |
%(message)s | The logged message, computed as msg % args. This is set when Formatter.format() is invoked. |
%(msecs)d | Millisecond portion of the time when the LogRecord was created. |
%(name)s | Name of the logger used to log the call. |
%(pathname)s | Full pathname of the source file where the logging call was issued (if available). |
%(process)d | Process ID (if available). |
%(processName)s | Process name (if available). |
%(thread)d | Thread ID (if available). |
%(threadName)s | Thread name (if available). |
- 项目
标签: