Giter VIP home page Giter VIP logo

cakephp-databaselog's Introduction

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.

cakephp-databaselog's People

Contributors

anvyst avatar dereuromark avatar georgeconstantinou avatar ishanvyas22 avatar liviakuenzli avatar mamchenkov avatar ravage84 avatar slamkajs avatar webtechnick avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

cakephp-databaselog's Issues

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.

Cake 4.3 Routing deprecation

This should be fixed by changing config/routes.php, and requiring cake 4.3

diff --git a/config/routes.php b/config/routes.php
index 6ed7cdd..0cd2bce 100644
--- a/config/routes.php
+++ b/config/routes.php
@@ -3,7 +3,8 @@
 use Cake\Routing\RouteBuilder;
 use Cake\Routing\Router;
 
-Router::prefix('Admin', function (RouteBuilder $routes) {
+/** @var \Cake\Routing\RouteBuilder $routes */
+$routes->prefix('Admin', function (RouteBuilder $routes) {

I tried to make a PR but couldn't get test to run with new Test Migrator

Missing route

Thank you for your work.

I downloaded your plugin via composer and installed it completely. I've loaded the plugin with routes => true and managed to load 'database-log/logs'. However when I try to access 'Details' or 'Delete' I get the following:

Error: A route matching "array ( 'controller' => 'Users', 'action' => 'login', 'plugin' => NULL, 'prefix' => 'admin', '_ext' => NULL, )" could not be found.

I get the above when I try to access: admin/database-log/logs/view/37

Seems to be an issue with Auth. I managed to solve it by adding the following in the LogsController of your plugin;
public function beforeFilter(Event $event) {
if ($this->Auth->User('role_id') == 1)
$this->Auth->allow(['logs', 'index', 'view', 'delete']);
}

However I'm not sure if this is the right way to handle it.

Running cakephp v 3.3.16

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

Three debug messages are produced by the plugin tests

When I execute all the plugin tests, I get three unwanted, by the test cases produced, debug messages:

D:\dev\xampp\htdocs\cakephp_app\app>console\cake.bat test DatabaseLog AllDatabaseLog  - -stderr


Welcome to CakePHP v2.5.6 Console
---------------------------------------------------------------
App : app
Path: D:\dev\xampp\htdocs\cakephp_app\app\
---------------------------------------------------------------
CakePHP Test Shell
---------------------------------------------------------------
PHPUnit 3.7.38 by Sebastian Bergmann.

2014-12-02 23:38:36 Error: x
2014-12-02 23:38:36 Warning: y
2014-12-02 23:38:36 Info: z
............

Time: 2.52 seconds, Memory: 10.00Mb

OK (12 tests, 18 assertions)

Those messages are produced by the following lines, because the objects
aren't mocked:
https://github.com/dereuromark/CakePHP-DatabaseLog/blob/1.0/Test/Case/Lib/Log/Engine/DatabaseLogTest.php#L58
https://github.com/dereuromark/CakePHP-DatabaseLog/blob/1.0/Test/Case/Lib/Log/Engine/DatabaseLogTest.php#L59
https://github.com/dereuromark/CakePHP-DatabaseLog/blob/1.0/Test/Case/Lib/Log/Engine/DatabaseLogTest.php#L60

The solution is to mock the following objects:
https://github.com/dereuromark/CakePHP-DatabaseLog/blob/1.0/Test/Case/Lib/Log/Engine/DatabaseLogTest.php#L35
https://github.com/dereuromark/CakePHP-DatabaseLog/blob/1.0/Test/Case/Lib/Log/Engine/DatabaseLogTest.php#L54

And not use the static calls to CakeLog::write().
Which means we can remove the attached test class here:
https://github.com/dereuromark/CakePHP-DatabaseLog/blob/1.0/Test/Case/Lib/Log/Engine/DatabaseLogTest.php#L71

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?

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'
]
],

New release

Hi,

Is it possible to bring out a new release?
Thank you in advance.

Cheers,

Frank

Add summary field for paginated filter backend

When displaying >> 10000 messages, the message mediumtext is slowing things down for pagination.
We must remove that field here for select() and instead use e.g. summary field as varchar 255 summary field here to display the essence of this log entry.

prio: high

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?

Building a dashboard

DatabaseLog controller

=> /admin/database-log

Containing count of all types and maybe important excerpts of the errors etc.

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. :)

Add monitor shell command

Add a shell command to monitor incoming logs.

Configured can be what type of logs (error, ...) trigger an email to the developer for example or do other alerts of some sort.
A timeout config "interval" (e.g. 2 hours) should prevent too many error mails being generated.

Anything missing?

Bad Gateway

I think I may go crazy - but just slightly.

So, I've loaded the plugin, setup the CakeLog config and I'm trying rather desperately to get this to work.

CakePlugin::load('DatabaseLog');
App::uses('CakeLog', 'Log');
CakeLog::config('default', array('engine' => 'DatabaseLog.Database'));

And yet, any request to my app results in a 502 gateway.

I know you'll ask the "is this guy being an idiot questions". So, as a process of elimination:

  • everything with my nginx config is fine (I think)
  • commenting the line CakeLog::config('default', array('engine' => 'DatabaseLog.Database')); results in my app working 100% again
  • There is no error output in the nginx error log other than
2015/03/12 21:48:55 [error] 70781#0: *258 upstream prematurely closed connection while reading response header from upstream, client: 127.0.0.1, server: dev.katalog.co.za, request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "XXXXXX.co.za"
  • Also, yep, I did configure APP/Config/database_log.php

My CakePHP version is 2.6.2

Aaaaany idea what might be causing it?

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?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.