Come specificare un file separato per logging INFO
in Laravel 5.1
?Laravel: Come registrare INFO per separare il file
Qualsiasi aiuto immediato sarà molto apprezzabile. Grazie
Come specificare un file separato per logging INFO
in Laravel 5.1
?Laravel: Come registrare INFO per separare il file
Qualsiasi aiuto immediato sarà molto apprezzabile. Grazie
Vuoi accedere specificamente info
ad un file di log e un altro tipo di registro a un altro Posizione? La mia soluzione potrebbe non essere d'aiuto in questo caso, ma potrebbe comunque essere utile.
Per scrivere un file di registro in un'altra posizione, utilizzare il metodo useDailyFiles
o useFiles
e quindi informazioni per accedere al file di registro nel percorso appena specificato. In questo modo:
Log::useDailyFiles(storage_path().'/logs/name-of-log.log');
Log::info([info to log]);
Il primo parametro per entrambi i metodi è il percorso del file di log (che viene creato, se non è già presente) e per useDailyFiles
il secondo argomento è il numero di giorni laravel registrerà per prima di cancellare i vecchi log. Il valore predefinito è illimitato, quindi nel mio esempio non ho inserito un valore.
Questo funziona ancora in Laravel 5.4, l'ho appena testato e utilizzato. – user2094178
funziona bene su laravel 4.2 vecchia versione. –
Per problemi/avvertimenti con questo approccio, vedere https://stackoverflow.com/a/32262707/96944 –
Se si desidera aggiungere un altro gestore monolog, è possibile utilizzare il metodo configureMonologUsing dell'applicazione.
Effettuare una chiamata a questo metodo nel file/app.php bootstrap destra prima che la variabile $ app viene restituito:
$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;
Ulteriori informazioni qui -> http://laravel.com/ docs/5.1/errori –
ottenendo questo errore 'PHP Errore fatale: Classe 'StreamHandler' non trovata in /var/www/html/project/bootstrap/app.php sulla riga 56' –
@RohitJindal scambia' StreamHandler' per 'Monolog \ Handler \ StreamHandler'. Potresti anche scoprire che devi scambiare 'Logger' per' Monolog \ Logger'. – alexrussell
Laravel utilizza monolog per la registrazione, ma utilizza una versione precedente che non supporta più canali in un singolo livello di registro.
Ecco un suggerimento che può aiutare: laravel 5.2 custom log file for different tasks
Un semplice aiutante logger che permette di accedere a più file personalizzati al volo. Puoi anche aggiungere il tuo gestore personalizzato e impostare il percorso del file.
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 (aggiungere il fornitore del servizio)
// Register Service Providers
$app->register(App\Providers\LogToChannelsServiceProvider::class);
Poi ovunque nella vostra applicazione è possibile chiamare tramite iniezione di dipendenza (aggiungere la classe nel costruttore e associarlo a un attributo log
classe)
$this->log->info('logger_name', 'Log message');
$this->log->error('other_logger_name', 'Log message', $someContext);
È anche possibile personalizzare l'output logger chiamando
$this->log->addChannel('channel_name', $customHandler);
E sarà accessibile quando chiamerai il suo nome ovunque nella tua app.
Possibile duplicato di [Laravel Monolog. Registra livello INFO per separare il file] (http://stackoverflow.com/questions/32286010/laravel-monolog-log-info-level-to-separate-file) –