codeinternetapplications / monolog-stackdriver

Monolog 的 Stackdriver 处理器。

2.0.0 2019-11-28 12:22 UTC

This package is auto-updated.

Last update: 2024-09-19 15:04:07 UTC


README

此包允许您将您的 Monolog 日志条目推送到 Stackdriver,后者是 Google Cloud Platform 的一部分。

提供的 StackdriverHandler 会根据您的日志方法将给定的日志级别复制到 Stackdriver 的严重性。

它还尊重上下文参数,允许您在日志消息中发送额外的上下文数据。这将存储在日志消息的 jsonPayload.data 下。

配置

服务账户

在我们的示例中,我们假设您在项目中有一个服务账户 Google Developers Console JSON 密钥文件,并且您可以使用读取权限指向它。

如果您还没有这个文件,您可以通过 Google Cloud Platform - IAM & Admin - Service accounts 创建它。请确保您至少启用了 Logs writer 角色。

Google\Cloud\Logging\LoggingClient 选项

请参阅 Google\Cloud\Logging\LoggingClient 的文档以获取其他认证选项和进一步的特定连接和设置。

Google\Cloud\Logging\Logger 选项

请参阅 通过 Google\Cloud\Logging\LoggingClient 设置 Google\Cloud\Logging\Logger 的文档以获取有关这些选项的详细信息。

此组选项将允许您设置默认的资源类型及其相关标签,这些标签适用于所有日志。请阅读 方法:monitoredResourceDescriptors.list 并执行 "尝试此 API" 来获取特定资源的完整标签列表。

Google\Cloud\Logging\Entry 选项

请参阅 通过 Google\Cloud\Logging\Logger 设置 Google\Cloud\Logging\Entry 的文档以获取有关这些选项的详细信息。

默认情况下,您可以通过在上下文数组中添加这些以 stackdriver-key 包装来添加 Stackdriver 特定的日志条目选项。例如,添加日志条目特定标签非常有用。

$context['stackdriver'] = [
    // stackdriver related entry options
];

如果需要,您可以通过在调用 StackdriverHandler::__construct 时将 $entryOptionsWrapper 设置为您的自己的值(字符串)来覆盖此键名。

您还可以通过全局常量设置凭据路径和项目 ID。

define('GOOGLE_APPLICATION_CREDENTIALS', '/path/to/service-account-key-file.json'); 
define('GOOGLE_CLOUD_PROJECT', 'eg-my-project-id-148223');

选择您所需的框架进行特定设置

Vanilla 使用

use Monolog\Logger;
use CodeInternetApplications\MonologStackdriver\StackdriverHandler;

// ( ... )

// GCP Project ID
$projectId = 'eg-my-project-id-148223';

// See Google\Cloud\Logging\LoggingClient::__construct
$loggingClientOptions = [
    'keyFilePath' => '/path/to/service-account-key-file.json'
];

// init handler
$stackdriverHandler = new StackdriverHandler(
    $projectId,
    $loggingClientOptions
);

// init logger with StackdriverHandler
$logger = new Logger('stackdriver', [$stackdriverHandler]);

// basic info log with contextual data
$logger->info('New order', ['orderId' => 1001]);
// ( ... )

// add specific log entry options, eg labels
$context = ['orderId' => 1001];

// add a 'stackdriver' entry to the context to append
// log entry specific options
$context['stackdriver'] = [
    'labels' => [
        'action' => 'paid'
    ]
];
$logger->info('Order update', $context);
// ( ... )

// add specific log entry options, eg labels and operation
$context = ['orderId' => 1001];
$context['stackdriver'] = [
    'labels' => [
        'order' => 'draft'
    ],
    'operation' => [
        'id' => 'order-1001',
        'first' => true,
        'last' => false
    ]
];
$logger->info('Order update', $context);

// update both label and operation
$context['stackdriver'] = [
    'labels' => [
        'order' => 'paid'
    ],
    'operation' => [
        'id' => 'order-1001',
        'first' => false,
        'last' => false
    ]
];
$logger->info('Order update', $context);

// update both label and operation again
$context['stackdriver'] = [
    'labels' => [
        'order' => 'fulfilled'
    ],
    'operation' => [
        'id' => 'order-1001',
        'first' => false,
        'last' => true
    ]
];
$logger->info('Order update', $context);