PHPackages                             sinemacula/laravel-log-database - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Logging &amp; Monitoring](/categories/logging)
4. /
5. sinemacula/laravel-log-database

ActiveLibrary[Logging &amp; Monitoring](/categories/logging)

sinemacula/laravel-log-database
===============================

A database Monolog log driver for Laravel - persists log records to an Eloquent-backed table.

00PHPCI passing

Since Jun 26Pushed todayCompare

[ Source](https://github.com/sinemacula/laravel-log-database)[ Packagist](https://packagist.org/packages/sinemacula/laravel-log-database)[ RSS](/packages/sinemacula-laravel-log-database/feed)WikiDiscussions master Synced today

READMEChangelog (1)DependenciesVersions (2)Used By (0)

Laravel Log Database
====================

[](#laravel-log-database)

[![Latest Stable Version](https://camo.githubusercontent.com/bd34796e6f756d363eefb2cef144e4459f76264d5591e60c5ed969112950bf89/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f73696e656d6163756c612f6c61726176656c2d6c6f672d64617461626173652e737667)](https://packagist.org/packages/sinemacula/laravel-log-database)[![Build Status](https://github.com/sinemacula/laravel-log-database/actions/workflows/tests.yml/badge.svg?branch=master)](https://github.com/sinemacula/laravel-log-database/actions/workflows/tests.yml)[![Quality Gates](https://github.com/sinemacula/laravel-log-database/actions/workflows/quality-gates.yml/badge.svg?branch=master)](https://github.com/sinemacula/laravel-log-database/actions/workflows/quality-gates.yml)[![Maintainability](https://camo.githubusercontent.com/785dc8af7eb8088ebb0d1f34d3079e662b43674a670708fca494e3bc93d07c0e/68747470733a2f2f716c74792e73682f67682f73696e656d6163756c612f70726f6a656374732f6c61726176656c2d6c6f672d64617461626173652f6d61696e7461696e6162696c6974792e737667)](https://qlty.sh/gh/sinemacula/projects/laravel-log-database)[![Code Coverage](https://camo.githubusercontent.com/2df1d219dde2274305e1bdb2319952ce79e0c33fb332749fb06daa0d472496ab/68747470733a2f2f716c74792e73682f67682f73696e656d6163756c612f70726f6a656374732f6c61726176656c2d6c6f672d64617461626173652f636f7665726167652e737667)](https://qlty.sh/gh/sinemacula/projects/laravel-log-database)[![Total Downloads](https://camo.githubusercontent.com/7922851c8f66b5cb4baebf09926d3948cfb37336e17199b3cc2762aa4f709368/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f73696e656d6163756c612f6c61726176656c2d6c6f672d64617461626173652e737667)](https://packagist.org/packages/sinemacula/laravel-log-database)

A custom Monolog log driver for Laravel that persists log records to a database table through an Eloquent model. Registering the package's `database` log channel routes your application's logs into a `logs` table instead of, or alongside, the filesystem, so they can be queried, filtered, and retained like any other model data.

The driver is deliberately thin. It adds per-channel minimum-level filtering, serialises any exception in the log context to a string before storage, prunes old records on a retention schedule, and falls back to a configured channel stack if the database write ever fails, so a logging backend outage never costs you the log line.

How It Works
------------

[](#how-it-works)

Laravel's logging stack is built on Monolog. This package registers a custom `database` driver with the log manager; when you log to that channel, records flow through `DatabaseHandler`, which writes them to the `logs` table via the `LogMessage` Eloquent model.

A few rules hold for every record before it is stored:

- **Level filtering.** Records below the channel's configured minimum level are dropped before any database work is done.
- **Exception serialisation.** If the context carries a `Throwable` under the `exception` key, it is cast to a string, so the full stack trace is stored as text rather than a serialised object.
- **Fallback on failure.** If the insert throws (for example, the database is unreachable), the record is re-emitted to a configured fallback channel stack so it is never silently lost.
- **Retention pruning.** `LogMessage` is mass-prunable, so Laravel's `model:prune` command removes records older than the configured retention window.

Installation
------------

[](#installation)

```
composer require sinemacula/laravel-log-database
```

The service provider is registered automatically through package discovery. Publish and run the migration to create the `logs` table:

```
php artisan vendor:publish --tag=log-database-migrations
php artisan migrate
```

Configuration
-------------

[](#configuration)

Register a `database` channel in `config/logging.php`:

```
'database' => [
    'driver'     => 'database',
    'level'      => env('LOG_LEVEL', 'debug'),
    'days'       => env('LOG_DATABASE_DAYS', 30),
    'connection' => env('LOG_DATABASE_CONNECTION'),
],
```

- `level` - the minimum Monolog level to persist; anything below it is ignored.
- `days` - how many days records are kept before `model:prune` removes them. Pruning is disabled when this is unset, zero, or non-numeric (it never deletes the whole table).
- `connection` - optional database connection to write logs on. Leave unset to use the application's default connection; point it at a separate connection to keep logs out of the host request's transaction (see Considerations).

Define a fallback channel that is used when a database write fails:

```
'fallback' => [
    'driver'   => 'stack',
    'channels' => ['single'],
],
```

Then point your application at the channel by setting `LOG_CHANNEL=database`, or include `database` in a `stack` channel to write to several destinations at once.

Usage
-----

[](#usage)

Once the `database` channel is configured, log to it like any other Laravel channel:

```
use Illuminate\Support\Facades\Log;

Log::channel('database')->info('Order shipped', ['order_id' => 42]);
Log::channel('database')->error('Payment failed', ['exception' => $throwable]);
```

Or set `LOG_CHANNEL=database` to make it the default destination for the `Log` facade and the `logger()` helper:

```
Log::warning('Cache miss rate is high', ['rate' => 0.82]);
```

The `exception` entry is stored as its full string representation; all other context is stored as JSON.

Pruning Old Records
-------------------

[](#pruning-old-records)

`LogMessage` is mass-prunable. Schedule Laravel's `model:prune` command to delete records past the retention window configured by the channel's `days` option:

```
use Illuminate\Support\Facades\Schedule;
use SineMacula\Log\Database\Models\LogMessage;

Schedule::command('model:prune', ['--model' => LogMessage::class])->daily();
```

Records older than `logging.channels.database.days` days are removed on each run.

Table Schema
------------

[](#table-schema)

ColumnTypeNotes`id``uuid` (primary)Generated via `HasUuids``level``string`Monolog level name (e.g. `INFO`, `ERROR`), indexed`channel``string`, nullableThe emitting Monolog channel`message``longText`The log message`context``json`, nullableLog context as JSON; throwables serialised to a string`extra``json`, nullableMonolog processor output (e.g. correlation ids)`created_at``datetime(6)`Record timestamp, microsecond precision, indexedConsiderations
--------------

[](#considerations)

- **Writes are synchronous.** Each record is written to the database inline, on the request that logged it. This keeps logs durable and immediately queryable, but high-volume logging on a hot path adds per-record write latency - raise the channel's minimum `level`, or route verbose channels elsewhere, if that matters for your workload.
- **Transactions.** Because the driver writes through Eloquent, a record logged inside a database transaction that later rolls back is rolled back with it. Set a separate `connection` on the channel to write logs outside the host request's transaction.
- **Sensitive data.** Messages, context, and serialised exception traces are stored in cleartext in a queryable table - a wider exposure surface than a host file log (replicas, backups, snapshots). Attach a [Monolog processor](https://laravel.com/docs/logging#customizing-monolog-for-channels) to the channel to scrub sensitive values before they are persisted, and consider setting `zend.exception_ignore_args=1` so exception traces do not capture call arguments.

Requirements
------------

[](#requirements)

- PHP ^8.3
- Laravel 12 (`illuminate/support ^12.0`)

Testing
-------

[](#testing)

```
composer test                # PHPUnit suite in parallel via Paratest
composer test:coverage       # suite with Clover coverage output
composer test:mutation       # Infection mutation gate (min MSI 90)
composer test:mutation:full  # full mutation suite without thresholds
composer check               # static analysis and lint via qlty
composer format              # format via qlty
composer smells              # duplication / complexity smells via qlty
```

Changelog
---------

[](#changelog)

See [CHANGELOG.md](CHANGELOG.md) for a list of notable changes.

Contributing
------------

[](#contributing)

Contributions are welcome. Please read [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines on branching, commits, code quality, and pull requests.

Security
--------

[](#security)

If you discover a security vulnerability, please report it responsibly. See [SECURITY.md](SECURITY.md) for the disclosure policy and contact details.

License
-------

[](#license)

Licensed under the [Apache License, Version 2.0](https://www.apache.org/licenses/LICENSE-2.0).

###  Health Score

21

—

LowBetter than 18% of packages

Maintenance65

Regular maintenance activity

Popularity0

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity13

Early-stage or recently created project

 Bus Factor1

Top contributor holds 75% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/6262ea965c244b0c946a2f29a94da05e30846c066a0b59399466216654c78fe6?d=identicon)[sinemacula](/maintainers/sinemacula)

---

Top Contributors

[![sinemacula-ben](https://avatars.githubusercontent.com/u/118753672?v=4)](https://github.com/sinemacula-ben "sinemacula-ben (3 commits)")[![sine-macula-releases[bot]](https://avatars.githubusercontent.com/in/4070845?v=4)](https://github.com/sine-macula-releases[bot] "sine-macula-releases[bot] (1 commits)")

### Embed Badge

![Health badge](/badges/sinemacula-laravel-log-database/health.svg)

```
[![Health](https://phpackages.com/badges/sinemacula-laravel-log-database/health.svg)](https://phpackages.com/packages/sinemacula-laravel-log-database)
```

###  Alternatives

[psr/log

Common interface for logging libraries

10.4k1.2B10.9k](/packages/psr-log)[open-telemetry/api

API for OpenTelemetry PHP.

1938.5M261](/packages/open-telemetry-api)[open-telemetry/sdk

SDK for OpenTelemetry PHP.

2326.5M315](/packages/open-telemetry-sdk)[illuminated/console-logger

Logging and Notifications for Laravel Console Commands.

8676.7k](/packages/illuminated-console-logger)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
