Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Monolog Handler Laravel Package

app-insights-php/monolog-handler

Monolog handlers for Microsoft Application Insights: send traces and dependency telemetry via AppInsightsTraceHandler and AppInsightsDependencyHandler. Supports buffering for long-running workers and enforces the 64KB telemetry size limit. Includes Laravel setup example.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Observability Alignment: The package bridges Monolog (PHP’s logging abstraction) with Azure Application Insights, enabling structured telemetry (traces, exceptions, dependencies) for distributed PHP applications. This aligns well with modern observability stacks but requires explicit instrumentation.
  • Layer Fit: Best suited for application-layer logging (not infrastructure-level metrics). Works alongside existing Monolog handlers (e.g., StreamHandler, SyslogHandler) but adds telemetry-specific features (e.g., custom dimensions, sampling).
  • Architecture Constraints:
    • Monolog Dependency: Requires Monolog v2+ (common in Laravel but may need version checks).
    • Azure Dependency: Tight coupling to Azure App Insights SDK (PHP extension or HTTP-based). May introduce latency if using HTTP ingestion.
    • Laravel-Specific Gaps: No native integration with Laravel’s Log facade or LogServiceProvider; requires manual handler registration.

Integration Feasibility

  • Laravel Compatibility:
    • High: Laravel’s Log facade uses Monolog under the hood. Integration is feasible via:
      • Registering the handler in AppServiceProvider or a dedicated logging config.
      • Leveraging Laravel’s Monolog configuration (e.g., config/logging.php).
    • Challenge: Laravel’s default log channels (e.g., single, stack) may need extension to include the App Insights handler.
  • Data Flow:
    • Logs → Monolog → AppInsightsHandler → Azure App Insights.
    • Customization: Supports log levels, custom properties, and sampling (via App Insights SDK).
  • Testing:
    • Unit Testing: Mock AppInsightsHandler to verify log routing.
    • E2E Testing: Validate telemetry in Azure Portal (e.g., trace IDs, custom dimensions).

Technical Risk

Risk Area Severity Mitigation Strategy
Azure SDK Dependency High Use PHP extension (lower latency) or HTTP fallback. Monitor SDK updates.
Cold Start Latency Medium Configure handler with retry logic for transient failures.
Log Volume Impact Medium Implement sampling in App Insights SDK or filter logs before ingestion.
Schema Evolution Low Azure App Insights handles backward-compatible schema changes.
Laravel Version Lock Low Test with Laravel 8/9/10; Monolog v2+ is stable.

Key Questions

  1. Telemetry Requirements:
    • Are traces, exceptions, and dependencies the primary need, or are metrics (e.g., custom events) also required?
    • Does the team need distributed tracing (e.g., correlating PHP logs with other services)?
  2. Cost vs. Value:
    • What is the expected log volume? Azure App Insights pricing is tiered (free tier has limits).
    • Are there existing observability tools (e.g., Datadog, New Relic) that could unify telemetry?
  3. Compliance/Security:
    • Are logs PII-sensitive? If so, ensure masking or exclusion in App Insights.
    • Is data residency a concern (Azure’s global endpoints)?
  4. Operational Overhead:
    • Who will manage the Azure App Insights resource (instrumentation key rotation, access control)?
    • Are there SLAs for log delivery reliability?
  5. Alternatives:
    • Could OpenTelemetry (via open-telemetry/php) provide a more vendor-agnostic solution?
    • Is there a need for log enrichment (e.g., adding request context) before sending to App Insights?

Integration Approach

Stack Fit

  • PHP/Laravel Stack:
    • Core Fit: Works natively with Laravel’s Monolog integration.
    • Extensions:
      • Laravel Debugbar: Can correlate logs with HTTP requests.
      • Telescope: For local log inspection (but App Insights is remote).
  • Azure Stack:
    • App Insights: Primary destination for traces/exceptions.
    • Alternatives: Could forward logs to other sinks (e.g., Elasticsearch) via App Insights’ continuous export.
  • Non-Fit:
    • Real-time Processing: Not suitable for streaming log analysis (e.g., Kafka, Fluentd).
    • Metrics-Centric: Lacks native support for custom metrics (use App Insights SDK directly for those).

Migration Path

  1. Assessment Phase:
    • Audit current logging (e.g., Log::error(), Log::debug() usage).
    • Identify critical log types (exceptions, business events, dependencies).
  2. Pilot Integration:
    • Add AppInsightsHandler to a non-production Laravel environment.
    • Test with a subset of logs (e.g., only ERROR level).
  3. Full Rollout:
    • Step 1: Replace default log channel with a stack channel including AppInsightsHandler.
      'channels' => [
          'app_insights' => [
              'driver' => 'monolog',
              'handler' => AppInsightsHandler::class,
              'level' => env('APP_INSIGHTS_LOG_LEVEL', 'error'),
              'with' => [
                  'instrumentation_key' => env('APP_INSIGHTS_INSTRUMENTATION_KEY'),
              ],
          ],
          'stack' => [
              'driver' => 'stack',
              'channels' => ['single', 'app_insights'], // Keep local logs + App Insights
          ],
      ],
      
    • Step 2: Update log statements to include context (e.g., Log::error('Failed', ['user_id' => $user->id])).
    • Step 3: Validate telemetry in Azure Portal (check for missing traces or dropped logs).
  4. Optimization:
    • Configure sampling in App Insights SDK to reduce cost.
    • Set up alerts for critical log patterns (e.g., 5xx errors).

Compatibility

  • Laravel Versions:
    • Tested with Laravel 8+ (Monolog v2+). Laravel 5.5–7.x may need Monolog updates.
  • PHP Versions:
    • Requires PHP 7.4+ (App Insights PHP SDK dependency).
  • Handler Conflicts:
    • Ensure no duplicate log processing (e.g., avoid adding AppInsightsHandler to multiple channels).
  • Environment Variables:
    • Use Laravel’s .env for APP_INSIGHTS_INSTRUMENTATION_KEY and log level.

Sequencing

  1. Pre-requisites:
    • Azure App Insights resource created with instrumentation key.
    • PHP extension for App Insights SDK installed (pecl install azure).
  2. Development:
    • Install package: composer require app-insights-php/monolog-handler.
    • Configure handler in AppServiceProvider or config/logging.php.
  3. Testing:
    • Local testing with APP_INSIGHTS_LOG_LEVEL=debug (but avoid high-volume debug logs in production).
    • CI/CD pipeline: Add a step to validate logs appear in App Insights (e.g., using Azure CLI).
  4. Deployment:
    • Roll out to staging first; monitor for:
      • Increased latency (if using HTTP ingestion).
      • Log volume spikes.
    • Gradually enable for all log levels.

Operational Impact

Maintenance

  • Handler Configuration:
    • Pros: MIT license; minimal moving parts (just Monolog + App Insights SDK).
    • Cons: Archived repo may lack updates for new Monolog/App Insights features.
    • Mitigation: Fork the repo if critical updates are needed (e.g., PHP 8.2 support).
  • Dependency Updates:
    • Monitor app-insights-php/monolog-handler and azure/azure-sdk-for-php for breaking changes.
    • Pin versions in composer.json to avoid surprises.
  • Logging Schema:
    • App Insights may deprecate fields over time; validate telemetry schema periodically.

Support

  • Troubleshooting:
    • Common Issues:
      • Logs not appearing: Check instrumentation key, network connectivity, and App Insights status.
      • High latency: Use PHP extension instead of HTTP ingestion.
    • Debugging Tools:
      • Azure Portal’s "Live Metrics" for real-time log inspection.
      • Monolog’s ErrorHandler to catch handler exceptions.
  • Vendor Lock-in:
    • Risk: App Insights-specific features (e.g., custom dimensions) may not port easily to other tools.
    • Mitigation: Use OpenTelemetry for multi-vendor support if needed.

Scaling

  • Log Volume:
    • Limitations: App Insights free tier limits to ~10K daily traces. High-volume apps may hit costs quickly.
    • Scaling Options:
      • Implement log sampling (e.g., only ERROR/WARN levels).
      • Use App Insights’ continuous export to store raw logs in Azure Blob/Table Storage.
  • Performance:
    • Latency: HTTP ingestion adds
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
emuniq/filament-browser-notifications
syriable/filament-translator
hungnm28/livewire-form
wenprise/eloquent
crudly/encrypted
fadion/bouncy
cuci/prototurk-sdk
gos/pubsub-router-bundle
cuci/prototurk-sdk-symfony
clementtalleu/easyadmin-markdown-bundle
codeflextech/permission-manager
karnoweb/livewire-datepicker
sayedenam/sayed-dashboard
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui