GitXplorerGitXplorer
d

CakePHP-DatabaseLog

public
43 stars
21 forks
0 issues

Commits

List of commits on branch master.
Verified
1e6c424f5854950e3a7d17ddcc23e6b425866007

PHPStan updates

ddereuromark committed 2 months ago
Verified
cd1bb9641fdd17020862fa0337dbc0eeb2abbea8

Delete tests/TestCase/Shell/DatabaseLogShellTest.php

ddereuromark committed 2 months ago
Verified
f3f6d30a0bdea26addeb54fc4d12dae37e02f00a

Add test entry part.

ddereuromark committed 2 months ago
Verified
71a185aa817936b18d4c5e5bd38e85f10ebe1661

CI PHP versions

ddereuromark committed 2 months ago
Verified
50b1c2910ece5bbd3c9c24d011c3d0b9f91115ee

Sync CI script

ddereuromark committed 2 months ago
Verified
e3a838175d0527a86394262c7f731de1af3303fc

Sync CI script

ddereuromark committed 2 months ago

README

The README file for this repository.

CakePHP DatabaseLog Plugin

CI Coverage Status Latest Stable Version Minimum PHP Version License Total Downloads

DatabaseLog engine for CakePHP applications.

This branch is for CakePHP 5.0+. See version map for details.

Features

  • Easy setup and almost no dependencies.
  • Detailed log infos added for both Web and CLI log entries.
  • Defaults to SQLite as single app application lightweight approach.
  • Ideal for multi-server or serverless applications where logging to a file is just not convenient.
  • If DB is used, fallback to SQLite in case the DB is not reachable.
  • Simple admin interface to view/delete logs included.
  • Basic monitoring and alert system included.
  • Export to TXT files possible.

Log Rotation

While file handling requires file log rotation and splitting into chunks of (compressed) files, a database approach can more easily keep the logs together in a single database. This is more convinient when looking through them or searching for something specific.

This plugin internally combines log entries of the exact same "content" into a single row with an increased count. Additionally, you would want to add a cronjob triggered cleanup shell to keep the total size and row count below a certain threshold.

Demo

Clone and install the sandbox app, create some errors and browse the admin backend for the logs overview.

Or just attach it to your app directly. Time needed: 5 minutes.

Install

Composer (preferred)

composer require dereuromark/cakephp-databaselog

Setup

Enable the plugin in your Application class:

$this->addPlugin('DatabaseLog');

or just call:

bin/cake plugin load DatabaseLog

You can simply modify the existing config entries in your config/app.php:

   'Log' => [
   	'debug' => [
   		'className' => 'DatabaseLog.Database',
   	],
   	'error' => [
   		'className' => 'DatabaseLog.Database',
   	],
   	...
   ],

This will use the database_log connection and an SQLite file database by default, stored in your logs folder.

Using an actual database (optional)

Create a config setting in your config/app.php what database connection it should log to:

'DatabaseLog' => [
	'connection' => 'custom',
],

It is recommended to not use the same connection as your production server (default) because when the DB is not reachable logging to it will also not be possible. In that case it will fall back to SQLite file logging on this server instance, though.

Once the connection is reachable, the database table (defaulting to database_logs) will be automatically created. No need to manually run any migration or SQL script here.

You can also manually create the table beforehand, if you prefer:

bin/cake Migrations migrate -p DatabaseLog

If you use a custom connection, make sure to set the connection here for migrations:

bin/cake Migrations migrate -p DatabaseLog -c custom

Fully tested so far are PostgreSQL and MySQL, but by using the ORM all major databases should be supported.

For default connection usage only: You can also just copy the migration file(s) into your app /config/Migrations/, modify if needed, and then run it as part of your app migrations.

Usage

Anywhere in your app where you call $this->log() or Log::write() the DatabaseLog engine will be used.

$this->log('This is a detailed message logged to the database', 'error');
// or
Log::write('error', 'This is a detailed message logged to the database');

There is also a browsable web backend you can view your logs with.

See Docs for more details.