Wie kann eine separate Datei für logging INFO
In Laravel 5.1
Angegeben werden?
Jede sofortige Hilfe wird sehr dankbar sein. Vielen Dank
Möchten Sie info
speziell in einer Protokolldatei und einen anderen Protokolltyp an einem anderen Speicherort protokollieren? Meine Lösung könnte in diesem Fall nicht helfen, aber dennoch nützlich sein.
Um eine Protokolldatei an einen anderen Speicherort zu schreiben, verwenden Sie die Methode useDailyFiles
oder useFiles
und dann info, um die Protokolldatei unter dem soeben angegebenen Pfad zu protokollieren. Wie so:
Log::useDailyFiles(storage_path().'/logs/name-of-log.log');
Log::info([info to log]);
Der erste Parameter für beide Methoden ist der Pfad der Protokolldatei (die erstellt wird, wenn sie noch nicht vorhanden ist), und für useDailyFiles
ist das zweite Argument die Anzahl der Tage Laravel = wird protokolliert, bevor alte Protokolle gelöscht werden. Der Standardwert ist unbegrenzt, daher habe ich in meinem Beispiel keinen Wert eingegeben.
In Laravel 5.6 kannst du deinen eigenen Kanal in config\logging.php
Erstellen. Wenn du ein Upgrade von einer älteren Laravel Version durchgeführt hast, musst du diese Datei erstellen ( https://laravel.com/docs/5.6/upgrade ).
Fügen Sie dies zu Ihrem Kanal-Array in config\logging.php
Hinzu.
'your_channel_name' => [
'driver' => 'single',
'path' => storage_path('logs/your_file_name.log'),
],
Sie können dann eine der folgenden 8 Protokollierungsstufen aufrufen:
Illuminate\Support\Facades\Log::channel('your_channel_name')->info('your_message');
Die Protokolle werden in logs/your_file_name.log
Gespeichert.
Wenn Sie einen weiteren Monolog-Handler hinzufügen möchten, können Sie die configureMonologUsing-Methode der Anwendung verwenden.
Rufen Sie diese Methode in der Datei bootstrap/app.php auf, bevor die Variable $ app zurückgegeben wird:
$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;
Seit Laravel> = 5.6 können wir Log Channels verwenden, damit es auf einfache Weise funktioniert. Auf diese Weise können Sie Protokollkanäle erstellen, die als eigene Protokolldateien mit eigenen Treibern, Pfaden oder Ebenen behandelt werden können. Sie brauchen nur diese paar Zeilen, damit es funktioniert.
config/logging.php:
return [
'channels' => [
'command' => [
'driver' => 'single',
'path' => storage_path('logs/command.log'),
'level' => 'debug',
],
],
];
Logge wo immer du willst, indem du den Kanalnamen analysierst:
Log::channel('command')->info('Something happened!');
Ein einfacher Logger-Helfer, mit dem Sie sich im Handumdrehen bei mehreren benutzerdefinierten Dateien anmelden können. Sie können auch Ihren benutzerdefinierten Handler hinzufügen und den Dateipfad festlegen.
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 (Dienstanbieter hinzufügen)
// Register Service Providers
$app->register(App\Providers\LogToChannelsServiceProvider::class);
Dann können Sie überall in Ihrer App mithilfe der Abhängigkeitsinjektion aufrufen (fügen Sie die Klasse in Ihrem Konstruktor hinzu und binden Sie sie an ein Klassenattribut log
).
$this->log->info('logger_name', 'Log message');
$this->log->error('other_logger_name', 'Log message', $someContext);
Sie können Ihre Logger-Ausgabe sogar anpassen, indem Sie anrufen
$this->log->addChannel('channel_name', $customHandler);
Sie können darauf zugreifen, wenn Sie den Namen an einer beliebigen Stelle in Ihrer App aufrufen.