简体   繁体   English

Monolog在服务器上的Laravel控制台中不起作用

[英]Monolog does not work in Laravel Console on server

I'm having problems with logging some basic info line from Console. 我在从控制台记录一些基本信息行时遇到问题。 Locally, this works fine, everything gets logged (errors, info messages) from web and from console. 在本地,这可以正常工作,所有内容都可以通过Web和控制台进行记录(错误,信息消息)。 But on server, error logging works fine, but when i call logger myself and try to log some info message it does not work. 但是在服务器上,错误日志记录可以正常工作,但是当我自己致电logger并尝试记录一些信息消息时,它将无法正常工作。

I'm using Laravel 5.4 version. 我正在使用Laravel 5.4版本。

This is some App\\Console\\Commands\\DummyCommand class for test purposes. 这是一些App \\ Console \\ Commands \\ DummyCommand类,用于测试。

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Log\Writer as Log;

class DummyCommand extends Command
{
    /**
     * Create a new console command instance.
     *
     * @param  \Illuminate\Log\Writer  $log
     * @return void
     */
    public function __construct(Log $log)
    {
        parent::__construct();

        $this->log = $log;
    }

    /**
 * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $message = "Some dummy message";
        $this->info($message); // gets printed on console both locally and on server
        $this->log->info($message);
        $ret = $this->log->getMonolog()->addInfo($message);

        dump($ret); // locally = true, server = false
        if ($ret === false) {
            throw new \Exception('Error while logging!'); // this log works fine both locally and on server
        }
    }
}

I run the following on the server: 我在服务器上运行以下命令:

php artisan dummy // prints "Some dummy message" and loggs error message "Error while logging!"
php artisan schedule:run // same thing as above

Any idea why is this not working ? 知道为什么这不起作用吗? Permissions on storage folder is fine for all folders and files. 存储文件夹的权限适用于所有文件夹和文件。 Error messages (exceptions) gets logged but info messages does not. 错误消息(异常)将被记录,而信息消息则不会。

Edited: This is working, I need to add following line before calling $this->log->info(); 编辑:这是可行的,我需要在调用$ this-> log-> info();之前添加以下行: but there is no way i would use on all Commands and every time need to set path to log file. 但是我无法在所有命令上使用,并且每次都需要设置日志文件的路径。 (source: https://laracasts.com/discuss/channels/general-discussion/logging-in-l5/replies/11771 ) (来源: https : //laracasts.com/discuss/channels/general-discussion/logging-in-l5/replies/11771

$this->log->useDailyFiles(storage_path().'/logs/laravel.log');

Figured it out, I added multiple message type: 想通了,我添加了多种消息类型:

$this->log->getMonolog()->addAlert($message);
$this->log->getMonolog()->addCritical($message);
$this->log->getMonolog()->addDebug($message);
$this->log->getMonolog()->addError($message);
$this->log->getMonolog()->addWarning($message);

And it logs all messages with error prefix (everything except info). 并记录所有带有错误前缀的消息(除信息外的所有消息)。 So I looked in .env file, and there was next line about log level: 所以我查看了.env文件,下面是有关日志级别的一行:

APP_LOG_LEVEL=notice

Changed that to "debug" and now it's working fine. 将其更改为“调试”,现在可以正常工作了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM