Download the PHP package danielme85/laravel-log-to-db without Composer

On this page you can find all versions of the php package danielme85/laravel-log-to-db. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package laravel-log-to-db

Laravel Log-to-DB

GitHub PHP from Packagist GitHub release GitHub tag Codecov CodeFactor CircleCI

Hi, this is a custom Laravel 5.6+ Log channel handler that can store log events to SQL or MongoDB databases. Uses Laravel native logging functionality trough Monolog.

:warning: For Laravel version 10 and beyond, please use version 4 or later of this package.
For Laravel version 5.6 to 9, please use version 3.x.x.

Installation

Use the composer require or add to composer.json.

If you are using SQL database server to store log events you can use the migration included. The MongoDB driver does not require the migration. Copy the migration file for log the database table to your app (you can also do any changes you want or manually copy this file your database/migrations folder).

Run the Laravel migration artisan command.

For optional MongoDB support you need to install jenseegers/mongodb addon to Laravel

Configuration

Starting with Laravel 5.6 and later, you will have a new config file: "config/logging.php". You will need to add an array under 'channels' for Log-to-DB here like so:

These are the minimum required logging.php config settings to get started. Please note that the array index 'database' can be whatever string you like as long as it is unique to this logging config. You can also give the logging channel a name that later is referenced in a column in the DB table, this way you can have multiple logging-to-db channels.

More info about some of these options: https://laravel.com/docs/9.x/logging#customizing-monolog-for-channels

There are some default settings and more information about configuring the logger in the 'logtodb.php' config file. This could be copied to your project if you would like edit it with the vendor publish command.

You can also change these settings in your env file.

PLEASE NOTE: Starting with v2.2.0, the datetime column will be saved as a string in the format given in 'datetime_format' in logtodb.php config file, or the LOG_DB_DATETIME_FORMAT value in your .env file.

Config priority order

There are three places you can change different options when using log-to-db:

  1. The config file: config/logtodb.php (after doing vendor:publish).
  2. Your .env file will override settings in the logtodb.php config file.
  3. The Laravel logging config file: config/logging.php. You need to add a custom array here as mentioned above, in this same array you can specify/override config settings specifically for that log channel.

Config values set in point 1 & 2 would work as default for all new log channels you add in the "channels" array for the Laravel logging configuration (config/logging.php).

Log Worker Queue

It might be a good idea to save the log events with a Queue Worker. This way your server does not have to wait for the save process to finish. You would have to configure the Laravel Queue settings and run the Queue listener. https://laravel.com/docs/6.x/queues#running-the-queue-worker

The queue can be enabled/disabled in any of the following places:

Usage

Since this is a custom log channel for Laravel, all "standard" ways of generating log events etc should work with the Laravel Log Facade. See https://laravel.com/docs/6.x/logging for more information.

You can also log to specific log channels: Log::channel('database')debug("This is an test DEBUG log event");

Fetching Logs

The logging by this channel is done trough the Eloquent Model builder. LogToDB::model($channel, $connection, $collection); You can skip all function variables and the default settings from the config/logtodb.php will be used.

Some more examples of getting logs

When getting logs for specific channel or DB connection and collection you can either use the channel name matching config/logging.php or connection name from config/databases.php. You can also specify collection/table name if needed as the third function variable when fetching the model.

Custom Eloquent Model

Since Laravel is supposed to use static defined collection/table names, it might be better to use your own model in your app for a more solid approach. You can use your own eloquent model by referencing it in the config, then adding the trait: "LogToDbCreateObject"

SQL
MongoDB

LOG_DB_MODEL='App\Models\CustomLog'

WARNING: Fetching the model trough the dynamic Eloquent model (default behavior) have some side-effects as tables and connections are declared dynamically instead of assigned properties in the model class. Certain functions are broken like LogToDB::model->all(), while LogToDB::model->where()->get() will work as normal. Using your own models avoids these problems.

Model Closures and Observers

You can either add closures on your custom application model mentioned above, or add a model observer for the default LogToDb models.
Create a observer:

Then add to your AppServiceProvider (or another provider that calls the app boot function).

Adding tables/expanding collections

The Log handler for SQL expects the following schema:

This is the migration that ships with this plugin. You can add as many tables as you want, and reference them in the 'collection' config value. Collection = table, I used the term collection as it works for both SQL/noSQL. No migrations needed for MongoDB.

No indexes are added per default, so if you fetch a lot of log results based on specific time ranges or types: it might be a good idea to add some indexes.

Log Cleanup

There are config values that you can set to specify the max number of log records to keep, or the max record age in hours.

These option is set to false per default, these have to be set to desired integers before you can run the "log:delete" artisan command.

This command will delete records based on settings described above. Add this command to your Console/kernel.php, or run manually in cron etc to enable automatic cleanup.

Manual Cleanup

There is a helper function to remove the oldest log events and keep a specified number

Or based on date (most be valid date/datetime supported by strtotime()) http://php.net/manual/en/function.strtotime.php

Processors

Monolog ships with a set of processors, these will generate additional data and populate the 'extra' field. I've also added a couple of example processors in this pacage under src/Processors. To enable processors you can add them to the log config array:

You could also create your own custom processor, make sure they implement Monolog\Processor\ProcessorInterface.

Example of custom processor

More logging.php config examples

Lumen Installation

You also need to make sure that all the needed basic config values for logtodb is set by either:

Since we are using Lumen we need to specify the config and service providers in the "bootstrap/app.php" file.

Next step is to register the service provider, either in bootstrap/app.php or in app/Provider/AppServiceProvider.

After adding the service provider you should be able to run the database migration in Lumen with:

Please note that you need a working db connection in Lumen at this point.

And then maybe it works... ¯_(ツ)_/¯

Using worker queue to write log to db with Lumen

You would need to set up a queue driver in Lumen before you can use the queue (default is: QUEUE_CONNECTION=sync, which is basically no queue). More info about the queues in Lumen doc (they are mostly the same as Laravel). I would recommend the Redis queue driver but database should also work.

How to make Redis work in Lumen (in general).

install per command

OR add to composer.json

Or install other alternatives to predis.

Add service provider and enable eloquent in your bootstrap/app.php file (Eloquent only needed if you use the model/model helper class to fetch new log event);

Now you can set your .env values to use Redis for the queue and cache if you so please:

Local Testing With Docker

There is a helper bash script called 'runLocalTestInDocker.sh', that runs the following docker commands:

To run Docker is required, give execute rights to the script and run:

Development supported by:


All versions of laravel-log-to-db with dependencies

PHP Build Version
Package Version
Requires php Version ^8.1
illuminate/support Version ^10.0|^11.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package danielme85/laravel-log-to-db contains the following files

Loading the files please wait ....