PHPackages                             steadfast-collective/laravel-system-log - 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. steadfast-collective/laravel-system-log

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

steadfast-collective/laravel-system-log
=======================================

Provides a System Log model and helpers for contextful logging with a FilamentPHP Resource panel generator

v1.3(2mo ago)01.2k↓66.7%MITPHPPHP 8.3.\* || 8.4.\* || 8.5.\*CI passing

Since Oct 20Pushed 2mo agoCompare

[ Source](https://github.com/steadfast-collective/laravel-system-log)[ Packagist](https://packagist.org/packages/steadfast-collective/laravel-system-log)[ Docs](https://github.com/steadfast-collective/laravel-system-log)[ GitHub Sponsors](https://github.com/steadfast-collective)[ RSS](/packages/steadfast-collective-laravel-system-log/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (4)Dependencies (22)Versions (11)Used By (0)

Laravel System Log
==================

[](#laravel-system-log)

[![Latest Version on Packagist](https://camo.githubusercontent.com/d4d8762d32ec378d10b3a0c9e8e403a07b9688fe1952de22eb1025d483efc75d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7374656164666173742d636f6c6c6563746976652f6c61726176656c2d73797374656d2d6c6f672e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/steadfast-collective/laravel-system-log)[![Tests](https://camo.githubusercontent.com/9643bd27dcb151396ba5eba49f2124135a9cf4c1715e37b2f3c9e4352121174b/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f7374656164666173742d636f6c6c6563746976652f6c61726176656c2d73797374656d2d6c6f672f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/steadfast-collective/laravel-system-log/actions/workflows/run-tests.yml)[![Total Downloads](https://camo.githubusercontent.com/99984416b95fc3ccb0a39b33dc211511657ae5545e18c33894793c5e5d3dacc8/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7374656164666173742d636f6c6c6563746976652f6c61726176656c2d73797374656d2d6c6f672e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/steadfast-collective/laravel-system-log)

Provides a System Log model and helpers for contextful logging with FilamentPHP compatibility.

The main interface for this package is the `HasSystemLogger` trait which can be added to any class you want to track from to expose the `$this->addSystemLog()` method and to any object you want to pass as the `model` parameter to `addSystemLog`for some automatic context adding.

For example you might add it to an Eloquent Model and the Action which submits it to an external API to track API calls and data.

There is also a `HasSystemLoggerAssertions` trait for use in tests to check that a SystemLog has been created.

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

[](#installation)

You can require and setup the packge with these commands:

```
composer require steadfast-collective/laravel-system-log

# Publish and run the migrations to create the system_logs table:
php artisan vendor:publish --tag="system-log-migrations"
php artisan migrate

# Publish the config file (optional)
php artisan vendor:publish --force --tag="system-log-config"

# Run the install command to publish the model and factory, and if you have Filament
# installed to create to resource. Use the `--panel=Admin` option to customise which
# Filament panel it's added to.
php artisan system-log:install
```

The generated model and panel are now ready for you to customise to fit into your application.

Usage
-----

[](#usage)

You can add HasSystemLogger to any model you want to easily log from.

### Eloquent Model Example

[](#eloquent-model-example)

In this example we use SystemLogger to log when a local Eloquent model has been synced with an external API.

We add `HasSystemLogger` to the both the Model and the API class. The Model so it can provide the internal/external ID details and the API class for convenient access.

You could only use the system logger on the product and pass it in as the model, but I find this code more readable.

```
class Product extends Model
{
    use HasSystemLogger;

    public function submitRequest($product)
    {
        $this->addSystemLog(
            messasge: 'Making PUT Request',
            'log_level' => 'debug',
            context: [
                'product' => $product,
            ]
        );

        ...
    }

    public function getInternalId()
    {
        return $this->key();
    }

    public function getInternalType()
    {
        return get_class($this);
    }

    public function getExternalId()
    {
        return $this->external_api_id;
    }

    public function getExternalType()
    {
        return 'products';
    }
}

class SyncProduct
{
    function handle(Product $product)
    {
        $this->addSystemLog(
            'Syncing Product',
            model: $product,
            context: [
                'changedFields' = $product->getChanged(),
            ],
        );
    }
}
```

This creates a `SystemLog` model in your database, and also called `Log::$debug($message)`.

The `SystemLog` model will have some properties set:

FieldValueDescriptioninternal\_idDefaults to $this-&gt;key() for Eloquent modelsA unique identifier for the object in your local systeminternal\_typeDefaults to the classnameA descriptive name for the type of object in your local systemexternal\_id.Defaults to null - create getExternalId() to setA unique identifier for this object in a remote system.external\_typeDefaults to internal\_type - create getExternalType() to setA descriptive name for this type of object in a remote systemlog\_levelPSR-7 log levels (defaults to 'info')codeAny string of your choosingA code which represents what type of item this is (e.g. SYNC\_ERROR)messageAny string of your choosing (required)A description of what is being loggedcontextAn array of related dataAny other data you want to include with this logger### Testing

[](#testing)

Use the `HasSystemLoggerAssertions` trait in your tests to assert that a SystemLog has (or has not) been created.

```
use SteadfastCollective\LaravelSystemLog\Concerns\HasSystemLoggerAssertions;

class ProductTest extends TestCase
{
    use HasSystemLoggerAssertions;

    public function test_create_simple_system_log_no_context()
    {
        $product = new Product;
        $product->id = 12345;

        $product->doSomething();

        // Check doSomething created a SystemLog
        $this->assertSystemLogLogged(
            message: 'This is a test message',
            model: $product
        );
    }
}
```

### Formatting your log lines

[](#formatting-your-log-lines)

When you create a SystemLog the message will also be logged to the standard Larvel logger.

You can customise this message by adding a `makeLogMessage` method to your class.

For example to prefix the log message with the class name wrapped in square brackes:

```
class MyClass
{
    use HasSystemLogger;

    public function makeLogMessage(string $message): string
    {
        return '['.class_basename($this).'] '.$message;
    }
}
```

### FilamentPHP UI

[](#filamentphp-ui)

This package comes with a UI generator which allows you to browse the system logs in a FilamentPHP Resource.

The UI is generated so you're free to customise it as you please, or update it fom the latest version using `php artisan system-logs:install`.

The System Logs benefit from a very wide screen, add this snippet to your panel provider to make the system logs page full width. Modify the path as necessary.

```
