Skip to main content

pysimple-log

你还为 配置日志而烦恼吗? 你还为一些不同的日志格式而痛苦吗? 那么现在来使用这个库吧。 这个库的目的简化 繁琐的日志配置,简单,省时。

概要

使用 logging 进行封装了一下 日志格式进行封装, 同时使用 concurrent_log_handler 来完成多进程安全的写日志

特性

  • 对日志格式进行了默认格式的定义
  • 使用 concurrent_log_handler库来保证多进程写日志的安全性
  • 对日志进行切割,防止日志太大,不利于查看文件
  • 对日志切割后,可以设置保留几份

安装

Install and update using pip:

pip install pysimple-log

快速开始

from simplelog import logger  

logger.info("this is test")
logger.error("this is test")
logger.warning("this is test")

定制化logger

    import logging
    from  simplelog import  Logger
    log = Logger(name='test',filename='app.log',level=logging.DEBUG)
    
    logger = log.get_logger()

    logger.debug("this is test")
    logger.warning("this is test")
    logger.info("this is test")
    logger.error("this is test")
    logger.exception("this is test")

结果如下:

[2020-03-06 22:53:57 WARNING/33060] test <input>:<module>:9 this is test
[2020-03-06 22:53:57 INFO/33060] test <input>:<module>:10 this is test
[2020-03-06 22:53:57 ERROR/33060] test <input>:<module>:11 this is test
[2020-03-06 22:53:57 ERROR/33060] test <input>:<module>:12 this is test
NoneType: None

高级的一些用法

核心的类 Logger

参数介绍

    :param name: logger's name  日志的名称,如果不指定 有默认值 simple_log
                
    :param filename: 文件路径 '/aaa/bbb/file.log' ,日志文件的路径,如果没有提供这个值,
                     则不写入文件,直接输出日志 到控制台。
                    
    :param log_fmt: 日志的格式 ,默认格式如下:
     [日期 时间  日志级别/进程号] logger的名称 文件名称 函数名:行号 打印日志内容
    [2020-03-09 18:02:21 INFO/39044] simplelog test_basic.py:<module>:14 hello world

    :param date_fmt: 日期的格式 ,使用指定的时间格式,默认格式 '%Y-%m-%d %H:%M:%S'

    :param backup_count: 对日志切割后 可以设置保留几份,默认保留5份

    :param max_bytes: 超过 max_bytes 将会陪切割,默认值:100M, 单位是 字节

    :param level: logging.INFO ,logging.DEBUG , 默认级别:INFO 
                   日志级别参考logging 模块
                   CRITICAL = 50
                   FATAL = CRITICAL
                   ERROR = 40
                   WARNING = 30
                   WARN = WARNING
                   INFO = 20
                   DEBUG = 10

使用方法

    import logging
    from  simplelog import  Logger
    # 实例化 Logger 对象
    log = Logger(name='app',filename='app.log',level=logging.INFO)
    
    # 获取 logger 对象
    logger = log.get_logger()
    
    logger.info("hello world")
    logger.warning("hello world")
    logger.exception("hello world")
    logger.error("hello world")

设置 日志级别 set_level

    import logging
    from simplelog import Logger
    from simplelog import set_level
    # 实例化 Logger 对象
    log = Logger(name='app',filename='app.log',level=logging.INFO)
    
    #获取 logger 对象
    logger = log.get_logger()

    logger.debug('hello world 1')
    logger.info('hello world 2')
    logger.warning('hello world 3')
    logger.error('hello world 4')

    set_level(name=log.name,level=logging.WARNING)
    logger.debug('hello world 1')
    logger.info('hello world 2')
    logger.warning('hello world 3')
    logger.error('hello world 4')

    set_level(name=log.name,level=logging.ERROR)
    logger.debug('hello world 1')
    logger.info('hello world 2')
    logger.warning('hello world 3')
    logger.error('hello world 4')

捕获堆栈信息 设置 exc_info = True

    
    import logging
    from simplelog import Logger
    # 实例化 Logger 对象
    log = Logger(name='app',filename='app.log',level=logging.INFO)

    logger = log.get_logger()
    # exc_info = True 
    logger.error("This is error ",exc_info=True)

输出json的格式的日志

    import logging
    from simplelog import Logger,JSONFormatter
    # 实例化 Logger 对象
    log = Logger(name='app',level=logging.INFO,json_formatter=JSONFormatter)

    logger = log.get_logger()
    logger.propagate = False

    logger.info("hello world",extra={'key1':'hello world'})
    logger.warning("hello world")
    logger.exception("hello world")
    logger.error("hello world")

贡献

如果你对这个项目感兴趣,非常欢迎可以一起维护这个项目。 如果你在使用的过程发现什么问题,可以联系我。

链接

Metadata

Release files for pysimple-log 0.0.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pysimple-log 0.0.5
File Size Uploaded
pysimple-log-0.0.5.tar.gz 8.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pysimple-log 0.0.5
File Interpreter ABI Platform
pysimple_log-0.0.5-py3-none-any.whl Python 3 none any Details

Total release size: 20.2 kB

Release files / pysimple-log-0.0.5.tar.gz

Download URL pysimple-log-0.0.5.tar.gz
Size 8.8 kB
Tags Source
SHA-256 checksum
How to use checksums
c0979a1a93e687c3ada0d25ca8156e1f4c73f99b08510c1418ce9e76149ce4f9
BLAKE2b-256 checksum
How to use checksums
5d862887d6eea6ea59f5469fa86817cde9017260ca001721cd3227997a42021e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.8.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.2

Release files / pysimple_log-0.0.5-py3-none-any.whl

Download URL pysimple_log-0.0.5-py3-none-any.whl
Size 11.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4f943acecd1fcfbdd5141ef25be292851893cbbc3e7876419d5f7a6eb65bf428
BLAKE2b-256 checksum
How to use checksums
e72de12773ba64e58d2365a8b926b9d5fa04819fb09c88cbad2590800a79b710
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.8.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.2

Release history Release notifications | RSS feed

This release

0.0.5 This release

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page