Skip to main content

日志助手,支持多进程,支持json格式,支持flask

Project description

日志助手

特性

  1. 几乎无需配置,就可生成日志文件
  2. 可选,输出到文件的同时打印到屏幕
  3. 可选(安装python-json-logger即可),生成纯文本日志的同时生成一份json格式的日志,方便采集
  4. 可选(安装flask即可),捕获flask的remote_addr和url信息
  5. 可选(安装concurrent-log),支持多进程并发记录日志

创建logger

# 简易用法
from loghelper import LogHelper

log_helper = LogHelper(log_dir='my_project', log_path='/tmp/')

logger = log_helper.create_logger()

logger.info('Hello World')
# 完整用法
import logging
from loghelper import LogHelper, ConcurrentTimedRotatingFileHandler

log_helper = LogHelper(
    log_dir='my_project', log_path = '/var/log/',
    backup_count=7, when='midnight',
    timed_rotating_file_handler=ConcurrentTimedRotatingFileHandler
)

logger = log_helper.create_logger(
    name='my_module',
    filename='my_module',
    add_stream_handler = True,
    json_ensure_ascii = False,
    reserved_attrs = [
        'msg',
        'args',
        'levelno',
        'relativeCreated',
    ],
    level=logging.INFO
)

logger.info('Hello World')

生成日志样例

纯文本格式 /tmp/my_project/raw_loghelper.loghelper.Runtime.log

<2021-12-15 14:40:38,844> INFO (<stdin> <module> 1) {13064 4474015232 MainThread} [- 127.0.0.1] Hello World

json格式 /tmp/my_project/json_loghelper.loghelper.Runtime.log

{"message": "Hello World", "name": "loghelper.loghelper", "levelname": "INFO", "pathname": "<stdin>", "filename": "<stdin>", "module": "<stdin>", "exc_info": null, "exc_text": null, "stack_info": null, "lineno": 1, "funcName": "<module>", "created": 1639550438.844173, "msecs": 844.1729545593262, "thread": 4474015232, "threadName": "MainThread", "processName": "MainProcess", "process": 13064, "url": "-", "remote_addr": "127.0.0.1", "asctime": "2021-12-15 14:40:38,844", "timestamp": "2021-12-15T06:40:38.844173+00:00"}

仅日志记录敏感信息(如堆栈,异常)

from loghelper import SensitiveLogger


logger = SensitiveLogger(logger)
msg = logger.error(msg='发生错误', sensitive_msg=f'{ex}')

生成msg样例

发生错误 (详见日志:['/tmp/my_project/raw_loghelper.loghelper.Runtime.log', '/tmp/my_project/json_loghelper.loghelper.Runtime.log'])

生成日志样例

发生错误 (错误详情:ex的具体内容)

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

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

loghelper-1.0.9-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file loghelper-1.0.9-py3-none-any.whl.

File metadata

  • Download URL: loghelper-1.0.9-py3-none-any.whl
  • Upload date:
  • Size: 5.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.1 CPython/3.10.5 Darwin/22.5.0

File hashes

Hashes for loghelper-1.0.9-py3-none-any.whl
Algorithm Hash digest
SHA256 130729176591ecfdc69f96c9fd1a8a27a1a9a29eb1a32b788aa9c85a1b067782
MD5 7c1f4f52d3010ddf124ebf5983a246d6
BLAKE2b-256 353ebacda6a8601ee9a2bbb8e17d080aabffe04d655dcac1724dd39ce94d009b

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