LoginSignup
1
0

More than 5 years have passed since last update.

Symfony 2.x で static な logger を用意する

Posted at

標準の logger は service container に登録されているので、毎回コンテナから取得する手間があったり、ちょっとしたユーティリティクラス等で利用しにくいので、AppBundle 配下で static に利用できるようにします。

Loggerを用意

まずは static な logging メソッドを持つクラスを用意。

ついでに、ログレベルの判定で、利用クラス側で Monolog\Logger を参照させないために、isHandling() もラップしておきます。

AppBundle/DependencyInjection/Logging/AppLogger.php

<?php

namespace AppBundle\DependencyInjection\Logging;

use Psr\Log\LoggerInterface;
use Monolog\Logger;

class AppLogger {

    private static $logger;

    private function __construct() {
    }

    // ここは Monolog\Logger で良いが、なんとなく…
    public static function configure(LoggerInterface $logger) {
        self::$logger = $logger;
    }

    public static function getLogger() {
        return self::$logger;
    }

    public static function isDebugEnabled() {
        return self::$logger->isHandling(Logger::DEBUG);
    }

    public static function isInfoEnabled() {
        return self::$logger->isHandling(Logger::INFO);
    }

    public static function isWarnEnabled() {
        return self::$logger->isHandling(Logger::WARN);
    }

    public static function isErrorEnabled() {
        return self::$logger->isHandling(Logger::ERROR);
    }

    public static function isAlertEnabled() {
        return self::$logger->isHandling(Logger::ALERT);
    }

    public static function debug($message, array $context = array()) {
        return self::$logger->debug($message, $context);
    }

    public static function info($message, array $context = array()) {
        return self::$logger->info($message, $context);
    }

    public static function warn($message, $ex = null) {
        $context = self::assignException($ex);
        return self::$logger->warning($message, $context);
    }

    public static function error($message, $ex = null) {
        $context = self::assignException($ex);
        return self::$logger->error($message, $context);
    }

    public static function alert($message, $ex = null) {
        $context = self::assignException($ex);
        return self::$logger->alert($message, $context);
    }

    private static function assignException($exception, $context = array()) {
        if ($exception !== null && $exception instanceof \Exception) {
            $context['exception'] = $exception;
        }
        return $context;
    }
}

AppBundle で初期化

AppBundle でラップする logger を設定します。
これで AppLogger::info('info'); のように利用できるようになり、サービスコンテナから取得する手間が省けます。

AppBundle\AppBundle.php
<?php

namespace AppBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;
use AppBundle\DependencyInjection\Logging\AppLogger;

class AppBundle extends Bundle {

    public function boot() {
        parent::boot();
        // 標準のロガーを利用する場合
        AppLogger::configure($this->container->get('logger'));
    }
}


1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0