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

Log To Json Laravel Package

behnamhosseini/log-to-json

Convert Laravel log output to JSON. Choose between two logging modes via a simple config setting, and publish the package config to customize behavior. Install via Composer and integrate with your existing Laravel logging setup.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Use Case Alignment: The package converts Laravel’s default log format (monolog) into structured JSON logs, which is valuable for:
    • Observability: Enables easier parsing, querying, and visualization in tools like ELK, Datadog, or Splunk.
    • Compliance/Forensics: Structured logs simplify audit trails and regulatory reporting.
    • Microservices: JSON logs standardize log formats across polyglot architectures.
  • Non-Intrusive: Operates as a log processor (via Monolog handlers) without modifying core Laravel logic, reducing architectural risk.
  • Limitation: Only handles Laravel’s default Monolog setup; custom log handlers (e.g., syslog, database) may require additional configuration.

Integration Feasibility

  • Low Coupling: Integrates via Monolog’s Formatter interface, requiring minimal changes to existing logging pipelines.
  • Backward Compatibility: Can coexist with existing log handlers (e.g., single vs. multiple channels).
  • Testing Overhead: Requires validation of log structure in downstream systems (e.g., log shippers, dashboards).

Technical Risk

Risk Mitigation
Log Volume Spikes Test performance under high-throughput scenarios; consider async processing.
Schema Drift Document JSON schema explicitly to avoid breaking consumers.
Tooling Incompatibility Validate compatibility with log aggregators (e.g., Fluentd, Logstash).
Deprecation Risk Monitor Laravel/Monolog updates for breaking changes (last release: 2021).

Key Questions

  1. Consumer Requirements:
    • What tools/dashboards will ingest these logs? Do they require specific JSON fields (e.g., context, extra)?
  2. Performance:
    • What is the expected log volume? Will JSON serialization add latency?
  3. Customization:
    • Are there existing log handlers (e.g., SingleLineFormatter, LineFormatter) that need preservation?
  4. Rollout Strategy:
    • Should JSON logs replace or supplement existing formats during migration?

Integration Approach

Stack Fit

  • Best For:
    • Laravel applications using Monolog (default in Laravel 5.5+).
    • Systems relying on structured logging (e.g., ELK, Grafana Loki, AWS CloudWatch).
  • Less Ideal For:
    • Applications with custom log handlers (e.g., database logs, third-party integrations).
    • Environments where log size is a constraint (JSON adds ~20–50% overhead vs. plaintext).

Migration Path

  1. Assessment Phase:
    • Audit current log handlers (config/logging.php) and identify target channels for JSON conversion.
    • Sample existing logs to define required JSON fields (e.g., level, message, context, timestamp).
  2. Implementation:
    • Option A (Replacement): Replace SingleLineFormatter with LogToJson\JsonFormatter in target channels.
      'channels' => [
          'single' => [
              'driver' => 'single',
              'path' => storage_path('logs/laravel.json'),
              'level' => 'debug',
              'formatter' => \LogToJson\JsonFormatter::class, // Add this line
          ],
      ],
      
    • Option B (Dual-Write): Add JSON handler alongside existing handlers temporarily for validation.
  3. Validation:
    • Test log output with tools like jq or log aggregators.
    • Verify no critical data is lost (e.g., stack traces, request payloads).

Compatibility

  • Laravel Versions: Tested on Laravel 5.5–8.x (Monolog v1/v2). May need adjustments for Laravel 9+ (PHP 8.0+).
  • Monolog Handlers: Works with stream, syslog, and database handlers if they support formatters.
  • Downstream Tools:
    • Ensure log shippers (e.g., Fluent Bit, Filebeat) support JSON input.
    • Configure parsers in ELK/Grafana to handle the schema.

Sequencing

  1. Phase 1: Pilot in non-production (e.g., staging) with a subset of log channels.
  2. Phase 2: Gradually roll out to production, monitoring for:
    • Log ingestion failures.
    • Performance degradation.
  3. Phase 3: Deprecate old log formats post-validation.

Operational Impact

Maintenance

  • Proactive Tasks:
    • Schema Management: Document and version JSON fields to avoid breaking consumers.
    • Dependency Updates: Monitor for Laravel/Monolog updates that may affect the package.
  • Reactive Tasks:
    • Debugging log parsing errors in downstream systems (e.g., malformed JSON).
    • Handling edge cases (e.g., circular references in context data).

Support

  • Troubleshooting:
    • Logs may appear corrupted if:
      • The formatter fails silently (add error handling).
      • Custom data in context contains non-serializable objects (e.g., closures).
    • Debugging Tip: Temporarily revert to SingleLineFormatter to isolate issues.
  • Support Matrix:
    Issue Type Owner Resolution Path
    JSON Parsing Errors DevOps/Observability Validate schema, update log shippers.
    Performance Bottlenecks Backend Team Optimize serialization or use async logging.
    Laravel Upgrade Issues TPM/Dev Test compatibility; patch if needed.

Scaling

  • Horizontal Scaling:
    • JSON logs are larger than plaintext; ensure log storage/shipping systems (e.g., S3, Kafka) can handle increased volume.
    • Consider compression (e.g., Gzip) for log files.
  • Vertical Scaling:
    • Minimal impact if serialization overhead is acceptable (<5% CPU increase).
    • Monitor monolog process memory usage during spikes.

Failure Modes

Failure Scenario Impact Mitigation
Log Shipper Rejects Malformed JSON Data loss, alert fatigue Implement pre-ship validation.
JSON Serialization Fails Logs written in fallback format Add retry logic for transient failures.
Schema Changes Break Consumers Dashboard/API failures Use backward-compatible field additions.
High Log Volume Overloads Storage Storage costs, slow queries Archive cold logs; implement log retention.

Ramp-Up

  • Onboarding New Team Members:
    • Documentation: Add a LOGGING.md with:
      • JSON schema examples.
      • Troubleshooting steps for common issues.
    • Training: Demo log visualization in tools like Kibana to showcase value.
  • Knowledge Transfer:
    • Highlight differences between old/new log formats (e.g., message vs. structured_data).
    • Share runbooks for rollback scenarios (e.g., re-enabling plaintext logs).
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.
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours