Skip to main content

一个全局拦截日志并转为loguru日志的插件

Package version Supported Python versions

安装

pip install use-logger

使用

  • 直接使用
from use_logger import useLogger

useLogger()  # 使用默认配置
  • 如果你是usepy用户
from usepy.plugin import useLogger

useLogger()  # 使用默认配置

如果你自身项目正在使用loguru,这一切似乎感觉毫无变化。因为默认的配置只是修改了一点输出样式。

如果想要感受它带来的“魔法”,需要稍微配置一下。

from usepy.plugin import useLogger

useLogger(packages=["scrapy", "django", "usepy"])
Logstash/Filebeat

日志的更重要能力是将日志记录发送到Logstash/Filebeat,这样就可以将日志记录存储到Elasticsearch 中,方便进行日志分析。所以统一日志的最终输出格式是非常重要的。

useLogger内置一个logstash_handler统一化输出格式。

from loguru import logger
from usepy.plugin import useLogger, useLoggerHandlers

useLogger(
    handlers=[
        useLoggerHandlers.logstash_handler(level="DEBUG", extra={"app_name": "spider"})
    ],
    packages=["usepy"],  # hook拦截 usepy 的日志
    extra={"project_name": "usepy"}
)
logger.warning("test warning")
logger.info("test info")
logger.debug("test debug")
Uvicorn/FastAPI

使用 uvicorn 作为 FastAPI 的服务器,那么你可以使用 useLogger 来拦截 uvicorn 的日志。 (如果使用的是 gunicorn, 那么只要指定 class 为 uvicorn.workers.UvicornWorker 即可)

fr

Metadata

Release files for use-logger 0.1.9

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

Source distribution (sdist)

Source distribution for use-logger 0.1.9
File Size Uploaded Provenance
use_logger-0.1.9.tar.gz 4.9 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for use-logger 0.1.9
File Interpreter ABI Platform
use_logger-0.1.9-py3-none-any.whl Python 3 none any Details

Total release size: 11.3 kB

Release files / use_logger-0.1.9.tar.gz

Download URL use_logger-0.1.9.tar.gz
Size 4.9 kB
Tags Source
SHA-256 checksum
How to use checksums
c5b47d9c30885667734eedf9ba84a8fef740c4be2ea0edc511910729f792d897
BLAKE2b-256 checksum
How to use checksums
7c6983814b5180f1a8c04434833e14a272d718d1515469f5d08af764e8178ff1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.7.1 CPython/3.10.13 Linux/6.2.0-1018-azure

Release files / use_logger-0.1.9-py3-none-any.whl

Download URL use_logger-0.1.9-py3-none-any.whl
Size 6.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8e2baac3fc02e10ed3e4d3c928769098f7fdaea15ea656f1fdbb6c85c4ecad3b
BLAKE2b-256 checksum
How to use checksums
0bbd6424397d41abe02438bf5cc27f9ec194a2792380122c0c0b6a13e3a4d351
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.7.1 CPython/3.10.13 Linux/6.2.0-1018-azure

Security for use-logger 0.1.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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