简体   繁体   中英

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. But on server, error logging works fine, but when i call logger myself and try to log some info message it does not work.

I'm using Laravel 5.4 version.

This is some App\\Console\\Commands\\DummyCommand class for test purposes.

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

$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:

APP_LOG_LEVEL=notice

Changed that to "debug" and now it's working fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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