Prerequisites
- Create an Axiom account.
- Create a dataset in Axiom where you send your data.
- Create an API token in Axiom with permissions to update the dataset you have created.
- PHP development environment
- Composer installed on your system
- Laravel app setup
Installation
Create a Laravel project
Create a new Laravel project:Exploring the logging config file
In your Laravel project, theconfig
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 log channel
Thedefault
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.
Replace
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 send your 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 log channel
Thedeprecations
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.
Configuration log channel
The heart of thelogging.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: Designed for simplicity, the
single
channel writes logs to a single file. It’s a straightforward solution for tracking logs without needing complex log management strategies. - Axiom: The custom
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. - Daily: This channel rotates logs daily, keeping your log files manageable and making it easier to navigate log entries over time.
LOG_LEVEL
environment variable sets this, defaulting to debug
for capturing detailed log information.
Getting started with log levels in Laravel
Laravel lets you choose from eight different levels of importance for your log messages, just like a list of warnings from very serious to just for info. Here’s what each level means, starting with the most severe:- EMERGENCY: Your app is broken and needs immediate attention.
- ALERT: similar to
EMERGENCY
, but less severe. - CRITICAL: Critical errors within the main parts of your app.
- ERROR: error conditions in your app.
- WARNING: something unusual happened that may need to be addressed later.
- NOTICE: Important info, but not a warning or error.
- INFO: General updates about what your app is doing.
- DEBUG: used to record some debugging messages.
Creating the custom logger class
In this section, we will explain how to create the custom logger class designed for sending your Laravel app’s logs to Axiom. This class namedAxiomHandler
, extends Monolog’s AbstractProcessingHandler
giving us a structured way to handle log messages and forward them to Axiom.
- Initializing cURL: The
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 toapplication/json
. - Handling errors: If there’s an error during the cURL request, it’s logged to PHP’s error log. This helps in diagnosing issues with log forwarding without disrupting your app’s normal operations.
- Formatting logs: Lastly, we specify the log message format using the
getDefaultFormatter
method. By default, we use Monolog’sJsonFormatter
to ensure our log messages are JSON encoded, making them easy to parse and analyze in Axiom.
Creating the test controller
In this section, we will demonstrate the process of verifying that your custom Axiom logger is properly set up and functioning within your Laravel app. To do this, we’ll create a simple test controller with a method designed to send a log message using the Axiom channel. Following this, we’ll define a route that triggers this logging action, allowing you to easily test the logger by accessing a specific URL in your browser or using a tool like cURL. Create a new controller calledTestController
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.
Registering the route
Next, you need to make this test accessible via a web route. Open yourroutes/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.
Run the app
If you are running the Laravel app locally, to see your custom Axiom logger in action, you’ll need to start your Laravel app. Open your terminal or command prompt, navigate to the root directory of your Laravel project, and run the following command:http://localhost:8000/test-log
, but the command output will specify the exact address.
View the logs in Axiom
Once you’ve set up your Laravel app with Axiom logging and sent test logs via ourTestController
, 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