beyounglabs / monolog-stackdriver
Monolog 的 Stackdriver 处理器(codeinternetapplications/monolog-stackdriver 衍生)。
Requires
- php: >=8.0
- google/cloud-logging: ^1.18
- monolog/monolog: ^2.0
This package is auto-updated.
Last update: 2024-09-12 01:37:09 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\Logger 通过 Google\Cloud\Logging\LoggingClient 设置 的文档,了解这些选项的详细信息。
这组选项将允许您设置默认的资源类型及其相关标签,这些标签适用于所有日志。请阅读 方法:monitoredResourceDescriptors.list 并执行 "尝试此 API",以获取每个资源的完整标签列表。
Google\Cloud\Logging\Entry 选项
请阅读 Google\Cloud\Logging\Entry 通过 Google\Cloud\Logging\Logger 设置 的文档,了解这些选项的详细信息。
默认情况下,您可以通过在上下文数组中添加这些包装在 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');
选择您需要的框架进行特定设置
- Laravel/Laravel v5.5
- Laravel/Laravel v5.6
- Laravel/Laravel v5.7
- Laravel/Lumen v5.5
- Laravel/Lumen v5.6
- Laravel/Lumen v5.7
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);