小的无依赖性类,使日志记录更容易

privex-loghelper的Python项目详细描述


python日志助手

python日志助手是一个小类,旨在简化内置python日志模块的使用。

它没有依赖项,并且应该与大多数python 2.x和3.x版本兼容(尽管我们仍然建议最低3.4版本)。

它最初由Chris (Someguy123)创建,用于个人python项目, 以及在Privex Inc.

开发的项目中使用

如果这个项目帮助了您,请考虑grabbing a VPS or Dedicated Server from Privex-价格最低为8美元/月(我们采用加密货币!)

许可证

python日志助手Privex Inc. of Belize City创建,并根据x11/mit许可证获得许可。 有关许可证文本,请参见文件LICENSE

tl;dr;许可证:

我们不提供担保。您可以复制、修改它,在具有不同许可证的项目中使用它,甚至在商业(付费)软件中也可以使用它。

最重要的规则是-you必须在任何副本中保持原始许可文本可见(请参见LICENSE)。

贡献

我们很乐意接受拉取请求,不管请求有多小。

请确保您所做的任何更改都符合以下基本要求:

  • 没有其他依赖项。我们希望loghelper是轻量级的,安装起来也不费力。
  • 从其他项目获取的任何代码都应与麻省理工学院许可证兼容
  • 这是一个新项目,因此,支持3.4之前的python版本的优先级非常低。
  • 但是,我们很高兴接受prs来提高与旧版本python的兼容性,只要它不:
      大大增加了代码的复杂度。
    • 或者给那些更新版本的python带来问题。

安装

使用pip

从pypi下载并安装

python 3

pip3 install privex-loghelper

python 2

pip install privex-loghelper

(可选)从git手动安装

选项1-使用pip直接从github安装

pip3 install git+https://github.com/Privex/python-loghelper

选项2-手动克隆并安装

# Clone the repository from Github
git clone https://github.com/Privex/python-loghelper
cd python-loghelper

# RECOMMENDED INSTALL METHOD# Use pip to install the source code
pip3 install .

# ALTERNATIVE INSTALL METHOD# If you don't have pip, or have issues with installing using it, then you can use setuptools instead.
python3 setup.py install

用法

代码有很好的文档记录,您可以在LogHelper.py中找到所有使用文档。

所有函数都带有类型注释,并带有详细的pydoc块注释。如果您使用的是python优化的ide,比如PyCharm,那么它应该 使用包时提供代码完成和帮助。

Screenshot of PyCharm code completion and docs

基本用法

最基本的用法是简单地初始化没有参数的类,并附加一个 将日志输出发送到文件的文件处理程序。

# Import the classfromprivex.loghelperimportLogHelper# The first param is logger_name - a hierarchical dot-separated name to organise loggers.# If it's not specified, or is None, it will use the root logger (which will affect some# third-party packages that don't have their own logging settings)lh=LogHelper('mylogger')# Log to a file called test.log in the current directorylh.add_file_handler('test.log')# Grab the logger instancelog=lh.get_logger()# Now let's log 'hello world' to the file.log.info('hello world')

运行以上命令后,test.log应该包含:

2018-12-05 22:05:18,915 root         INFO     hello world

将日志配置复制到其他日志记录器名称

第三方包通常使用自己的日志实例名称。以便于您复制设置 对于其他实例,可以使用copy_logger(name)方法。

# Set up logging for your app, log anything >=INFOlh=LogHelper('myapp',handler_level=logging.INFO)# Log to a file called test.log in the current directory (note: absolute path is better)lh.add_file_handler('test.log')# Now copy your logging level, handlers, and formatting to the logger privex.jsonrpclh.copy_logger('privex.jsonrpc')# You can specify multiple logger names as positional arguments. All specified loggers will# have their handlers replaced with yours, and have their logging level set to match.lh.copy_logger('example.app','otherexample')

将设置复制到命名记录器后,使用该记录器的所有日志记录都应使用指定的处理程序, 以及你的日志级别。

这包括通过logging.getLogger('loggername')和python日志包装器访问记录器的模块。 比如女贞女侍者。

将错误和调试日志拆分为不同的文件

可以更容易地筛选日志的方法是将正常的调试日志从警告和错误中分离出来。

使用标准的logging模块,这可能需要10行代码,而且重复性很强。

使用python日志助手,只需几行代码。

# Import the classfromprivex.loghelperimportLogHelper# Import logging to be able to specify the verbosity levelsimportlogging# The global level (kwarg `level`) defaults to DEBUG, but the default handler level is only INFO.# You can specify handler_level to change that. # To help prevent conflicts from other python packages which use the root ('') logger, you should set a custom# logger name.lh=LogHelper(logger_name='my_app',handler_level=logging.DEBUG)# Log messages that are DEBUG (default handler and global logging level) or higher to `debug.log`lh.add_file_handler('debug.log')# Log only WARNING or above to `error.log`lh.add_file_handler('error.log',level=logging.WARNING)# Grab the logger instancelog=lh.get_logger()# To test that logs are being sent to the correct files, let's try an `info`, a `warning` and an `error` log message.log.info('something normal is happening')log.warning('something strange is happening')log.error('something VERY BAD is happening')

运行上述python脚本后,您将看到以下日志文件:

debug.log

2018-12-05 22:10:25,253 root         INFO        something normal is happening
2018-12-05 22:10:25,254 root         WARNING     something strange is happening
2018-12-05 22:10:25,256 root         ERROR       something VERY BAD is happening

error.log

2018-12-05 22:10:25,254 root         WARNING     something strange is happening
2018-12-05 22:10:25,256 root         ERROR       something VERY BAD is happening

如您所见,debug.log保存了所有消息,而error.log只保存了警告和错误。

对数旋转

也可以进行无痛的原木旋转,无需比如logrotated

loghelper类有一个函数add_timed_file_handler,它包装了logging.handler.TimedRotatingFileHandler

只需指定要旋转的间隔(when)的类型、旋转的频率(interval)以及 希望在删除旧版本之前保留(backups)。如果不想删除旧日志,请将backups设置为0。

有关intervalwhenbackups的更多信息, 见the official logging docs

默认情况下,日志每天循环一次(间隔=1)(当'd'时),并在14天后删除(备份=14)。

# Import the classfromprivex.loghelperimportLogHelper# Using sleep to simulate time passingfromtimeimportsleeplh=LogHelper()# Log to a file called test.log in the current directory# Rotate the log every minute, and only keep up to 60 minutes of logslh.add_timed_file_handler('test.log',when='M',interval=1,backups=60)# Grab the logger instancelog=lh.get_logger()# Now let's log 'hello world' to the file.log.info('hello world')log.info('hello world 2')sleep(60)log.info('hello world 3')

现在我们可以看到它生成了两个带时间戳的日志,因为间隔设置为1分钟。

$ ls -l
    -rw-r--r--  1 user  users    585 Dec 22:58 test.log.2018-12-05_22-58
    -rw-r--r--  1 user  users    585 Dec 22:59 test.log.2018-12-05_22-59

$ cat test.log.2018-12-05_22-58
    2018-12-05 22:58:24,600 root         INFO     hello world
    2018-12-05 22:58:24,743 root         INFO     hello world 2

$ cat test.log.2018-12-05_22-59
    2018-12-05 22:59:25,512 root         INFO     hello world 3

谢谢你的阅读!

如果这个项目对您有帮助,请考虑grabbing a VPS or Dedicated Server from Privex-价格最低为8美元/月(我们采用加密货币!)

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
身份验证如何在Java中使用REST get方法?   java JTextField未显示超/下标   多线程如何在Java的2个计时器任务中可靠地创建一个锁(因此在其他任务运行时将等待)   JavaSpring集成应用程序和缓存   泛型用法和java标准   java 3 csv文件,对每个文件求和,然后对所有文件求和   java枚举及其工作方式   Java中不可解析的日期错误   AES 128 ISO10126的java初始化向量   java使用MarkupBuilder和Groovy。。。创建XML参数时出现问题   java未知标记(c:foreach)。日蚀   java ant可运行jar不工作   java为什么带ExecutorService和不带ExecutorService的任务需要相同的时间?   java相当于其他流行语言中的后期静态绑定(PHP)   Vaadin如何将Spring java对象实例作为单例行为   java需要一个关于应用程序类的解释   优化Java JIT循环展开策略?   java无法将xmlReader强制转换为解析器   Spring应用程序中的java Riak