jitesoft / loggers
PSR-3 日志记录器的集合。
Requires
- php: >=8.1
- nesbot/carbon: ^2.61
- psr/log: >=3.0.0
Requires (Dev)
- ext-json: *
- ext-pdo: *
- mikey179/vfsstream: 1.6.*
- php-mock/php-mock-phpunit: 2.6.*
- phpunit/phpunit: ^9.5
- squizlabs/php_codesniffer: ^3.7
Suggests
- ext-json: Required for most loggers using json parsing.
- ext-pdo: Required to use the PDO Logger.
README
此仓库包含一组实现 PSR-3 日志记录器接口的日志记录器。
欢迎 Pull-requests 和功能请求。
Php7 和 Php8
v2.x 标签需要 php 7+
v3.x 标签需要 php 8+
v4.x 标签需要 php 8.1+
仅维护最新版本(目前为 php8.1+)。
实现的日志记录器
所有日志记录器都能通过 setLogLevel
方法设置它们实际应记录的日志级别。
StdLogger
将日志输出到 stdout 和 stderr。
可以通过实例类中的 setFormat
和 setTimeFormat
方法更改输出格式和时间戳的格式。
FileLogger
FileLogger 可能是最有用的日志记录器。它所做的只是将输出打印到所选文件(默认为 /tmp/log.txt
)。
与 StdLogger
类似,可以更改输出格式和时间戳的格式。
JsonLogger
JsonLogger 是一个 StdLogger,它记录一个 json 对象而不是标准格式化的字符串。
json 对象如下所示
{ "severity": "error", "message": "Formatted message.", "context": { }, "time": "1977-04-22T06:00:00Z", "ts": 230533200 }
JsonFileLogger
JsonFileLogger 是一个 FileLogger,它记录一个 json 对象而不是标准格式化的字符串。每个日志条目都将是一个新对象,以下格式被使用
{ "severity": "error", "message": "Formatted message.", "context": { }, "time": "1977-04-22T06:00:00Z", "ts": 230533200 }
PDOLogger
将输出记录到所选的 PDO 连接。它期望添加数据的表应如下所示
level - varchar(255)
message - text
time - datetime
便捷方法
CREATE TABLE IF NOT EXISTS log_messages (`level` int, `message` TEXT, `time` TIME )
SysLogLogger
SysLogLogger 将您的日志发送到本地 syslog 服务器。
目前,它无法使用远程服务器,但将来应该可以...
它内部使用 php 的 syslog
和 openlog
方法。请查看构造函数文档以获取默认值。
MultiLogger
MultiLogger 被创建是为了能够在单个日志记录器中使用多个日志记录器。
这是由于您通常希望将日志记录器绑定到 LoggerInterface 以进行依赖注入,而这将不允许您绑定多个日志记录器!
您可以通过 addLogger(LoggerInterface $logger, string $name = null)
和 removeLogger(LoggerInterface|string $logger)
添加和删除日志记录器
CallbackLogger
CallbackLogger 实质上只是一个在记录日志时被调用的回调。回调传递以下参数
string: loglevel
string: message (message with context placeholders replaced)
string: text (message without context placeholders replaced)
array: context
CompactJsonLogger
使用 (Compact Log Event Format JSON)[https://clef-json.org/] 输出到流。
输出格式
{"@t":"DateTime as ISO8601 String","@l":"(int)level","@m":"Formatted message","@mt":"Message template","@r": {"context-key": "context-value"}}
CompactJsonFileLogger
使用 (Compact Log Event Format JSON)[https://clef-json.org/] 输出到文件。
{"@t":"DateTime as ISO8601 String","@l":"(int)level","@m":"Formatted message","@mt":"Message template","@r": {"context-key": "context-value"}}
许可证
MIT License
Copyright (c) 2020 Jitesoft
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.