Code Monkey home page Code Monkey logo

cakephp-databaselog's Introduction

CakePHP DatabaseLog Plugin

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

DatabaseLog engine for CakePHP applications.

This branch is for CakePHP 3.5+

Features

  • Easy setup and almost no dependencies.
  • Defaults to SQLite as single app application light weight approach.
  • Ideal for multi app 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 config/bootstrap.php:

Plugin::load('DatabaseLog', ['bootstrap' => true]);

or just call:

bin/cake plugin load DatabaseLog -b

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.

cakephp-databaselog's People

Contributors

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

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.