noxdk / monolog-journal-handler
纯PHP编写的journald(systemd)Monolog处理器
1.0.3
2024-01-16 18:19 UTC
Requires
- php: >=8.1
- ext-sockets: *
- monolog/monolog: ^3.0
Requires (Dev)
- phpstan/phpstan: ^1.10
- phpstan/phpstan-deprecation-rules: ^1.1
- phpstan/phpstan-strict-rules: ^1.5
README
Monolog处理器为journald(systemd)。此包不依赖于systemd PHP扩展。日志直接写入journald套接字。它使用LineFormatter
进行消息格式化,但格式略有修改,以从消息中删除时间戳。格式化程序可以通过$handler->setFormatter();
进行更改。
功能
- 支持自定义标签(使用monolog上下文/额外字段)
- 支持多行日志
- 异常文件和行日志标签
安装
使用composer安装:composer require noxdk/monolog-journal-handler
配置参数
示例
简单的日志条目
<?php require 'vendor/autoload.php'; use Monolog\Logger; use Noxdk\MonologJournalHandler\JournalHandler; $log = new Logger('MyLogger', [new JournalHandler()]); $log->info('This is logged to the journal');
nov 20 10:08:14 host MyLogger[1]: INFO: This is logged to the journal
多行
$log->info("This is logged to the journal\nand contains multiple\nlines");
nov 20 10:08:14 host MyLogger[1]: INFO: This is logged to the journal
and contains multiple
lines
自定义标签(日志条目作为裁剪后的JSON输出)
$log->info('This is logged to the journal with custom tag', [ 'MY_TAG' => 'MyTag', 'ANOTHER_TAG' => "With\nnewlines" ]);
nov 20 10:08:14 host MyLogger[1]: INFO: This is logged to the journal with custom tag {"MY_TAG":"MyTag","ANOTHER_TAG":"With
newlines"}
{ ... "SYSLOG_IDENTIFIER": "MyLogger", "_HOSTNAME": "host", "_UID": "1000", "MY_TAG": "MyTag", "ANOTHER_TAG": "With\nnewlines", "MESSAGE": "INFO: This is logged to the journal with custom tag {\"MY_TAG\":\"MyTag\",\"ANOTHER_TAG\":\"With\nnewlines\"} ", "PRIORITY": "6", "_CMDLINE": "php test.php", ... }
异常日志
$log->error('Some error occurred', [new Exception('Bad something')]);
nov 20 10:08:14 host MyLogger[1]: ERROR: Some error occurred ["[object] (Exception(code: 0): Bad something at /test.php:18)
[stacktrace]
#0 /test.php(13): TestClass->update()
#1 /test.php(22): TestClass->__construct()
#2 {main}
"]