logistcloud/logging-bundle

logist.cloud LoggingBundle

安装: 29

依赖: 0

建议者: 0

安全性: 0

类型:symfony-bundle

dev-default 2017-12-05 13:11 UTC

This package is auto-updated.

Last update: 2019-08-08 03:11:16 UTC


README

添加到您的 app/config/parameters.yml.dist

parameters:
    logging.config_dir: 'Resources/config'

Composer

运行命令 $ php composer.phar require logistcloud/logging-bundle:dev-default。Composer 将安装包到 logistcloud/logging-bundle 目录。

添加到您的 app/config/config.yml

logging:
    types:
        error: '%logging.config_dir%/error.yml'
        exception: '%logging.config_dir%/exception.yml'
        low_error: '%logging.config_dir%/low_error.yml'
        denied: '%logging.config_dir%/denied.yml'
        not_found: '%logging.config_dir%/not_found.yml'
        high_error: '%logging.config_dir%/high_error.yml'
        failed: '%logging.config_dir%/failed.yml'
        auth: '%logging.config_dir%/auth.yml'
        granted: '%logging.config_dir%/granted.yml'
        file: '%logging.config_dir%/file.yml'
        entity: '%logging.config_dir%/entity.yml'
        controller: '%logging.config_dir%/controller.yml'
        event: '%logging.config_dir%/event.yml'
        actions: '%logging.config_dir%/actions.yml'

添加到您的 app/AppKernel.php

<?php
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Config\Loader\LoaderInterface;

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            //...
            new LoggingBundle\LoggingBundle()
        );

        return $bundles;
    }

//...
}
?>

将迁移文件放在 app/DoctrineMigrations 目录中,并运行: php app/console doctrine:migrations:migrate 20171201110753

使用

使用示例

预定义的日志实体位于 Entity 目录中。它们必须继承自 EntityBase 类。

在您的控制器中

<?php
#...
use LoggingBundle\Entity\Log;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
#...
/**
 * @Route("/", name="homepage")
 * @return Response
 */
public function indexAction()
{
    $logger = $this->get('logist.cloud.logger'); // Get the logging service
    $logger->log(
        [new Log()], // Adding entities
        [[
            'userId' => 1,
            'orderId' => 1,
            'message' => 'test test', // required
            'tenderId' => 1,
            'channel' => 1,
            'level' => $logger::$ERROR // required
        ]], // Value of the fields to be logged
        [], // Related entities, if necessary
        [['test.png']], // Related files, if necessary
        true // flag for use Monolog
    ); // Logging of a record
    $logs = $logger->get(Log::class); // Getting a list of database logs with any level
    $logs = $logger->get(Log::class, ['level' => $logger::$ERROR]); // Getting a list of database logs with a level equal to ERROR.
    // Equivalently $logs = $logger->getErrors(Log::class);
    $logs = $logger->get(Log::class, ['level' => $logger::$ERROR], ['id' => 'desc'], 0, 10); // Getting a list of database logs with a level equal to ERROR + sorting, offset and limit
    $logs = $logger->get(Log::class, $logger::$ERROR | $logger::$AUTH); // Getting a list of database logs with a level equal to ERROR and AUTH.
    echo '<pre>';
    return new Response(print_r($logs, true));
}
#...
?>

日志级别 "Error" 的配置示例

添加到您的 vendor/logistcloud/logging-bundle/Resources/config/error.yml

default:
    size: 1048576 # maximum size of the log file
    datetime: 'd.m.Y H:i:s' # date format
    output: '[%%datetime%%] %%channel%%.%%level_name%%: %%message%%' # %%context%% %%extra%% # format for a message
    related_files: ' files: %s' # related files
    related_entities: ' entities: %s' # related entities
    log:
        path: '' # path to log files
        filename: '' # name of the log file
    json:
        path: '' # path to json files
        filename: '' # name of the json file
    db:
        rows_per_table: 1000 # number of rows in the table partition
error:
    log: true # logging to the log file
    json: true # logging to the json file
    db: true # database logging

日志操作配置示例

添加到您的 vendor/logistcloud/logging-bundle/Resources/config/actions.yml

default:
    size: 1024
    datetime: 'd.m.Y H:i:s'
    output: '[%%datetime%%] %%channel%%.%%level_name%%: %%message%%' # %%context%% %%extra%%
    message: 'User %s went to route %s'
    related_files: ' files: %s'
    related_entities: ' entities: %s'
    log:
        path: ''
        filename: ''
    json:
        path: ''
        filename: ''
    db:
        rows_per_table: 10
actions:
    LoggingBundle\Controller\DefaultController::indexAction:
        action:
            log: true
            json: true
            db: true
        message: 'User %s viewed the home page'