This guide demonstrates how to configure logging in a Laravel app to send logs to Axiom
config
directory contains several configurations on how different parts of your app work, such as how it connects to the database, manages sessions, and handles caching. Among these files, logging.php
identifies how you can define your app logs activities and errors. This file is designed to let you specify where your logs go: a file, a cloud service, or other destinations. The configuration file below includes the Axiom logging setup.
logging.php
file in your Laravel project, you’ll find some Monolog handlers like NullHandler
, StreamHandler
, and a few more. This shows that Laravel uses Monolog to help with logging, which means it can do a lot of different things with logs.
default
configuration specifies the primary channel Laravel uses for logging. In our setup, this is set through the .env
file with the LOG_CHANNEL
variable, which you’ve set to axiom
. This means that, by default, log messages will be sent to the Axiom channel, using the custom handler you’ve defined to send logs to the dataset.
API_TOKEN
with the Axiom API token you have generated. For added security, store the API token in an environment variable.Replace DATASET_NAME
with the name of the Axiom dataset where you want to send data.Replace AXIOM_DOMAIN
with api.axiom.co
if your organization uses the US region, and with api.eu.axiom.co
if your organization uses the EU region. For more information, see Regions.deprecations
channel is configured to handle logs about deprecated features in PHP and libraries, helping you prepare for updates. By default, it’s set to ignore these warnings, but you can adjust this to direct deprecation logs to a specific channel if needed.
logging.php
file lies within the channels
array where you define all available logging channels. The configuration highlights channels like single
, axiom
, and daily
, each serving different logging purposes:
single
channel writes logs to a single file. It’s a straightforward solution for tracking logs without needing complex log management strategies.axiom
channel sends logs to your specified Axiom dataset, providing advanced log management capabilities. This integration enables powerful log analysis and monitoring, supporting better insights into your app’s performance and issues.LOG_LEVEL
environment variable sets this, defaulting to debug
for capturing detailed log information.
EMERGENCY
, but less severe.AxiomHandler
, extends Monolog’s AbstractProcessingHandler
giving us a structured way to handle log messages and forward them to Axiom.
initializeCurl
method sets up a cURL handle to communicate with Axiom’s API. It prepares the request with the appropriate headers, including the authorization header that uses your Axiom API token and content type set to application/json
.getDefaultFormatter
method. By default, we use Monolog’s JsonFormatter
to ensure our log messages are JSON encoded, making them easy to parse and analyze in Axiom.TestController
within your app/Http/Controllers
directory. In this controller, add a method named logTest
. This method will use Laravel’s logging to send a test log message to your Axiom dataset. Here’s how you set it up:
axiom
channel, which we previously configured to forward logs to your Axiom account. The message Testing Axiom logger! should then appear in your Axiom dataset, confirming that the logger is working as expected.
routes/web.php
file and add a new route that points to the logTest
method in your TestController
. This enables you to trigger the log message by visiting a specific URL in your web browser.
/test-log
on your Laravel app’s domain will execute the logTest
method, send a log message to Axiom, and display ‘Log sent to Axiom’ as a confirmation in the browser.
http://localhost:8000/test-log
, but the command output will specify the exact address.
TestController
, check your dataset. There, you’ll find your logs categorized by levels like debug
, info
, error
, and warning
. This confirms everything is working and showcases Axiom’s capabilities in handling log data.
View Laravel logs in Axiom