Skip to content

Repository files navigation

Logs Bundle

Package versionTotal DownloadsBuild StatusScrutinizer CoverageScrutinizer Code QualityLicense

SensioLabsInsight

This project is a bundle to write logs in database and read logs with browser

Installation

Step 1: Download the Bundle

Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:

$ composer require norsys/logs-bundle "dev-master"

This command requires you to have Composer installed globally, as explained in the installation chapter of the Composer documentation.

Step 2: Enable the Bundle

Then, enable the bundle by adding it to the list of registered bundles in the app/AppKernel.php file of your project:

<?php// app/AppKernel.php// ...class AppKernel extends Kernel
{
publicfunctionregisterBundles()
{
$bundles = array(
// ...newKnp\Bundle\PaginatorBundle\KnpPaginatorBundle(),
newNorsys\LogsBundle\NorsysLogsBundle(),
// ...
);
// ...
}
// ...
}

Step 3: Configuration

First of all, you need to configure the Doctrine DBAL connection to use in the handler. You have 2 ways to do that:

By using an existing Doctrine connection:

Note: we set the logging and profiling option to false to avoid DI circular reference.

# app/config/config.ymldoctrine:
dbal:
connections:
default:
...monolog:
driver: pdo_sqlitedbname: monologpath: %kernel.root_dir%/cache/monolog2.dbcharset: UTF8logging: falseprofiling: falsenorsys_logs:
doctrine:
connection_name: monolog

By creating a custom Doctrine connection for the bundle:

# app/config/config.yml
norsys_logs:
doctrine:
connection:
driver: pdo_sqlite
driverClass: ~
pdo: ~
dbname: monolog
host: localhost
port: ~
user: root
password: ~
charset: UTF8
path: %kernel.root_dir%/db/monolog.db # The filesystem path to the database file for SQLite
memory: ~ # True if the SQLite database should be in-memory (non-persistent)
unix_socket: ~ # The unix socket to use for MySQL

Please refer to the Doctrine DBAL connection configuration for more details.

Optionally you can override the schema table name (monolog_entries by default):

# app/config/config.ymlnorsys_logs:
doctrine:
table_name: monolog_entries

Now your database is configured, you can generate the schema for your log entry table by running the following command:

./app/console norsys:logs:schema-create
# you should see as result:
# Created table monolog_entries for Doctrine Monolog connection

Then, you can configure Monolog to use the Doctrine DBAL handler:

# app/config/config_prod.yml # or any envmonolog:
handlers:
main:
type: fingers_crossed # or bufferlevel: errorhandler: norsys_logsapp:
type: bufferaction_level: infochannels: apphandler: norsys_logsdeprecation:
type: bufferaction_level: warningchannels: deprecationhandler: norsys_logsnorsys_logs:
type: serviceid: norsys_logs.handler.doctrine_dbal

Now you have enabled and configured the handler, you migth want to display log entries, just import the routing file:

# app/config/routing.ymlnorsys_logs:
resource: "@NorsysLogsBundle/Resources/config/routing.xml"prefix: /admin/monolog

Translations

If you wish to use default translations provided in this bundle, make sure you have enabled the translator in your config:

# app/config/config.ymlframework:
translator: ~

Overriding default layout

You can override the default layout of the bundle by using the base_layout option:

# app/config/config.ymlnorsys_logs:
base_layout: "NorsysLogsBundle::layout.html.twig"

or quite simply with the Symfony way by create a template on app/Resources/NorsysLogsBundle/views/layout.html.twig.

Using service tags to implement logger

This bundle comes up with a compiler pass helper, easing integration of the logger by using service tags when combined with LoggerAwareTrait.

First, use trait in your class:

<?php# src/AppBundle/Acme/Demo.phpnamespaceAppBundle\Acme;
// ...useNorsys\LogsBundle\LoggerBehaviorTrait;
class Demo
{
// ...use LoggerBehaviorTrait;
// ...
}

Then update the class setup in your service container config:

# app/config/services.ymlservices:
# ...app.acme.demo:
class: AppBundle\Acme\Demotags:
- { name: logger.aware }# ...

That's it, now your logger is ready to use!:

<?php# src/AppBundle/Acme/Demo.phpnamespaceAppBundle\Acme;
// ...useNorsys\LogsBundle\LoggerBehaviorTrait;
class Demo
{
// ...use LoggerBehaviorTrait;
// ...publicfunctiondoSomething()
{
$this->getLogger()->debug('Method AppBundle\Acme::doSomething() was called');
// ...
}
}

Credits

Developped with ❤️ by Norsys

License

This project is licensed under the MIT license.

About

Bundle to manage logs in symfony application

Resources

Stars

2 stars

Watchers

8 watching

Forks

Releases

Packages

Used by

Contributors

Languages