small/logger-bundle

Symfony logger

22.0.0 2023-04-14 11:39 UTC

This package is auto-updated.

Last update: 2024-05-14 14:15:13 UTC


README

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/'

  Sebk\SmallLogger\Contracts\SwitchLogicInterface:
    class: Sebk\SmallLogger\SwitchLogic\DefaultSwitchLogic

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

  • Sebk\SmallLogger\SwitchLogic\DefaultSwitchLogic -> log BasicLog to standard ouput
  • Sebk\SmallLogger\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 Sebk\SmallLogger\Contracts\LogInterface;
use Sebk\SmallLogger\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 !");
    }

}