小的无依赖性类,使日志记录更容易
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安装
选项2-手动克隆并安装 代码有很好的文档记录,您可以在LogHelper.py中找到所有使用文档。 所有函数都带有类型注释,并带有详细的pydoc块注释。如果您使用的是python优化的ide,比如PyCharm,那么它应该
使用包时提供代码完成和帮助。 最基本的用法是简单地初始化没有参数的类,并附加一个
将日志输出发送到文件的文件处理程序。 运行以上命令后, 第三方包通常使用自己的日志实例名称。以便于您复制设置
对于其他实例,可以使用 将设置复制到命名记录器后,使用该记录器的所有日志记录都应使用指定的处理程序,
以及你的日志级别。 这包括通过 可以更容易地筛选日志的方法是将正常的调试日志从警告和错误中分离出来。 使用标准的 使用python日志助手,只需几行代码。 运行上述python脚本后,您将看到以下日志文件: debug.log error.log 如您所见, 也可以进行无痛的原木旋转,无需比如 loghelper类有一个函数 只需指定要旋转的间隔( 有关 默认情况下,日志每天循环一次(间隔=1)(当'd'时),并在14天后删除(备份=14)。 现在我们可以看到它生成了两个带时间戳的日志,因为间隔设置为1分钟。 如果这个项目对您有帮助,请考虑grabbing a VPS or Dedicated Server from Privex-价格最低为8美元/月(我们采用加密货币!)pip3 install git+https://github.com/Privex/python-loghelper
# 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
用法
基本用法
# 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行代码,而且重复性很强。# 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')
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
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
。add_timed_file_handler
,它包装了logging.handler.TimedRotatingFileHandler
。when
)的类型、旋转的频率(interval
)以及
希望在删除旧版本之前保留(backups
)。如果不想删除旧日志,请将backups
设置为0。interval
、when
和backups
的更多信息,
见the official logging docs# 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')
$ 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
谢谢你的阅读!
推荐PyPI第三方库