Skip to main content

Andersen

一个Python的日志库的封装

使用方式

安装

pip install andersen

代码使用

直接使用

import andersen


andersen.debug('debug')
andersen.info('info')
andersen.warn('warn')
andersen.error('error')

系统自带了一个输出到控制台的handler,日志级别为info,因此上边的代码会输出: 默认日志

自定义配置

可以自己指定配置的文件,有两种指定方式:环境变量和函数指定

指定环境变量

import os
os.environ.setdefault('ANDERSEN_CONFIG', 'log_config.toml')
# 必须在导入前设置环境变量
import andersen
andersen.debug('debug')
andersen.info('info')
andersen.warn('warn')
andersen.error('error')

函数加载

import andersen
andersen.init('log_config.toml')

andersen.debug('debug')
andersen.info('info')
andersen.warn('warn')
andersen.error('error')

目前只支持toml的自定义配置文件,示例文件如下

[log.common]
    # 可选参数
    name = "common"

    # 必要参数
    level = "info"

    # 可选参数,这里如果配置的话则handler处没有配置的话会到这里查找。如果都找不到则会抛出异常
    format = "[%(asctime)s] [%(levelname)s] %(message)s"

# 输出到控制台的配置
[[log.common.handlers]]
    type = "std"
    # 可选参数
    format = "%(asctime)s %(levelname)s %(message)s"
    level = "info"

# 输出到文件的配置
[[log.common.handlers]]
    type = "file"
    # 可选参数
    format = "%(asctime)s %(levelname)s %(message)s"
    level = "info"

    # 必要参数
    log_file = "log/common.log"

# 输出到按大小迭代的文件的配置
[[log.common.handlers]]
    type = "rotate"
    # 可选参数
    format = "%(asctime)s %(levelname)s %(message)s"
    level = "info"

    # 必要参数
    log_file = "log/common1.log"
    max_bytes = 1024

# 输出到按时间迭代的文件的配置
[[log.common.handlers]]
    type = "time_rotate"
    # 可选参数
    format = "%(asctime)s %(levelname)s %(message)s"
    level = "info"

    # 必要参数
    log_file = "log/common2.log"
    when = 'h'

# 如果需要配置另一个,修改log后边的key名称即可,然后需要配置哪些类型的handler,从上边复制后修改即可
[log.sample]
    name = "sample"
    level = "debug"
    format = "[%(asctime)s] [%(levelname)s] %(message)s"

# 输出到控制台的配置
[[log.sample.handlers]]
    type = "std"
    # 可选参数
    format = "%(asctime)s %(levelname)s %(message)s"
    level = "debug"

相关函数

日志配置

init(conf=None, default_logger='common')

初始化函数。可重复调用,只会实际执行一次。

  • conf 如果为None则从环境变量读取配置文件,如果不存在则使用默认配置,即创建一个输出到控制台的日志对象
  • default_logger 初始化后默认使用的日志对象
generate_sample_config(filename)

将示例配置写入一个toml文件。写入内容见上文

  • filename 目标文件
get_logger(key='common')

获取日志对象。key必须在配置文件里已经定义。返回的是一个python的logging.Logger对象

  • key 日志的key值,即配置中log后边所跟的参数,默认是common

日志输出

debug(*args, **kwargs)

输出调试信息。kwargs中可选参数

  • logger 要使用哪个日志输出信息,未指定的话使用默认日志输出
  • sep 默认分隔符,如果指定该参数那么下边三个参数的默认值是该值
  • list_sep 列表分隔符,即使用该参数连接参数args
  • para_sep 参数分隔符,即使用该参数连接args和kwargs生成的字符串
  • dict_sep 字典分隔符,即使用该参数连接参数kwargs
info(*args, **kwargs)

输出信息。kwargs中可选参数

  • logger 要使用哪个日志输出信息,未指定的话使用默认日志输出
  • sep 默认分隔符,如果指定该参数那么下边三个参数的默认值是该值
  • list_sep 列表分隔符,即使用该参数连接参数args
  • para_sep 参数分隔符,即使用该参数连接args和kwargs生成的字符串
  • dict_sep 字典分隔符,即使用该参数连接参数kwargs
warn(*args, **kwargs)

输出警告信息。kwargs中可选参数

  • logger 要使用哪个日志输出信息,未指定的话使用默认日志输出
  • sep 默认分隔符,如果指定该参数那么下边三个参数的默认值是该值
  • list_sep 列表分隔符,即使用该参数连接参数args
  • para_sep 参数分隔符,即使用该参数连接args和kwargs生成的字符串
  • dict_sep 字典分隔符,即使用该参数连接参数kwargs
error(*args, **kwargs)

输出错误信息。kwargs中可选参数

  • logger 要使用哪个日志输出信息,未指定的话使用默认日志输出
  • sep 默认分隔符,如果指定该参数那么下边三个参数的默认值是该值
  • list_sep 列表分隔符,即使用该参数连接参数args
  • para_sep 参数分隔符,即使用该参数连接args和kwargs生成的字符串
  • dict_sep 字典分隔符,即使用该参数连接参数kwargs

Metadata

Release files for andersen 0.0.1

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

Source distribution (sdist)

Source distribution for andersen 0.0.1
File Size Uploaded
andersen-0.0.1.tar.gz 6.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for andersen 0.0.1
File Interpreter ABI Platform
andersen-0.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 16.8 kB

Release files / andersen-0.0.1.tar.gz

Download URL andersen-0.0.1.tar.gz
Size 6.7 kB
Tags Source
SHA-256 checksum
How to use checksums
3e0d4fad18af47324d6242fffa70c0f51c49421852fecf4d54881f1ac40d0c51
BLAKE2b-256 checksum
How to use checksums
66601d5e8cc6f27247b28f9697c504da83b77286a1fafef95110374eb0a860e2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.3

Release files / andersen-0.0.1-py3-none-any.whl

Download URL andersen-0.0.1-py3-none-any.whl
Size 10.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
44430386606e387c4ad672b236baf8dbf06f9843a5e7d34f380766228b49e464
BLAKE2b-256 checksum
How to use checksums
54a40f4545a019762d74a62002d335d3a61c517fc9548f0137fd6dca794d6a2e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.3

Release history Release notifications | RSS feed

This release

0.0.1 This release

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