PHP code example of danilovl / log-viewer-bundle

1. Go to this page and download the library: Download danilovl/log-viewer-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/ */

    

danilovl / log-viewer-bundle example snippets


return [
    // ...
    Danilovl\LogViewerBundle\LogViewerBundle::class => ['all' => true],
];

use Danilovl\LogViewerBundle\Event\LogViewerDownloadEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class LogViewerSubscriber implements EventSubscriberInterface
{
    public static function getSubscribedEvents(): array
    {
        return [
            LogViewerDownloadEvent::class => 'onDownload'
        ];
    }

    public function onDownload(LogViewerDownloadEvent $event): void
    {
        $source = $event->source;
        // Logic to check if the user can download this file
        if ($source->name === 'security.log') {
            $event->stopPropagation();
        }
    }
}

namespace App\Parser;

use Danilovl\LogViewerBundle\DTO\LogEntry;
use Danilovl\LogViewerBundle\Interfaces\LogInterfaceParser;
use Danilovl\LogViewerBundle\Interfaces\LogParserGoPatternInterface;
use Danilovl\LogViewerBundle\Util\DateNormalizer;

class MyCustomParser implements LogInterfaceParser, LogParserGoPatternInterface
{
    private const string PATTERN = '/(?P<timestamp>\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}) (?P<level>\w+) (?P<message>.*)/';

    public function parse(string $line, string $filename): LogEntry
    {
        $match = preg_match(self::PATTERN, $line, $matches);
        if (!$match) {
            return new LogEntry(
                timestamp: '',
                level: 'INFO',
                channel: 'custom',
                message: $line,
                file: $filename,
                normalizedTimestamp: '',
                context: []
            );
        }

        return new LogEntry(
            timestamp: $matches['timestamp'],
            level: $matches['level'] ?? 'INFO',
            channel: 'custom',
            message: $matches['message'] ?? $line,
            file: $filename,
            normalizedTimestamp: DateNormalizer::normalize($matches['timestamp']),
            context: []
        );
    }

    public function getName(): string
    {
        return 'my_custom_format';
    }

    public function supports(?string $parserType): bool
    {
        return $parserType === 'my_custom_format';
    }

    public function getPattern(): string
    {
        return self::PATTERN;
    }

    public function getDateFormat(): string
    {
        return 'Y-m-d H:i:s';
    }

    public function getGoParserName(?string $parserType): string
    {
        return 'custom';
    }

    public function getGoPattern(?string $parserType): string
    {
        return '(?P<timestamp>\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}) (?P<level>\w+) (?P<message>.*)';
    }
}
bash
php bin/console assets:install