- 下面直接上代码,如何修改控制台日志输出和日期写入的格式
- 分享自己从作者的源码中查到的一些参数说明.用于格式化日志的输出
loguru的git网址,点我去
先看二次封装loguru后的结果如何.
- 控制台输出
- 日志写入
而默认的输出模式是这样的
我更改了其输出的格式使我们可以利用编辑器快速定位到问题所在
说明:loguru的格式化输出,控制台和日志写入是不同的,分开设置的.
以pycharm为例,我们鼠标选中模块名和方法名,Ctrl+Shift+N 即可在ALL中找到该方法,然后根据行号快速定位到日志出题所在.
前置条件:
pip install loguru
import sys
from loguru import logger
class MyLogger:
def __init__(self):
self.logger = logger
# 清空所有设置
self.logger.remove()
# 添加控制台输出的格式,sys.stdout为输出到屏幕;关于这些配置还需要自定义请移步官网查看相关参数说明
self.logger.add(sys.stdout,
format="{time:YYYYMMDD HH:mm:ss} | " # 颜色>时间
"{process.name} | " # 进程名
"{module} .{function} " # 模块名.方法名
":{line} | " # 行号
"{level} : " # 等级
"{message} ", # 日志内容
)
# # 输出到文件的格式,2022年4月20日09:18:44 关闭日志输出到文件,这里的add和上面的不冲突,两个add控制两个输出
# self.logger.add(log_file_path, level='DEBUG',
# format='{time:YYYYMMDD HH:mm:ss} - '
# '{module}.{function}:{line} - {level} -{message}',
# rotation="10 MB")
def get_logger(self):
return self.logger
my_logger = MyLogger().get_logger()
def ss():
my_logger.info(2222222)
my_logger.debug(2222222)
my_logger.warning(2222222)
my_logger.error(2222222)
my_logger.exception(2222222)
if __name__ == '__main__':
ss()
分享自己从作者的源码中查到的一些参数说明.用于格式化日志的输出
+------------+---------------------------------+----------------------------+
| Key | Description | Attributes |
+============+=================================+============================+
| elapsed | The time elapsed since the | See |timedelta| |
| | start of the program | |
+------------+---------------------------------+----------------------------+
| exception | The formatted exception if any, | ``type``, ``value``, |
| | ``None`` otherwise | ``traceback`` |
+------------+---------------------------------+----------------------------+
| extra | The dict of attributes | None |
| | bound by the user (see |bind|) | |
+------------+---------------------------------+----------------------------+
| file | The file where the logging call | ``name`` (default), |
| | was made | ``path`` |
+------------+---------------------------------+----------------------------+
| function | The function from which the | None |
| | logging call was made | |
+------------+---------------------------------+----------------------------+
| level | The severity used to log the | ``name`` (default), |
| | message | ``no``, ``icon`` |
+------------+---------------------------------+----------------------------+
| line | The line number in the source | None |
| | code | |
+------------+---------------------------------+----------------------------+
| message | The logged message (not yet | None |
| | formatted) | |
+------------+---------------------------------+----------------------------+
| module | The module where the logging | None |
| | call was made | |
+------------+---------------------------------+----------------------------+
| name | The ``__name__`` where the | None |
| | logging call was made | |
+------------+---------------------------------+----------------------------+
| process | The process in which the | ``name``, ``id`` (default) |
| | logging call was made | |
+------------+---------------------------------+----------------------------+
| thread | The thread in which the | ``name``, ``id`` (default) |
| | logging call was made | |
+------------+---------------------------------+----------------------------+
| time | The aware local time when the | See |datetime| |
| | logging call was made | |
+------------+---------------------------------+----------------------------+
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)