michabbb / logentries
适用于 LogEntries 服务(https://logentries.com/)的 PHP PSR-3 兼容日志类
Requires
- php: >=5.3
- psr/log: 1.0.0
Requires (Dev)
- phpunit/phpunit: 4.0.*
README
Chris Schuld(http://chrisschuld.com/)编写的 LogEntries 特定日志类,用于将信息记录到 LogEntries(https://logentries.com)SaaS 应用程序
关于
LogEntries 是一个易于使用的 PHP PSR-3 兼容日志类,用于将信息记录到 LogEntries SaaS 应用程序。
安装
Composer
从命令行
composer require cbschuld/LogEntries:1.*
在你的 composer.json
{ "require": { "cbschuld/LogEntries": "1.*" } }
基本用法
<?php use cbschuld\LogEntries; require "vendor/autoload.php"; $token = "2bfbea1e-10c3-4419-bdad-7e6435882e1f"; // your LogEntries token (sample from docs) $log = LogEntries::getLogger($token,true,true); // create persistent SSL-based connection $log->info("some information"); $log->notice(json_encode(["status"=>"ok","message"=>"send some json"]));
高级用法
你可以向所有日志函数发送一个字符串(PSR-3)、编码的 JSON(PSR-3)或一个数组,该数组将被编码成 JSON(不是 PSR-3 但可用)
<?php use cbschuld\LogEntries; require "vendor/autoload.php"; $token = "2bfbea1e-10c3-4419-bdad-7e6435882e1f"; // your LogEntries token (sample from docs) $jsonInfo = ["json"=>true,"example"=>"yes","works"=>true]; $log = LogEntries::getLogger($token,true,true); // create persistent SSL-based connection $log->info(["status"=>"ok","example"=>"with json messages"]); $log->notice($jsonInfo);
你也可以创建一个非静态实例,用于依赖注入或多个日志令牌的使用
<?php use cbschuld\LogEntries; require "vendor/autoload.php"; $token = "2bfbea1e-10c3-4419-bdad-7e6435882e1f"; // your LogEntries token (sample from docs) $jsonInfo = ["json"=>true,"example"=>"yes","works"=>true]; $log = new LogEntries($token,true,true); // create persistent SSL-based connection $log->info(["status"=>"ok","example"=>"with json messages"]); $log->notice($jsonInfo); $token2 = "2bfbea1e-10c3-4419-bdad-7e6435882e1f"; // your LogEntries token (sample from docs) $log2 = new LogEntries($token2,true,true); // create persistent SSL-based connection $log2->info(["status"=>"ok","example"=>"with json messages","from"=>"log2"]); $log2->notice($jsonInfo);
中间件(写入器)
使用 LogEntriesWriter,你可以将日志写入中间件附加到日志中。在日志写入 LogEntries 之前会调用中间件。它允许检测 JSON 或文本。这使得你可以将来自你的架构的数据附加到你的日志中。
以下是一个示例用法
<?php use cbschuld\LogEntries; use cbschuld\LogEntriesWriter; class WriterTest extends LogEntriesWriter { // always add the hostname public function log($message,$isJson=false) { if($isJson) { $json = json_decode($message,true); $json["hostname"] = gethostname(); $message = json_encode($json); } else { $hostname = gethostname(); $message .= " hostname={$hostname}"; } return $message; } } $writer = new WriterTest(); $json = array( "datetime" => new \DateTime("now"), "status" => "ok", ); $log = new LogEntries("MYTOKEN",true,true); $log->addWriter($writer); $log->info($json);
PSR-3 兼容
LogEntries 是 PHP PSR-3 兼容的。这意味着它实现了 Psr\Log\LoggerInterface
。
许可证
MIT 许可证
版权所有 (c) 2015 Chris Schuld chris@chrisschuld.com
以下条件,任何人获得本软件及其相关文档文件(“软件”)的副本,均可免费使用该软件,不受任何限制,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或销售软件副本的权利,并允许向获得软件的人提供该软件副本,以供其使用,具体条件如下:
上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。
软件按“原样”提供,不提供任何形式的保证,无论是明示的还是暗示的,包括但不限于适销性、特定用途适用性和非侵权性。在任何情况下,作者或版权所有者均不对任何索赔、损害或其他责任负责,无论该索赔、损害或其他责任是由于合同、侵权或其他方式引起的,无论是否与软件或软件的使用或其他方式有关。