DatabaseLog CakePHP plugin to log into DB instead of files. Better to filter and search.

Overview

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 4.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 light weight 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' => [
	'datasource' => 'my_datasource', // DataSource to use
]

It is recommended to not use the same datasource 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 fallback to SQLite file logging on this server instance, though.

Once the datasource is reachable and once the first log entry is being written, 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 before hand, if you prefer:

bin/cake Migrations migrate -p DatabaseLog

Or just copy the migration file into your app /config/Migrations, modify if needed, and then run it as part of your app migrations.

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

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.

Comments
  • Some fatal/syntax errors do not log

    Some fatal/syntax errors do not log

    What I did

    • I have tried using this plugin with PHP 7.1.11 and a dedicated mysql datasource. Basic db logging works.
    • Even if I add syntax errors to my controller action, it works.
    • However if I add syntax/fatal errors to my .ctp templates, it does not show up neither in the database log (nor potential file logs).

    Can you confirm? Any idea why?

    bug 
    opened by inoas 9
  • Duplicate errors being written to the DB table

    Duplicate errors being written to the DB table

    Running CakePHP v3.3.16 I've noticed a very strange issue with logging manually (i.e. using Log::write() from a controller or a view)

    It seems that logs are being written to the database twice. After several testing I've noticed that the issue is due to the config in the app.php file;

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

    With the above config, the logged will save two records in the table. But the following config will save only one record: 'Log' => [ 'debug' => [ 'className' => 'DatabaseLog.Database' ] ],

    opened by thomasgatt 8
  • Fatal error on logging with uploads

    Fatal error on logging with uploads

    Hi. I'm having trouble using the plugin with the friendsofcake/upload plugin. I'm getting the error below.

    Fatal error: Allowed memory size of 2151677952 bytes exhausted (tried to allocate 20480 bytes) in /var/www/mydevsys/vendor/cakephp/cakephp/src/ORM/Marshaller.php on line 75

    I tried increasing the memory limit but this increased the execution time and the error persisted. I'm using MySql to log the messages. Please assist.

    opened by hammertime44 7
  • Pagination fix

    Pagination fix

    With this patch it is possible to overwrite pagination options from the child classes. For example:

    <?php
    namespace App\Controller;
    
    use DatabaseLog\Controller\Admin\LogsController as BaseController;
    
    class LogsController extends BaseController
    {
        public function initialize()
        {
            parent::initialize();
            $this->paginate['limit'] = 5;
        }
    }
    
    opened by mamchenkov 6
  • DatbaseLogsTable::ensureTables() causes app to crash in environments without test connection

    DatbaseLogsTable::ensureTables() causes app to crash in environments without test connection

    I have test connection configured only in my dev environment.

    On production where there is no test connection defined I get an error:

    Error: The datasource configuration test was not found in config/app.php

    which is caused by DatbaseLogsTable::ensureTables() method. I think this (ensuring tables) should be at least optional.

    opened by r34117y 5
  • Missing Controller Error

    Missing Controller Error

    Running version 3.0.0 of this plugin on CentOS 7 in CakePHP 4.1.3. Followed the installation/configuration instructions. Plugin is loaded with 'routes' => true option. The Friends of Cake "Search" plugin is also loaded. When I try to load localhost/admin/database-log/logs, I get the following error:

    DatabaseLog.LogsController could not be found

    The LogsController is present, however.

    When I just load localhost/admin/database-log, I can see the error log entries. I cannot , however, use any of the filters or any of the actions (Remove Duplicates, Reset Logs) without getting the following error:

    Search.PrgComponent could not be found.

    I'm guessing that's because I should be loading admin/database-log/logs instead of admin/database-log. Can you suggest what I should look for to fix this?

    opened by die7fox 4
  • Altering URI field

    Altering URI field

    JavaScript DataTables plugin uses GET request by default to perform AJAX calls for loading table content. In certain occassions it started hitting VARCHAR(255) column limits.

    SQLSTATE[22001]: String data, right truncated: 1406 Data too long for column 'uri'

    As a quick fix, we can alter 'uri' column into text, that will let us store longer URI's in the table.

    enhancement 
    opened by anvyst 3
  • working with AuthComponent

    working with AuthComponent

    Hello there,

    nice plugin. I am curious how can it be used with AuthComponent. In Plugin/DatabaseLog/Controllers I added a befreFilter function with $this->Auth->allow();

    If I update the plugin through composer, won't these files be overwritten? Is there a way not to do so?

    question 
    opened by hkosmidi 3
  • Deprecated errors

    Deprecated errors

    I'm getting depreciation errors.

    CakePHP 3.6.x

    Deprecated: DatabaseLog\Model\Table\DatabaseLogsTable::displayField() is deprecated. Use setDisplayField()/getDisplayField() instead. - 
    C:\xampp\htdocs\pos-certificados\vendor\dereuromark\cakephp-databaselog\src\Model\Table\DatabaseLogsTable.php, line: 46
    

    and

    Deprecated: DatabaseLog\Model\Table\DatabaseLogsTable::connection() is deprecated. Use setConnection()/getConnection() instead. - 
    C:\xampp\htdocs\pos-certificados\vendor\dereuromark\cakephp-databaselog\src\Model\Table\LazyTableTrait.php, line: 56
    
    opened by IvanovAlmeida 2
  • Unit-Testing doesn't seem to work

    Unit-Testing doesn't seem to work

    Hi!

    I use this plugin and a slightly altered version of the cakephp-app template and the testMissingTemplateInDebug() https://github.com/cakephp/app/blob/master/tests/TestCase/Controller/PagesControllerTest.php#L75 test fails on me.

    I think it might be because a missingTemplate Exception is thrown (deliberately), which starts this database log plugin, but it's looking for a test-log datasource configuration: Cake\Datasource\Exception\MissingDatasourceConfigException: The datasource configuration "test_database_log" was not found. That config doesn't seem to exist.

    How can we fix that, how can we ensure the test_database_log config does indeed exist? We could add the test_database_log connection in your bootstrap.php, just like we do with database_log, but I'm not sure that is the way to go. Do you have any insight on this? Except Disabling databaselog for testing https://github.com/dereuromark/CakePHP-DatabaseLog/tree/master/docs#disabling-for-tests. :)

    enhancement 
    opened by liviakuenzli 2
  • Update README.md

    Update README.md

    We tried using the plugin with just the default settings (loading the plugin and changing the entries in config/app.php as described in README.md). It didn't work, kept telling me that "datasource configuration "database_log" was not found." I figured out that we would need to add said datasource configuration and that this is done in your bootstrap file, which therefore should be included when loading the plugin.

    opened by liviakuenzli 2
Releases(3.2.0)
  • 3.2.0(Nov 1, 2021)

    Improvements

    • Use FrozenTime class where possible instead of basic string usage for date(time) stamps.
    • Remove deprecations where possible.

    Note: CakePHP 4.3+ and PHP 7.3+ now

    Source code(tar.gz)
    Source code(zip)
  • 3.1.0(Apr 9, 2021)

    Improvements

    Better defaults to avoid size overload

    • Display DB type and size in dashboard if applicable.
    • Log rotation and alerting system improvements
    Source code(tar.gz)
    Source code(zip)
  • 3.0.1(Nov 6, 2020)

  • 3.0.0(Aug 31, 2020)

    CakePHP 4 release

    A standalone logging solution for CakePHP apps as database driven approach. Instead of log files, it logs into an easily searchable and filterable DB backend.

    • Detailed log infos added for both Web and CLI log entries.
    • Defaults to SQLite as single app application light weight approach.
    • Ideal for multi-server or serverless applications where logging to a file is just not convenient.
    • Basic monitoring and alert system included.
    Source code(tar.gz)
    Source code(zip)
  • 2.7.2(Mar 24, 2020)

  • 3.0.0-beta(Jan 23, 2020)

  • 2.7.1(Jul 23, 2019)

    Improvements

    • Display last errors on dashboard now.
    • Remove duplicates by default in non-strict mode (using summary)
    • Display URI / CLI command on logs index now
    Source code(tar.gz)
    Source code(zip)
  • 2.7.0(Mar 12, 2019)

    Improvements

    For larger log messages (mediumtext) and larger count of rows in your logs table, pagination and other operations will soon become super-slow or even kill the SQL server. A summary field now takes a truncated version (length of 255), and only that is searchable by default now.

    This keeps performance at a good rate. If you need to dive into message content (fulltext search), you need to implement something on top here.

    A dashboard has been added for convenience entry into the backend. The backend is now also Foundation 5+ and BS 3+ compatible in a very basic way.

    Migration note: Please make sure you run the included migration. For some DB types it might be necessary to reset the logs here (empty all rows), or manually create valid non-empty summary content.

    Source code(tar.gz)
    Source code(zip)
  • 2.6.0(Jan 22, 2019)

  • 2.5.2(Jan 10, 2019)

    Improvements

    You can add additional infos into the stacktrace via custom saveCallback callable:

    // in your app.php config
    'DatabaseLog' => [
    	'saveCallback' => function (\DatabaseLog\Model\Entity\DatabaseLog $databaseLog) {
    		if (empty($_SESSION) || empty($_SESSION['language'])) {
    			return;
    		}
    		$currentSessionLanguage = $_SESSION['language'];
    		$databaseLog->message .= PHP_EOL . 'Language: ' . $currentSessionLanguage;
    	},
    ],
    

    This will run after all the internal processing of the entity has been done, prior to actually saving the log.

    Source code(tar.gz)
    Source code(zip)
  • 2.5.1(Jan 9, 2019)

  • 2.5.0(Nov 21, 2018)

    Improvements

    • Added CLI vs WEB logging info.
    • Added isCli() entity method to quickly check on this and display command info in Logs view action.
    Source code(tar.gz)
    Source code(zip)
  • 2.4.0(Apr 19, 2018)

    Included commits: https://github.com/dereuromark/CakePHP-DatabaseLog/compare/2.3.4...2.4.0

    Improvements

    Compatibility for CakePHP 3.6+. Enable PHPStan check. Raise min. CakePHP support to 3.5+.

    Source code(tar.gz)
    Source code(zip)
  • 2.3.4(Feb 11, 2018)

  • 2.3.3(Jan 3, 2018)

  • 2.3.2(Dec 22, 2017)

  • 2.3.1(Aug 23, 2017)

  • 2.3.0(Aug 16, 2017)

  • 2.2.0(Jan 8, 2017)

    The error types are now colorful labels to make it easier to spot the critical ones:

    • error: red
    • warning/notice: yellow
    • ...

    Also added:

    • CLI show to list all recent log entries
    • CLI export to export the DB driven entries into a txt file
    • CLI monitor to have a basic alerting tool for (critical) errors via email, SMS, ...
    Source code(tar.gz)
    Source code(zip)
  • 2.1.0(Oct 9, 2016)

  • 2.0.0(Sep 28, 2016)

  • 1.0(Dec 2, 2014)

    With the great help of @ravage84 this plugin seems to be now in perfect shape to be released as tagged version. No need to use the dev-master branch anymore.

    Source code(tar.gz)
    Source code(zip)
Owner
Mark Sch.
Senior Software Developer
Mark Sch.
CakePHP plugin to allow passing currently logged in user to model layer.

Footprint This plugin allows you to pass the currently logged in user info to the model layer of a CakePHP application. It comes bundled with the Foot

Muffin 88 Nov 14, 2022
AcLog is a simple, zero-dependency PHP package to log activity to files

AcLog is a simple, zero-dependency PHP package to log activity to files. This is not meant for logging errors, this is can be used for logging c

null 2 Sep 9, 2022
Open source medical record system on CakePHP (OMCAKE)

omcake v0.1 小さな診療所用の電子カルテ(もどき)です。 奥村晴彦先生のtwitter オープンソースの電子カルテシステムで、WebベースでクライアントOSを選ばず、 サーバは普通のLinuxで動くPHPとか、ないんだろうか。 で唐突に召喚され、それ、ウチにありますけど〜、とノコノコ出てき

古林 敬一 7 Aug 16, 2022
High-performance API performing logging for PHP applications into files or SysLog

Logging API Table of contents: About Configuration Binding Points Logging Installation Unit Tests Reference Guide Specifications How Are Log Lines For

Lucian Gabriel Popescu 0 Jan 9, 2022
Log Laravel application request and response with a unique ID for easy debugging

Flexible and extendable logging of Laravel application request and responses Zero configuration logging of Requests and Responses to database or custo

Bilfeldt 37 Nov 6, 2022
A simple and beautiful laravel log reader

Laravel Log Reader A simple and beautiful laravel log reader Documentation Get full documentation of Laravel Log Reader Other Packages Laravel H - A h

Md.Harun-Ur-Rashid 356 Dec 30, 2022
Remove messages from the error log that are from a certain folder

Error-Focus A package to stay focused on relevant entries in your error-log Scope This package allows you to declare folders that are not under your d

Andreas Heigl 7 Jul 25, 2022
An effective,fast,stable log extension for PHP

SeasLog An effective,fast,stable log extension for PHP @author Chitao.Gao [[email protected]] Documentation On php.net 中文文档 Synopsis Why use seaslog What

Neeke Gao 76 Sep 28, 2022
This package is for centralized logging of multiple Laravel application into one connection.

Laralogs This package is for centralized logging of multiple Laravel application into one connection. Installation You can install the package via com

Remon 4 Apr 26, 2022
Sends your logs to files, sockets, inboxes, databases and various web services

Monolog - Logging for PHP Monolog sends your logs to files, sockets, inboxes, databases and various web services. See the complete list of handlers be

Jordi Boggiano 20.1k Jan 8, 2023
Sends your logs to files, sockets, inboxes, databases and various web services

Monolog - Logging for PHP ⚠ This is the documentation for Monolog 3.x, if you are using older releases see the documentation for Monolog 2.x or Monolo

Jordi Boggiano 20.1k Jan 7, 2023
123Solar is a set of PHP/JS files that make a web logger to monitor your photovoltaic inverter(s)

123Solar is a set of PHP/JS files that make a web logger to monitor your photovoltaic inverter(s). It just need a web server and PHP, no databases are even needed. The philosophy is: To keep it simple, fast, with a low foot print to run on cheap and low powered devices.

null 30 Jan 6, 2023
CakePHP3: plugin that facilitates versioned database entities

Version A CakePHP 4.x plugin that facilitates versioned database entities Installation Add the following lines to your application's composer.json: "r

Jose Diaz-Gonzalez 52 Sep 3, 2022
Capture and monitor detailed error logs with nice dashboard and UI

Capture and monitor detailed error logs with nice dashboard and UI Requirements Check Laravel 6 requirements Check Laravel 7 requirements Installation

Bugphix 107 Dec 12, 2022
PHP logging library that is highly extendable and simple to use.

Analog - Minimal PHP logging library Copyright: (c) 2012-Present Johnny Broadway License: MIT A minimal PHP logging package based on the idea of using

Aband*nthecar 331 Dec 21, 2022
Paste, share and analyse Minecraft server logs

mclo.gs Paste, share & analyse your Minecraft server logs About The project mclo.gs was created in 2017 by the Aternos team after more than 4 years of

Aternos 99 Jan 3, 2023
Keep your laravel logs small and tidy.

Logs can get quite out of hand. This package helps save server space and keep your Laravel log files small.

Accent Interactive 73 Nov 14, 2022
Robust, composite logger with filtering, formatting, and PSR-3 support

laminas-log ???? Русским гражданам Мы, участники Laminas, родились и живем в разных странах. У многих из нас есть друзья, родственники и коллеги как в

Laminas Project 27 Nov 24, 2022
[READ-ONLY] CakePHP Utility classes such as Inflector, Text, Hash, Security and Xml. This repo is a split of the main code that can be found in https://github.com/cakephp/cakephp

CakePHP Utility Classes This library provides a range of utility classes that are used throughout the CakePHP framework What's in the toolbox? Hash A

CakePHP 112 Feb 15, 2022
[READ-ONLY] Collection library in CakePHP. This repo is a split of the main code that can be found in https://github.com/cakephp/cakephp

CakePHP Collection Library The collection classes provide a set of tools to manipulate arrays or Traversable objects. If you have ever used underscore

CakePHP 85 Nov 28, 2022