shadowbane/laravel-datadog-logger
Composer 安装命令:
composer require shadowbane/laravel-datadog-logger
包简介
Custom laravel monolog logger for datadog logs management, both api and agent ways
关键字:
README 文档
README
A custom Laravel Monolog logger for sending logs to DataDog via their HTTP API.
Features
- Send Laravel logs directly to DataDog via HTTP API
- Automatic exception tracking with stack traces
- Custom log context support
- Configurable log levels and endpoints
- Error reporting for failed API requests
- Environment-based tagging
Requirements
- PHP ^8.1 || ^8.2 || ^8.3 || ^8.4
- Laravel 10.x or higher
- ext-json
- ext-curl
- Guzzle ^6.0 || ^7.0
- Monolog ^3.0
Installation
Install the package via Composer:
composer require shadowbane/laravel-datadog-logger
The service provider will be automatically registered.
Configuration
Environment Variables
Add the following to your .env file:
DATADOG_API_KEY=your-datadog-api-key DATADOG_LEVEL=warning DATADOG_BUBBLE=true DATADOG_ENVIRONMENT=production DATADOG_API_ENDPOINT=https://http-intake.logs.datadoghq.com/api/v2/logs DATADOG_ERROR_LOG_CHANNEL=stack
Configuration Options
| Variable | Description | Default |
|---|---|---|
DATADOG_API_KEY |
Your DataDog API key (required) | - |
DATADOG_LEVEL |
Minimum log level (debug, info, notice, warning, error, critical, alert, emergency) | warning |
DATADOG_BUBBLE |
Whether logs should bubble to other handlers | true |
DATADOG_ENVIRONMENT |
Environment tag for DataDog | Laravel's app()->environment() |
DATADOG_API_ENDPOINT |
DataDog logs API endpoint | https://http-intake.logs.datadoghq.com/api/v2/logs |
DATADOG_ERROR_LOG_CHANNEL |
Laravel log channel for logging DataDog API failures | false |
Laravel Logging Configuration
The package automatically merges the datadog-api channel into your logging configuration. You can use it in your config/logging.php:
'channels' => [ 'stack' => [ 'driver' => 'stack', 'channels' => ['single', 'datadog-api'], ], ]
Usage
Basic Logging
Use the datadog-api channel to send logs to DataDog:
use Illuminate\Support\Facades\Log; Log::channel('datadog-api')->info('User logged in', [ 'user_id' => 123, 'ip_address' => '192.168.1.1', ]); Log::channel('datadog-api')->error('Payment failed', [ 'order_id' => 456, 'amount' => 99.99, ]);
Exception Logging
When logging exceptions, pass the exception in the context array with the key exception:
try { // Your code } catch (\Exception $e) { Log::channel('datadog-api')->error('Failed to create company: ' . $e->getMessage(), [ 'exception' => $e, 'company_id' => 123, 'user_id' => 456, ]); }
The logger will automatically extract:
- Exception class name (becomes the message)
- Error code (
error.kind) - Error message (
error.message) - Stack trace (
error.stack)
Log Context
All context data (except exception, message, and messages) is sent to DataDog as custom attributes:
Log::channel('datadog-api')->warning('High memory usage', [ 'memory_used' => '512MB', 'memory_limit' => '256MB', 'server' => 'web-01', ]);
This will create a log in DataDog with custom attributes: memory_used, memory_limit, and server.
Testing
Test your DataDog integration using the included Artisan command:
php artisan datadog:send-test-exception
This command sends a test exception to DataDog with sample context data.
Log Level Mapping
Laravel log levels are mapped to DataDog status levels:
| Laravel Level | DataDog Status |
|---|---|
| debug, info | info |
| notice, warning | warn |
| error, critical, alert, emergency | error |
Error Handling
If the DataDog API request fails, you can configure a fallback Laravel log channel to capture these errors:
DATADOG_ERROR_LOG_CHANNEL=stack
Failed API requests will be logged to this channel with the Guzzle exception details.
DataDog Log Structure
Logs sent to DataDog include the following fields:
ddsource: Always set tolaravelddtags: Environment tag (e.g.,env:production)hostname: Server hostnamemessage: Formatted log message (or exception class name)service: Your Laravel application name (fromconfig('app.name'))status: Mapped log level (info, warn, error)timestamp: Log timestamp in milliseconds- Custom attributes from log context
For exceptions:
error.kind: Exception codeerror.message: Exception messageerror.stack: Exception stack trace
Development
Static Analysis
Run PHPStan for static analysis:
vendor/bin/phpstan analyze
License
This package is open-sourced software licensed under the MIT license.
Credits
Support
If you discover any issues, please open an issue on the GitHub repository.
shadowbane/laravel-datadog-logger 适用场景与选型建议
shadowbane/laravel-datadog-logger 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6.59k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2022 年 07 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「log」 「php」 「api」 「monolog」 「logger」 「formatter」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 shadowbane/laravel-datadog-logger 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 shadowbane/laravel-datadog-logger 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 shadowbane/laravel-datadog-logger 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A PSR-7 compatible library for making CRUD API endpoints
Stackdriver handler for Monolog (codeinternetapplications/monolog-stackdriver Fork).
Laravel 5.x.x library for integration Monolog Sentry
Pacote simplificado para persistência de logs
Collection of tools to use the full power of the Enyalius framework
Audit changes of your Eloquent models in Laravel/Lumen
统计信息
- 总下载量: 6.59k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 7
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-07-19