Skip to content

Instantly share code, notes, and snippets.

@helloworldlab
Created October 18, 2021 02:09
Show Gist options
  • Select an option

  • Save helloworldlab/aa8c100879858adfe82f207c5ebd9f00 to your computer and use it in GitHub Desktop.

Select an option

Save helloworldlab/aa8c100879858adfe82f207c5ebd9f00 to your computer and use it in GitHub Desktop.
Log Viewer

Pada Controller

public function senaraiRalat()
{
    $senaraiNamaFail = [];

    $files = File::files(storage_path('logs'));

    $senaraiNamaFail = array_reverse(
        array_map(fn ($file) => $file->getFilename(), $files)
    );

    if (request()->filled('nama_fail') && in_array(request()->query('nama_fail'), $senaraiNamaFail)) {
        $file = array_filter(
            $files,
            fn ($file) => $file->getFilename() == request()->query('nama_fail')
        );

        $logContent = array_values($file)[0]->getContents();
    } else {
        $logContent = isset($files[0]) ? $files[0]->getContents() : null;
    }

    preg_match_all(
        '/^\[(?<date>.*)\]\s(?<env>\w+)\.(?<type>\w+):(?<message>.*)/m',
        $logContent,
        $senaraiRalat,
        PREG_SET_ORDER,
        0
    );

    return view('pengurusan.ralat.senarai', compact(
        'senaraiNamaFail',
        'senaraiRalat'
    ));
}

Pada View

<section>
    <form id="form-nama-fail">
        <div class="mb-3">
            <select class="form-control" id="select-fail-log" name="nama_fail" onchange="event.preventDefault();document.getElementById('form-nama-fail').submit();">
                <option disabled selected hidden>Sila Pilih</option>
                @foreach ($senaraiNamaFail as $namaFail)
                    <option value="{{ $namaFail }}" {{ $namaFail == request()->query('nama_fail') ? 'selected' : null }}>{{ $namaFail }}</option>
                @endforeach
            </select>
        </div>
    </form>
</section>

<section class="table-responsive">
    <table class="table table-hover">
        <thead>
            <tr>
                <th>Tarikh/Masa</th>
                <th>Jenis</th>
                <th>Ralat</th>
                <th></th>
            </tr>
        </thead>
        <tbody>
            @foreach ($senaraiRalat as $ralat)
                <tr class="align-middle">
                    <td class="font-monospace text-nowrap" style="width: 14%;">{{ Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $ralat['date'])->format('d-m-Y H:i:s') ?? 'Tiada maklumat' }}</td>
                    <td class="font-monospace">
                        @if (isset($ralat['type']) && $ralat['type'] == 'ERROR')
                            <span class="badge bg-danger">ERROR</span>
                        @endif
                    </td>
                    <td class="font-monospace">{{ Str::limit($ralat['message'], 110) ?? 'Tiada maklumat' }}</td>
                    <td class="text-end">
                        <a class="btn btn-outline-secondary btn-sm fw-bold" href="#collapse-{{ $loop->iteration }}" data-bs-toggle="collapse">
                            <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-chevron-bar-down" viewBox="0 0 16 16">
                                <path fill-rule="evenodd" d="M3.646 4.146a.5.5 0 0 1 .708 0L8 7.793l3.646-3.647a.5.5 0 0 1 .708.708l-4 4a.5.5 0 0 1-.708 0l-4-4a.5.5 0 0 1 0-.708zM1 11.5a.5.5 0 0 1 .5-.5h13a.5.5 0 0 1 0 1h-13a.5.5 0 0 1-.5-.5z"/>
                            </svg>
                        </a>
                    </td>
                </tr>
                <tr class="collapse bg-light" id="collapse-{{ $loop->iteration }}">
                    <td class="font-monospace p-4" colspan="4">
                        {{ $ralat['message'] ?? 'Tiada maklumat' }}
                    </td>
                </tr>
            @endforeach
        </tbody>
    </table>
</section>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment