2015-09-13 27 views
31

Jak określić osobny plik dla logging INFO w Laravel 5.1?laravel: Jak się zalogować do osobnego pliku INFO

wszelkiej natychmiastowej pomocy będzie bardzo odczuwalny. Dzięki

+0

Możliwy duplikat [laravel Monolog. Zaloguj poziom INFO, aby oddzielić plik] (http://stackoverflow.com/questions/32286010/laravel-monolog-log-info-level-to-separate-file) –

Odpowiedz

27

Chcesz specjalnie zalogować info do jednego pliku dziennika i innego rodzaju dziennika do drugiego Lokalizacja? Moje rozwiązanie może nie pomóc w tym przypadku, ale może być przydatne.

Aby zapisać plik dziennika w innej lokalizacji, użyj metody useDailyFiles lub useFiles, a następnie informacji, aby zalogować się do pliku dziennika w podanej ścieżce. Tak:

Log::useDailyFiles(storage_path().'/logs/name-of-log.log'); 
    Log::info([info to log]); 

Pierwszy parametr dla obu metod jest ścieżka do pliku logu (który jest tworzony, jeśli jeszcze nie istnieje) i useDailyFiles drugim argumentem jest liczba dni laravel zarejestruje dla przed skasowaniem starych logów. Domyślna wartość jest nieograniczona, więc w moim przykładzie nie wprowadziłem wartości.

+6

To wciąż działa w Laravel 5.4, właśnie testowałem i używałem tego. – user2094178

+0

działa dobrze na starej wersji laravel 4.2. –

+1

Dla problemów/zastrzeżeń z tym podejściem zobacz https://stackoverflow.com/a/32262707/96944 –

10

Jeśli chcesz dodać kolejną obsługi monolog, można użyć metody configureMonologUsing aplikacji.

Place wywołanie tej metody w pliku bootstrap/app.php tuż przed zwracana jest zmienna $ app:

$app->configureMonologUsing(function($monolog) { 
    $monolog->pushHandler(new StreamHandler('path/to/info.log', Logger::INFO, false)); // false value as third argument to disable bubbling up the stack 
}); 

return $app; 
+0

Więcej informacji tutaj -> http://laravel.com/ docs/5.1/errors –

+0

ten błąd 'PHP Błąd krytyczny: Klasa 'StreamHandler' nie została znaleziona w /var/www/html/project/bootstrap/app.php na linii 56' –

+4

@RohitJindal zamień' StreamHandler' out dla 'Monolog \ Handler \ StreamHandler'. Możesz również znaleźć opcję "Logger" dla 'Monolog \ Logger'. – alexrussell

2

Prosty rejestrator pomocnik, który pozwala zalogować się do wielu plików niestandardowych w locie. Możesz również dodać niestandardową procedurę obsługi i ustawić ścieżkę pliku.

App \ Helper \ LogToChannels.php

<?php 
/** 
* Logger helper to log into different files 
* 
* @package App\Helpers 
* @author  Romain Laneuville <[email protected]> 
*/ 

namespace App\Helpers; 

use Monolog\Logger; 
use Monolog\Handler\HandlerInterface; 
use Monolog\Handler\StreamHandler; 
use Monolog\Formatter\LineFormatter; 

/** 
* Class LogToChannels 
* 
* @package App\Helpers 
*/ 
class LogToChannels 
{ 
    /** 
    * The LogToChannels channels. 
    * 
    * @var Logger[] 
    */ 
    protected $channels = []; 

    /** 
    * LogToChannels constructor. 
    */ 
    public function __construct() 
    { 
    } 

    /** 
    * @param string $channel The channel to log the record in 
    * @param int $level The error level 
    * @param string $message The error message 
    * @param array $context Optional context arguments 
    * 
    * @return bool Whether the record has been processed 
    */ 
    public function log(string $channel, int $level, string $message, array $context = []): bool 
    { 
     // Add the logger if it doesn't exist 
     if (!isset($this->channels[$channel])) { 
      $handler = new StreamHandler(
       storage_path() . DIRECTORY_SEPARATOR . 'logs' . DIRECTORY_SEPARATOR . $channel . '.log' 
      ); 

      $handler->setFormatter(new LineFormatter(null, null, true, true)); 

      $this->addChannel($channel, $handler); 
     } 

     // LogToChannels the record 
     return $this->channels[$channel]->{Logger::getLevelName($level)}($message, $context); 
    } 

    /** 
    * Add a channel to log in 
    * 
    * @param string   $channelName The channel name 
    * @param HandlerInterface $handler  The channel handler 
    * @param string|null  $path  The path of the channel file, DEFAULT storage_path()/logs 
    * 
    * @throws \Exception When the channel already exists 
    */ 
    public function addChannel(string $channelName, HandlerInterface $handler, string $path = null) 
    { 
     if (isset($this->channels[$channelName])) { 
      throw new \Exception('This channel already exists'); 
     } 

     $this->channels[$channelName] = new Logger($channelName); 
     $this->channels[$channelName]->pushHandler(
      new $handler(
       $path === null ? 
        storage_path() . DIRECTORY_SEPARATOR . 'logs' . DIRECTORY_SEPARATOR . $channelName . '.log' : 
        $path . DIRECTORY_SEPARATOR . $channelName . '.log' 
      ) 
     ); 
    } 

    /** 
    * Adds a log record at the DEBUG level. 
    * 
    * @param string $channel The channel name 
    * @param string $message The log message 
    * @param array $context The log context 
    * 
    * @return bool Whether the record has been processed 
    */ 
    public function debug(string $channel, string $message, array $context = []): bool 
    { 
     return $this->log($channel, Logger::DEBUG, $message, $context); 
    } 

    /** 
    * Adds a log record at the INFO level. 
    * 
    * @param string $channel The channel name 
    * @param string $message The log message 
    * @param array $context The log context 
    * 
    * @return bool Whether the record has been processed 
    */ 
    public function info(string $channel, string $message, array $context = []): bool 
    { 
     return $this->log($channel, Logger::INFO, $message, $context); 
    } 

    /** 
    * Adds a log record at the NOTICE level. 
    * 
    * @param string $channel The channel name 
    * @param string $message The log message 
    * @param array $context The log context 
    * 
    * @return bool Whether the record has been processed 
    */ 
    public function notice(string $channel, string $message, array $context = []): bool 
    { 
     return $this->log($channel, Logger::NOTICE, $message, $context); 
    } 

    /** 
    * Adds a log record at the WARNING level. 
    * 
    * @param string $channel The channel name 
    * @param string $message The log message 
    * @param array $context The log context 
    * 
    * @return bool Whether the record has been processed 
    */ 
    public function warn(string $channel, string $message, array $context = []): bool 
    { 
     return $this->log($channel, Logger::WARNING, $message, $context); 
    } 

    /** 
    * Adds a log record at the WARNING level. 
    * 
    * @param string $channel The channel name 
    * @param string $message The log message 
    * @param array $context The log context 
    * 
    * @return bool Whether the record has been processed 
    */ 
    public function warning(string $channel, string $message, array $context = []): bool 
    { 
     return $this->log($channel, Logger::WARNING, $message, $context); 
    } 

    /** 
    * Adds a log record at the ERROR level. 
    * 
    * @param string $channel The channel name 
    * @param string $message The log message 
    * @param array $context The log context 
    * 
    * @return bool Whether the record has been processed 
    */ 
    public function err(string $channel, string $message, array $context = []): bool 
    { 
     return $this->log($channel, Logger::ERROR, $message, $context); 
    } 

    /** 
    * Adds a log record at the ERROR level. 
    * 
    * @param string $channel The channel name 
    * @param string $message The log message 
    * @param array $context The log context 
    * 
    * @return bool Whether the record has been processed 
    */ 
    public function error(string $channel, string $message, array $context = []): bool 
    { 
     return $this->log($channel, Logger::ERROR, $message, $context); 
    } 

    /** 
    * Adds a log record at the CRITICAL level. 
    * 
    * @param string $channel The channel name 
    * @param string $message The log message 
    * @param array $context The log context 
    * 
    * @return bool Whether the record has been processed 
    */ 
    public function crit(string $channel, string $message, array $context = []): bool 
    { 
     return $this->log($channel, Logger::CRITICAL, $message, $context); 
    } 

    /** 
    * Adds a log record at the CRITICAL level. 
    * 
    * @param string $channel The channel name 
    * @param string $message The log message 
    * @param array $context The log context 
    * 
    * @return Boolean Whether the record has been processed 
    */ 
    public function critical(string $channel, string $message, array $context = []): bool 
    { 
     return $this->log($channel, Logger::CRITICAL, $message, $context); 
    } 

    /** 
    * Adds a log record at the ALERT level. 
    * 
    * @param string $channel The channel name 
    * @param string $message The log message 
    * @param array $context The log context 
    * 
    * @return bool Whether the record has been processed 
    */ 
    public function alert(string $channel, string $message, array $context = []): bool 
    { 
     return $this->log($channel, Logger::ALERT, $message, $context); 
    } 

    /** 
    * Adds a log record at the EMERGENCY level. 
    * 
    * @param string $channel The channel name 
    * @param string $message The log message 
    * @param array $context The log context 
    * 
    * @return bool Whether the record has been processed 
    */ 
    public function emerg(string $channel, string $message, array $context = []): bool 
    { 
     return $this->log($channel, Logger::EMERGENCY, $message, $context); 
    } 

    /** 
    * Adds a log record at the EMERGENCY level. 
    * 
    * @param string $channel The channel name 
    * @param string $message The log message 
    * @param array $context The log context 
    * 
    * @return bool Whether the record has been processed 
    */ 
    public function emergency(string $channel, string $message, array $context = []): bool 
    { 
     return $this->log($channel, Logger::EMERGENCY, $message, $context); 
    } 
} 

App \ Providers \ LogToChannelsServiceProvider.php

<?php 
/** 
* Logger service provider to be abled to log in different files 
* 
* @package App\Providers 
* @author  Romain Laneuville <[email protected]> 
*/ 

namespace App\Providers; 

use Illuminate\Support\ServiceProvider; 
use App\Helpers\LogToChannels; 

/** 
* Class LogToChannelsServiceProvider 
* 
* @package App\Providers 
*/ 
class LogToChannelsServiceProvider extends ServiceProvider 
{ 
    /** 
    * Initialize the logger 
    * 
    * @return void 
    */ 
    public function register() 
    { 
     $this->app->singleton('App\Helpers\LogToChannels', function() { 
      return new LogToChannels(); 
     }); 
    } 
} 

config \ app.php (dodaj usługodawcy)

// Register Service Providers 
$app->register(App\Providers\LogToChannelsServiceProvider::class); 

Następnie gdziekolwiek w aplikacji można wywołać przy użyciu iniekcji zależność (dodaj klasę w konstruktorze i powiązać go z atrybutem log klasa)

$this->log->info('logger_name', 'Log message'); 
$this->log->error('other_logger_name', 'Log message', $someContext); 

Można nawet dostosować swoje wyjście rejestratora poprzez wywołanie

$this->log->addChannel('channel_name', $customHandler); 

I to będzie dostępne, gdy będziesz nigdzie dzwonić jej nazwę w swojej aplikacji.

Powiązane problemy