PHP code example of viu / viu-laravel
1. Go to this page and download the library: Download viu/viu-laravel library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
viu / viu-laravel example snippets
'channels' => [
// Canal dedicado
'viu' => [
'driver' => 'viu',
],
// Stack combinando logs locais + Viu
'stack' => [
'driver' => 'stack',
'channels' => ['daily', 'viu'],
],
],
use Illuminate\Support\Facades\Log;
Log::channel('viu')->info('User logged in', ['user_id' => 123]);
Log::channel('viu')->error('Payment failed', ['amount' => 99.90]);
// Ou via stack (envia para 'daily' e 'viu' simultaneamente)
Log::stack(['daily', 'viu'])->warning('Rate limit approaching');
'viu' => [
'driver' => 'viu',
'level' => 'warning', // ignora debug e info neste canal
],
use Viu\ViuLaravel\Facades\Viu;
Viu::info('User logged in', ['user_id' => 123]);
Viu::warning('Rate limit approaching', ['remaining' => 10]);
Viu::error('Payment failed', ['amount' => 99.90, 'currency' => 'BRL']);
Viu::critical('Database unreachable', ['host' => 'db.prod']);
// Com exception
try {
processPayment($order);
} catch (\Throwable $e) {
Viu::error('Payment processing failed', [
'exception' => $e,
'order_id' => $order->id,
'amount' => $order->total,
]);
}
use Viu\ViuLaravel\ViuLogger;
class OrderService
{
public function __construct(
private readonly ViuLogger $logger,
) {}
public function processOrder(Order $order): void
{
$this->logger->info('Processing order', ['order_id' => $order->id]);
try {
// ...
} catch (\Throwable $e) {
$this->logger->error('Order failed', [
'exception' => $e,
'order_id' => $order->id,
]);
}
}
}
protected $middlewareGroups = [
'api' => [
\Viu\ViuLaravel\Middleware\ViuCorrelationMiddleware::class,
// ...
],
'web' => [
\Viu\ViuLaravel\Middleware\ViuCorrelationMiddleware::class,
// ...
],
];
use Viu\ViuLaravel\Middleware\ViuCorrelationMiddleware;
->withMiddleware(function (Middleware $middleware) {
$middleware->appendToGroup('api', ViuCorrelationMiddleware::class);
$middleware->appendToGroup('web', ViuCorrelationMiddleware::class);
})
// config/viu.php
return [
'service_name' => env('VIU_SERVICE_NAME', env('APP_NAME', 'laravel-app')),
'environment' => env('VIU_ENVIRONMENT', env('APP_ENV', 'production')),
'transport_mode' => env('VIU_TRANSPORT_MODE', 'http'), // 'http' | 'kafka'
'http' => [
'api_url' => env('VIU_API_URL', ''),
'api_key' => env('VIU_API_KEY', ''),
'timeout' => (int) env('VIU_HTTP_TIMEOUT', 5),
'max_retries' => (int) env('VIU_HTTP_MAX_RETRIES', 3),
],
'kafka' => [
'brokers' => env('VIU_KAFKA_BROKERS', 'localhost:9092'),
'topic' => env('VIU_KAFKA_TOPIC', 'logs.app.raw'),
'username' => env('VIU_KAFKA_USERNAME', ''),
'password' => env('VIU_KAFKA_PASSWORD', ''),
'sasl_mechanism' => env('VIU_KAFKA_SASL_MECHANISM', 'SCRAM-SHA-256'),
'security_protocol' => env('VIU_KAFKA_SECURITY_PROTOCOL', 'SASL_SSL'),
],
'batch_size' => (int) env('VIU_BATCH_SIZE', 100),
'level' => env('VIU_LOG_LEVEL', 'debug'),
'correlation_id_header' => env('VIU_CORRELATION_ID_HEADER', 'X-Correlation-ID'),
];
// Em um Job
public function handle(ViuLogger $logger): void
{
$logger->info('Job started', ['job' => static::class]);
// ...
$logger->info('Job completed');
$logger->flush(); // envia imediatamente
}
Viu::info('Job done');
Viu::flush();
bash
php artisan vendor:publish --tag=viu-config