2015-09-13 6 views
31

Laravel 5.1 में logging INFO के लिए एक अलग फ़ाइल कैसे निर्दिष्ट करें?लैरवेल: फ़ाइल को अलग करने के लिए INFO लॉग इन करें

कोई तत्काल सहायता अत्यधिक सराहनीय होगी। धन्यवाद

+0

[लैरवेल मोनोलॉग का संभावित डुप्लिकेट। फ़ाइल को अलग करने के लिए लॉग इन करें स्तर] (http://stackoverflow.com/questions/32286010/laravel-monolog-log-info-level-to-separate-file) –

उत्तर

27

आप विशेष रूप से प्रवेश करने के लिए info करने के लिए एक लॉग फ़ाइल और एक अन्य के लिए एक और लॉग प्रकार चाहते हैं स्थान? मेरा समाधान उस मामले में मदद नहीं कर सकता है, लेकिन अभी भी उपयोगी हो सकता है।

किसी अन्य स्थान पर लॉग फ़ाइल लिखने के लिए, विधि useDailyFiles या useFiles विधि का उपयोग करें, और फिर आपके द्वारा निर्दिष्ट पथ पर लॉग फ़ाइल पर लॉग इन करने के लिए जानकारी का उपयोग करें। इसलिए जैसा:

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

दोनों तरीकों के लिए पहले पैरामीटर लॉग फ़ाइल का पथ useDailyFiles के लिए दूसरा तर्क दिनों Laravel के लिए प्रवेश करेंगे की संख्या है (जो कि वह पहले से मौजूद नहीं है बनाई गई है) और है पुराने लॉग मिटाने से पहले। डिफ़ॉल्ट मान असीमित है, इसलिए मेरे उदाहरण में मैंने कोई मान दर्ज नहीं किया है।

+6

यह अभी भी लैरवेल 5.4 में काम कर रहा है, मैंने अभी परीक्षण किया है और इसका इस्तेमाल किया है। – user2094178

+0

लार्वेल 4.2 पुराने संस्करण पर अच्छी तरह से काम कर रहा है। –

+1

इस दृष्टिकोण के साथ समस्याओं/चेतावनियों के लिए https://stackoverflow.com/a/32262707/96944 –

10

यदि आप एक और मोनोलॉग हैंडलर जोड़ना चाहते हैं, तो आप एप्लिकेशन की कॉन्फ़िगरेशनमोनलॉगिंग विधि का उपयोग कर सकते हैं।

प्लेस बूटस्ट्रैप/app.php फ़ाइल में इस विधि के लिए एक कॉल ठीक पहले $ एप्लिकेशन चर दिया जाता है:

$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

यहां अधिक जानकारी -> http://laravel.com/ डॉक्स/5।1/त्रुटियों –

+0

यह त्रुटि प्राप्त कर रहा है 'PHP घातक त्रुटि: कक्षा 56' –

+4

पर /var/www/html/project/bootstrap/app.php में क्लास' स्ट्रीमहैंडलर 'नहीं मिला @ रोहित जिंदल' मोनोलॉग \ हैंडलर 'के लिए' स्ट्रीमहैंडलर ' \ StreamHandler'। आपको यह भी मिल सकता है कि आपको 'मोनोलॉग \ लॉगर' के लिए 'लॉगर' को स्वैप करने की भी आवश्यकता है। – alexrussell

0

लार्वेल लॉगिंग के लिए मोनोलॉग का उपयोग करता है, लेकिन यह एक पुराने संस्करण का उपयोग करता है जो एकल लॉग स्तर में एकाधिक चैनलों का समर्थन नहीं करता है। laravel 5.2 custom log file for different tasks

2

एक साधारण लकड़हारा सहायक है कि आप मक्खी पर एक से अधिक कस्टम फ़ाइलों के लिए लॉग इन करने की अनुमति देता है:

यहाँ एक सुझाव है कि मदद मिल सकती है है। आप अपना कस्टम हैंडलर भी जोड़ सकते हैं और फ़ाइल पथ सेट कर सकते हैं।

अनुप्रयोग \ हेल्पर \ LogToChannels.php

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

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); 
    } 
} 

अनुप्रयोग \ प्रदाता \ 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 (सेवा प्रदाता को जोड़ने)

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

तब कहीं भी अपने अनुप्रयोग में आप निर्भरता इंजेक्शन (अपने निर्माता में वर्ग जोड़ सकते हैं और एक log वर्ग विशेषता को यह बाँध)

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

तुम भी

$this->log->addChannel('channel_name', $customHandler); 
फोन करके अपने लॉगर आउटपुट अनुकूलित कर सकते हैं का उपयोग कर कॉल कर सकते हैं

और यह तब पहुंच योग्य होगा जब आप अपने ऐप में कहीं भी अपना नाम कॉल करेंगे।

संबंधित मुद्दे