small/logger-bundle

Symfony logger

22.1.0 2024-09-09 02:58 UTC

This package is auto-updated.

Last update: 2024-12-09 03:34:16 UTC


README

small-logger-bundle

Symfony small logger

In reality, it is only a bridge between symfony and small-logger.

Additionnaly, this package implement a symfony native http client for http output.

Install

Use composer to install the package in your symfony project :

$ composer require small/logger-bundle

Configure

First define service for SwitchLogicInterface :

# config/service.yaml
services:
  _defaults:
    autowire: true      # Automatically injects dependencies in your services.
    autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.

  App\:
    resource: '../src/'
    exclude:
      - '../src/DependencyInjection/'
      - '../src/Entity/'
      - '../src/Kernel.php'
      - '../src/Tests/'

  Small\Logger\Contracts\SwitchLogicInterface:
    class: Small\Logger\SwitchLogic\DefaultSwitchLogic

There is two default switches available in small-logger package :

  • Small\Logger\SwitchLogic\DefaultSwitchLogic -> log BasicLog to standard ouput
  • Small\Logger\SwitchLogic\CommonLogSwitchLoggic -> log CommonLog to file

See small-logger documentation to create your own switches, streams, formatters or logs

Create shortcuts

Shortcuts are callback that simplify writing of logs by developer.

You are required to declare them in the contructor of a class. For example :

namespace App\Log;

use Small\Logger\Contracts\LogInterface;
use Small\Logger\Log\BasicLog;
use Small\LoggerBundle\Service\Logger;

class Shortcuts
{
    public function __construct(Logger $logger)
    {
        $logger->registerShortcut('info', function(Logger $logger, $message) {
            $logger->log(new BasicLog(new \DateTime(), LogInterface::ERR_LEVEL_INFO, $message));
        });
    }
}

And declare the Shortcuts class in config :

# config/packages/small_logger.yaml
small_logger:
  shortcuts:
    - App\Log\Shortcuts

After declaring callback, just call it via logger :

<?php

namespace App\Controller;

use Small\LoggerBundle\Service\Logger;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class TestLog extends AbstractController
{

    /**
     * @Route("/log")
     * @param Dao $daoFactory
     * @param Logger $logger
     * @return Response
     */
    public function logAction(Logger $logger)
    {
        $logger->info('This is a message');
        
        return new Response("That's done !");
    }

}