composer require shureban/laravel-logplex
config/app.php:
Shureban\LaravelLogplex\LogplexServiceProvider::class,
php artisan vendor:publish --provider="Shureban\LaravelLogplex\LogplexServiceProvider"
config/logging.php:
'logplex' => [
'driver' => 'custom',
'via' => \Shureban\LaravelLogplex\LogplexLogger::class,
'level' => env('LOGPLEX_LEVEL', \Monolog\Level::Error),
],
.env to include the logplex channel in your stack:
LOG_STACK_CHANNELS=single,logplex
Or modify the stack channel directly in config/logging.php:
'stack' => [
'driver' => 'stack',
'channels' => ['single', 'logplex'],
],
Log an error with contextual data (e.g., user, request, trace):
Log::channel('logplex')->error('Failed to process payment', [
'user_id' => auth()->id(),
'request' => request()->all(),
'trace' => collect(debug_backtrace())->slice(1)->toArray(),
]);
This will generate a rich Slack-formatted message with:
logplex channel for structured logs.single (file) or slack for dual output:
'stack' => [
'driver' => 'stack',
'channels' => ['single', 'logplex', 'slack'],
],
error/critical to Logplex):
'logplex' => [
'driver' => 'custom',
'via' => \Shureban\LaravelLogplex\LogplexLogger::class,
'level' => env('LOGPLEX_LEVEL', \Monolog\Level::Error),
],
Attach metadata to logs using Laravel’s built-in context:
Log::channel('logplex')->info('User action', [
'user' => auth()->user(),
'metadata' => ['action' => 'profile_update'],
]);
The package automatically formats:
Override the default MessageBuilder to modify Slack blocks:
// app/Logging/Logplex/CustomMessageBuilder.php
namespace App\Logging\Logplex;
use Shureban\LaravelLogplex\Builder\MessageBuilderInterface;
use Shureban\LaravelLogplex\LogRecord;
class CustomMessageBuilder implements MessageBuilderInterface {
public function buildSlackMessage(LogRecord $logRecord, string $username, string $emoji) {
$message = new \Shureban\LaravelLogplex\Channels\Slack\Message($username, $emoji);
$message->addBlock(new \App\Logging\Logplex\CustomUserBlock($logRecord));
return $message;
}
}
Bind it in AppServiceProvider:
public function boot() {
$this->app->bind(
\Shureban\LaravelLogplex\Builder\MessageBuilderInterface::class,
\App\Logging\Logplex\CustomMessageBuilder::class
);
}
Wrap try/catch blocks for automatic exception logging:
try {
// Risky operation
} catch (\Exception $e) {
Log::channel('logplex')->error('Payment failed', [
'exception' => $e,
'context' => ['amount' => $request->amount],
]);
}
The package extracts:
Use middleware to log incoming requests:
// app/Http/Middleware/LogRequests.php
public function handle($request, Closure $next) {
Log::channel('logplex')->info('Incoming request', [
'method' => $request->method(),
'path' => $request->path(),
'headers' => $request->header(),
]);
return $next($request);
}
Channel Misconfiguration:
LOG_STACK_CHANNELS is misconfigured..env and config/logging.php:
LOG_STACK_CHANNELS=single,logplex
'stack' => ['channels' => ['single', 'logplex']],
Missing User Context:
auth()->user() returns null.Authenticate) runs before logging.Slack Formatting Errors:
$block->toArray(); // Check output before Slack sends it
Performance Overhead:
queue channel for async logging:
'logplex' => [
'driver' => 'queue',
'queue' => 'logs',
'via' => \Shureban\LaravelLogplex\LogplexLogger::class,
],
LOG_PLEX_ENDPOINT in .env is correct:
LOG_PLEX_ENDPOINT=https://your-logplex-endpoint.com
logplex channel in development:
LOG_LEVEL=debug
LOG_STACK_CHANNELS=logplex
single channel to debug formatting:
Log::channel('single')->debug('Raw log data', $logRecord->toArray());
Custom Blocks:
\Shureban\LaravelLogplex\Channels\Slack\Block to add new sections (e.g., DatabaseBlock).class DatabaseBlock implements Block {
public function toArray() {
return [
'type' => 'section',
'text' => [
'type' => 'mrkdwn',
'text' => '*Database Query*',
],
'fields' => [
['type' => 'mrkdwn', 'text' => '*Query:*\n```sql\nSELECT * FROM users```'],
],
];
}
}
Log Level Overrides:
Log::channel('logplex')->debug('Debug message', [], \Monolog\Level::Debug);
Webhook Integration:
MessageBuilderInterface:
public function buildWebhookMessage(LogRecord $logRecord) {
return [
'content' => $logRecord->message,
'embeds' => [/* custom embeds */],
];
}
Environment Variables:
LOGPLEX_LEVEL: Defaults to error; set to debug for verbose logs.LOGPLEX_USERNAME: Customize the bot username in Slack (default: LaravelLogplex).LOGPLEX_EMOJI: Change the emoji (e.g., :robot_face:).Logplex-Specific:
LOG_PLEX_TOKEN env var if authentication is required.Log Retention:
Alerting:
level=error AND user_id=123).Local Development:
How can I help you explore Laravel packages today?