PHP version 8 or higher is required.
Composer version 1.10.1 or higher is required.
Explore documentation
Better Stack PHP logging
Start logging in 3 steps
Collect logs from any PHP code, including Laravel or Symfony.
1. Install
Install Better Stack PHP client library:
composer require logtail/monolog-logtail
composer require logtail/monolog-logtail:^2.0.0
2. Setup
Set up Monolog logger with Better Stack:
require "../vendor/autoload.php";
use Monolog\Logger;
use Logtail\Monolog\LogtailHandlerBuilder;
$logger = new Logger("example-app");
$handler = LogtailHandlerBuilder::withSourceToken("$SOURCE_TOKEN")
->withEndpoint("https://$INGESTING_HOST")
->build();
$logger->pushHandler($handler);
3. Start logging 🎉
Use Monolog logger as usual:
$logger->error("Something bad happened.");
$logger->info("Log message with structured logging.", [
"item" => "Orange Soda",
"price" => 100,
]);
You should see your logs in Better Stack → Live tail.
Need help?
Please let us know at hello@betterstack.com.
We're happy to help! 🙏
Additional information
New to logging?
Check out our Intro guide to Logging in PHP.
Using Laravel?
After installing logtail/monolog-logtail
package, you can add a custom logging channel to send logs to Better Stack in your config/logging.php
script, and provide a BETTER_STACK_SOURCE_TOKEN
and BETTER_STACK_SOURCE_ENDPOINT
environment variables.
<?php
use Monolog\Handler\NullHandler;
use Monolog\Handler\StreamHandler;
use Monolog\Handler\SyslogUdpHandler;
use Logtail\Monolog\LogtailHandler;
return [
// ...
'channels' => [
'stack' => [
'driver' => 'stack',
'channels' => ['single', 'betterstack'], // add 'betterstack'
'ignore_exceptions' => false,
],
// ...
'betterstack' => [
'driver' => 'monolog',
'level' => env('LOG_LEVEL', 'debug'),
'handler' => LogtailHandler::class,
'handler_with' => [
'sourceToken' => env('BETTER_STACK_SOURCE_TOKEN'),
'endpoint' => env('BETTER_STACK_SOURCE_ENDPOINT'),
],
],
],
];
You can now use Laravel's Log
facade, and your logs will be sent to Better Stack automatically.
use Illuminate\Support\Facades\Log;
Log::error("Something bad happened.");
Log::info("Log message with structured logging.", [
"item" => "Orange Soda",
"price" => 100,
]);
Using Symfony?
After installing logtail/monolog-logtail
package, you can set up logging to Better Stack by registering two services in your, providing a BETTER_STACK_SOURCE_TOKEN
and BETTER_STACK_SOURCE_ENDPOINT
environment variables, and adding the handler to your monolog.yaml
file.
services:
# ...
Logtail\Monolog\LogtailHandler:
autoconfigure: true
arguments:
$sourceToken: '%env(BETTER_STACK_SOURCE_TOKEN)%'
$endpoint: '%env(BETTER_STACK_SOURCE_ENDPOINT)%'
tags:
- { name: monolog.handler }
# optional processor to replace context variables in your logs
# e.g. replaces {route} by context.route
Monolog\Processor\PsrLogMessageProcessor:
autoconfigure: true
tags:
- { name: monolog.processor }
monolog:
handlers:
# ...
better_stack:
type: service
id: Logtail\Monolog\LogtailHandler
Logging from console scripts?
By default, PHP doesn't trigger shutdown handlers when the script is terminated via CTRL+C
.
To make sure your logs will get updated after interrupt, register the following signal handler:
pcntl_async_signals(true);
pcntl_signal(SIGINT, function() {
exit;
});
Example project
Want to try a runnable example?
See our PHP Logs example project on GitHub.