centagon/papertrail

将 Papertrail 日志监控集成到您的 PHP 应用程序中

9.0.2 2022-05-27 12:42 UTC

This package is auto-updated.

Last update: 2024-09-27 17:22:59 UTC


README

Build Status

轻松地将 Papertrail 日志监控 集成到您的 PHP 应用程序中。此包为纯 PHP 和 Laravel 提供集成。

安装

composer require centagon/papertrail

配置

不要在代码中包含凭据是一种好习惯,因此密码不会存储在版本控制中。将敏感信息保留在代码中可能在您需要开源或与其他开发者共享代码时成为问题。因此,此包默认将从环境变量 PAPERTRAIL_HOSTPAPERTRAIL_PORTPAPERTRAIL_HOSTNAME 中获取您的 Papertrail 日志服务器详细信息。

本包包含 2 个驱动程序

  • Php 用于纯 PHP 应用程序
  • Laravel 用于 4.2 到 5.6 版本的 Laravel 应用程序

纯 PHP

对于纯 PHP 应用程序,我们还建议安装 Monolog

composer require monolog/monolog

然后按照以下方式集成 papertrail 包

require __DIR__ . '/vendor/autoload.php';

use StephaneCoinon\Papertrail\Php as Papertrail;

// Boot using default settings (ie Papertrail log server and port
// from environment variables and no log message prefix).
// It also gives you a monolog instance in the process.
$logger = Papertrail::boot();

// Now your logs will appear in Papertrail dashboard.
$logger->info('test log');

Laravel 4

在您的 start/global.php 文件中添加以下行

// Pass all parameters
\StephaneCoinon\Papertrail\Laravel::boot($host, $port, $prefix);

或者

// Grab log server details from environment variables and use a prefix
\StephaneCoinon\Papertrail\Laravel::bootWithPrefix('MY_APP');

Laravel 5

编辑 app/Providers/AppServiceProvider.php 文件,并在 boot 方法中添加此行

\StephaneCoinon\Papertrail\Laravel::boot();

然后测试它是否正常工作

// routes/web.php

Route::get('log', function () {
    Log::info('test log', ['foo' => 'bar']);

    return 'Logged';
});

API 参考

所有驱动程序都提供以下接口

/**
 * Boot connector with given host, port and log message prefix.
 * 
 * If host or port are omitted, we'll try to get them from the environment
 * variables PAPERTRAIL_HOST and PAPERTRAIL_PORT.
 * 
 * @param  string $host   Papertrail log server, ie log.papertrailapp.com
 * @param  int $port      Papertrail port number for log server
 * @param  string $prefix Prefix to use for each log message
 * @return \Psr\Log\LoggerInterface
 */
public static function boot($host = null, $port = null, $prefix = '')
/**
 * Boot connector using credentials set in environment variables and the
 * given log message prefix.
 * 
 * @param string $prefix Prefix to use for each log message
 * @return \Psr\Log\LoggerInterface
 */
public static function bootWithPrefix($prefix)

测试

首先,将 .env.dist 复制为 .env,并在其中设置您的 Papertrail 主机、端口和 API 密钥。

然后运行 PHPUnit

./vendor/bin/phpunit