PHP code example of frddl / laravel-simple-logging

1. Go to this page and download the library: Download frddl/laravel-simple-logging 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/ */

    

frddl / laravel-simple-logging example snippets


use Frddl\LaravelSimpleLogging\Traits\SimpleLoggingTrait;

class YourController extends Controller
{
    use SimpleLoggingTrait;
    
    // Your methods here
}

public function yourMethod(Request $request)
{
    return $this->logMethod('Your Method Name', $request->all(), function() use ($request) {
        // Your business logic here - automatic start/end logging!
        return response()->json(['message' => 'Success']);
    });
}

public function processOrder(Request $request)
{
    return $this->logMethod('Process Order', $request->all(), function() use ($request) {
        // Monitor input data and validation
        $this->log('Order validation started', [
            'order_data' => $request->all(),
            'user_id' => $request->user_id,
            'items_count' => count($request->items ?? [])
        ], 'info');
        
        // Track business logic execution
        $order = Order::create($request->validated());
        $this->log('Order created successfully', [
            'order_id' => $order->id,
            'total_amount' => $order->total,
            'payment_method' => $order->payment_method
        ], 'info');
        
        // Monitor external API calls
        $payment = $this->processPayment($order);
        $this->log('Payment processed', [
            'payment_id' => $payment->id,
            'status' => $payment->status,
            'amount' => $payment->amount
        ], 'info');
        
        // Track warnings and potential issues
        if ($payment->amount !== $order->total) {
            $this->log('Payment amount mismatch detected', [
                'order_total' => $order->total,
                'payment_amount' => $payment->amount,
                'difference' => $payment->amount - $order->total
            ], 'warning');
        }
        
        return response()->json(['order' => $order]);
    });
}

return [
    'enabled' => env('SIMPLE_LOGGING_ENABLED', true),
    'file_logging' => env('SIMPLE_LOGGING_FILE_LOGGING', false),
    'database_logging' => env('SIMPLE_LOGGING_DATABASE_LOGGING', true),
    'log_level' => env('SIMPLE_LOGGING_LEVEL', 'info'),
    'route_prefix' => 'logs',
    'middleware' => ['web'],
    'viewer' => [
        'per_page' => 50,
    ],
    'export' => [
        'max_records' => 100,
    ],
    'cleanup_old_logs_days' => env('SIMPLE_LOGGING_CLEANUP_DAYS', 30),
];

// In config/simple-logging.php
'cleanup_old_logs_days' => env('SIMPLE_LOGGING_CLEANUP_DAYS', 30),

// In your .env file
SIMPLE_LOGGING_CLEANUP_DAYS=30

'cleanup_old_logs_days' => null, // Disable cleanup

public function processOrder(Request $request)
{
        return $this->logMethod('Process Order', $request->all(), function() use ($request) {
        $order = Order::create($request->validated());
        
        $this->log('Order created', ['order_id' => $order->id], 'info');
        
        // Process payment
        $payment = $this->processPayment($order);
        
        $this->log('Payment processed', ['payment_id' => $payment->id], 'info');
        
        return response()->json(['order' => $order]);
    });
}

// Monitor user actions and debug variables
$this->log('User authentication started', [
    'email' => $request->email,
    'ip_address' => $request->ip(),
    'user_agent' => $request->userAgent(),
    'session_id' => session()->getId()
], 'info');

// Track business logic with complex data structures
$this->log('Order processing initiated', [
    'order_id' => $order->id,
    'customer_data' => $order->customer->toArray(),
    'items' => $order->items->map(function($item) {
        return [
            'name' => $item->name,
            'price' => $item->price,
            'quantity' => $item->quantity
        ];
    }),
    'total_calculation' => [
        'subtotal' => $order->subtotal,
        'tax' => $order->tax,
        'shipping' => $order->shipping,
        'total' => $order->total
    ]
], 'info');

// Monitor warnings and potential issues
$this->log('Low inventory warning', [
    'product_id' => $product->id,
    'current_stock' => $product->stock,
    'requested_quantity' => $quantity,
    'threshold' => $product->low_stock_threshold
], 'warning');

// Track errors with full context
$this->log('Payment processing failed', [
    'error' => $exception->getMessage(),
    'order_id' => $order->id,
    'payment_data' => $paymentData,
    'retry_count' => $retryCount,
    'stack_trace' => $exception->getTraceAsString()
], 'error');

config(['simple-logging.enabled' => false]);

// This will be automatically masked
$this->log('User data', [
    'password' => 'secret123',           // → [HIDDEN]
    'token' => 'abc123xyz',              // → [HIDDEN]
    'authorization' => 'Bearer token123' // → Bearer [HIDDEN]
]);



namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Http\Requests\CreateOrderRequest;
use Frddl\LaravelSimpleLogging\Traits\SimpleLoggingTrait;

class OrderController extends Controller
{
    use SimpleLoggingTrait;

    public function store(CreateOrderRequest $request)
    {
        return $this->logMethod('Create Order', $request->all(), function() use ($request) {
            $this->log('Validating order data', ['items_count' => count($request->items)]);
            
            $order = Order::create($request->validated());
            
            $this->log('Order created successfully', ['order_id' => $order->id]);
            
            // Process payment
            $payment = $this->processPayment($order);
            
            if (!$payment->success) {
                $this->log('Payment failed', ['error' => $payment->error], 'error');
                throw new PaymentException('Payment processing failed');
            }
            
            $this->log('Payment successful', ['payment_id' => $payment->id]);
            
            return response()->json(['order' => $order], 201);
        });
    }
    
    private function processPayment($order)
    {
        return $this->logMethod('Process Payment', ['order_id' => $order->id], function() use ($order) {
            // Payment logic here
            return (object) ['success' => true, 'id' => 'pay_123'];
        });
    }
}
bash
php artisan vendor:publish --provider="Frddl\LaravelSimpleLogging\SimpleLoggingServiceProvider" --tag="config"
php artisan vendor:publish --provider="Frddl\LaravelSimpleLogging\SimpleLoggingServiceProvider" --tag="migrations"
bash
php artisan migrate