PHP code example of intelligent-intern / loki-bundle

1. Go to this page and download the library: Download intelligent-intern/loki-bundle 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/ */

    

intelligent-intern / loki-bundle example snippets



namespace App\Controller;

use App\Factory\LogServiceFactory;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;

class LoggingController extends AbstractController
{
    public function __construct(
        private LogServiceFactory $logServiceFactory
    ) {}

    public function logMessage(Request $request): JsonResponse
    {
        $message = $request->get('message', '');
        $level = $request->get('level', 'info');

        if (empty($message)) {
            return new JsonResponse(['error' => 'Message cannot be empty'], 400);
        }

        try {
            $logger = $this->logServiceFactory->create();
            $logger->log($level, $message);

            return new JsonResponse(['message' => 'Log written successfully']);
        } catch (\Exception $e) {
            return new JsonResponse(['error' => $e->getMessage()], 500);
        }
    }