cbschuld/logentries

一个符合PSR-3规范的LogEntries服务(https://logentries.com/)日志类

1.0.4 2017-05-22 18:44 UTC

This package is auto-updated.

Last update: 2024-08-25 01:36:23 UTC


README

Build Status

一个针对LogEntries的特定日志类,由Chris Schuld编写,用于将日志信息记录到LogEntries

关于

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

特此授予任何获得本软件及其相关文档文件(以下简称“软件”)副本的任何人免费使用该软件的权利,不受任何限制,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或销售软件副本的权利,以及允许向提供软件的个人授予上述权利,前提是遵守以下条件

上述版权声明和本许可声明应包含在软件的任何副本或主要部分中。

软件按“原样”提供,不提供任何形式的保证,无论是明示的还是暗示的,包括但不限于对适销性、特定用途适用性和非侵权的保证。在任何情况下,作者或版权所有者不应对任何索赔、损害或其他责任负责,无论是基于合同、侵权或其他方式,无论源于、源于或与软件或其使用或以其他方式相关。