Skip to main content

Use Python's logging instead of print to quickly output program information to the screen and log files, distinguish information levels by color, and include the ability to calculate program runtime.

Project description

MyLogger

Use Python's logging instead of print to quickly output program information to the screen and log files, distinguish information levels by color, and include the ability to calculate program runtime.

具备功能:

  1. 支持不同日志等级以不同颜色输出。
  2. 支持同时输出到屏幕及日志文件内。
  3. 支持常用的日志设置。
  4. 支持计算程序运行时间。
  5. 支持原生print函数,附带文件名和行号。

更新功能:

  1. 增加可动态设置输出到屏幕与保存到日志属性功能。
  2. 增加路径参数校验功能。

修复问题:

  1. 解决多文件存在引用的情况下,设置相同的Logger名称下,会同时输出多个多个logger信息的问题。
  2. 解决with_run_time显示的行号不准确的问题。

快速使用

安装:
pip install yan-logger

from yan-logger import MyLogger  # 导入

mg = MyLogger()
mg.debug('debug')
mg.info('info')
mg.warning('warning')
mg.error('error')
mg.critical('critical')

运行结果: 包含 日志器名称、运行时间点、运行文件名、输出所在行号。不同日志等级以不同颜色输出,见下图: image

进阶使用

可自定义输出到屏幕和保存到日志文件两种handler,分别独立设置级别。 启用保存到文件时,还可设置按日期循环及文件大小循环两种模式对文件进行切割。 具体参数设置见源码,也可参考RotatingFileHandler及TimedRotatingFileHandler的参数。

from yan-logger import MyLogger

mg = MyLogger("new_logger", file_path='log_demo.log')   # 自定义日志器名称,输出到屏幕且保存到log_demo.log文件中。  具体参数设置见源码。
mg.stream_logger_level= "warning"                       # 设置输出到屏幕的日志级别为"warning"
mg.file_logger_level = 20                               # 设置保存到日志文件的级别为 “info”, 级别设置用数字和单词均可。
mg.debug('debug')
mg.info('info')
mg.warning('warning')
mg.error('error')
mg.critical('critical')

运行结果: 在屏幕上只输出"warning" 级别以上的内容,在日志文件中是输出info级别内容。 image

关闭/启动输出到屏幕

mg = MyLogger("new_logger", file_path='log_demo.log')
mg.disable_stream()     # 关闭
mg.enable_stream()      # 启动

关闭/启动输出到屏幕

mg = MyLogger("new_logger", file_path='log_demo.log')
mg.disable_file()       # 关闭
mg.enable_file()        # 启动,可以自定义新的参数。

计算程序运行时间

from yan-logger import MyLogger

mg = MyLogger("new_logger")

# 方法一,用with mg.with_run_time()包裹需要计算的代码即可
with mg.with_run_time():
    for _ in range(3):
        mg.debug(_)
        mg.time.sleep(1)

# 方法二,使用装饰器mg.run_time(),默认参数1 只运行一遍函数。
@mg.run_time()
def fun():
    for _ in range(3):
        mg.debug(_)
        mg.time.sleep(1)

fun()

运行结果如下:

自动输出程序运行时间
image

集成小功能

  1. 集成实例静态print函数,增加显示文件名及行号。
  2. 类方法集成了sleep模块,不用多次导入。
with ml.with_run_time():
    MyLogger.print("开始计时……")         # 集成实例静态print函数,增加显示文件名及行号。
    MyLogger.time.sleep(3)              # 类方法集成了sleep模块,不用多次导入。
    MyLogger.print("with 模块运行结束。")

运行结果
image

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

yan_logger-0.1.3.tar.gz (10.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

yan_logger-0.1.3-py3-none-any.whl (9.3 kB view details)

Uploaded Python 3

File details

Details for the file yan_logger-0.1.3.tar.gz.

File metadata

  • Download URL: yan_logger-0.1.3.tar.gz
  • Upload date:
  • Size: 10.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for yan_logger-0.1.3.tar.gz
Algorithm Hash digest
SHA256 9e4487fa3e10cf0c105e9d37c98efebd801464ff5284193352556af02b5b30a9
MD5 cfc04183579b5600fc5beacf8c89098b
BLAKE2b-256 700ce9ba1e9c10e94d9be1880b9edec06b32883f922a231c439bc853bc74b7dc

See more details on using hashes here.

File details

Details for the file yan_logger-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: yan_logger-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 9.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for yan_logger-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 37416a6b3bb6f3a0920bbf18f3b88776fc3a24a3f3296bc5b3a88d6c9dde0229
MD5 c8770b7d68c8a1b7654ee50c107b1d29
BLAKE2b-256 0594904c4c9c4ba06091a54d5a1fca5ef485bc9160b32f237035c563d28fc790

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page