New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Request] Docker-friendly logging #126

Closed
bkuhl opened this Issue Jun 27, 2016 · 11 comments

Comments

Projects
None yet
10 participants
@bkuhl

bkuhl commented Jun 27, 2016

Docker expects all logs to be sent to stdout and stderr and Laravel doesn't seem to have the ability to separate logs in such a way, but requires all logs go to either rather than the ability to split them up. It'd be great if we were able to send error logs to stderr and the rest of the logs to stdout.

@guiwoda

This comment has been minimized.

Show comment
Hide comment
@guiwoda

guiwoda Oct 18, 2016

You can, but not directly through Laravel's Log facade. You need to add two handlers to Monolog:

$monolog = Log::getMonolog();

// Always add the stderr output for errors over WARNING level.
$monolog->pushHandler(
    new \Monolog\Handler\StreamHandler('php://stderr', \Monolog\Logger::WARNING)
);

// Conditionally add stdout debug.
if (config('app.debug')) {
    $monolog->pushHandler(
        new \Monolog\Handler\StreamHandler('php://stdout', \Monolog\Logger::DEBUG)
    );
}

Something along those lines!

EDIT: I understand that the stdout Handler will also get errors. I'm looking up ways to configure a max level in Monolog, but this would be a minor problem, as stdout should only be done in a debug setup.

guiwoda commented Oct 18, 2016

You can, but not directly through Laravel's Log facade. You need to add two handlers to Monolog:

$monolog = Log::getMonolog();

// Always add the stderr output for errors over WARNING level.
$monolog->pushHandler(
    new \Monolog\Handler\StreamHandler('php://stderr', \Monolog\Logger::WARNING)
);

// Conditionally add stdout debug.
if (config('app.debug')) {
    $monolog->pushHandler(
        new \Monolog\Handler\StreamHandler('php://stdout', \Monolog\Logger::DEBUG)
    );
}

Something along those lines!

EDIT: I understand that the stdout Handler will also get errors. I'm looking up ways to configure a max level in Monolog, but this would be a minor problem, as stdout should only be done in a debug setup.

@bbjay

This comment has been minimized.

Show comment
Hide comment
@bbjay

bbjay Mar 11, 2017

didn't know where to put the sample above...
combining the code above with the advice given in this answer worked for me:
http://stackoverflow.com/questions/32552450/laravel-how-to-log-info-to-separate-file

In bootstrap/app.php:

$app->configureMonologUsing(function($monolog) {
    // Always add the stderr output for errors over WARNING level.
    $monolog->pushHandler(
        new \Monolog\Handler\StreamHandler('php://stderr', \Monolog\Logger::WARNING)
    );
});

bbjay commented Mar 11, 2017

didn't know where to put the sample above...
combining the code above with the advice given in this answer worked for me:
http://stackoverflow.com/questions/32552450/laravel-how-to-log-info-to-separate-file

In bootstrap/app.php:

$app->configureMonologUsing(function($monolog) {
    // Always add the stderr output for errors over WARNING level.
    $monolog->pushHandler(
        new \Monolog\Handler\StreamHandler('php://stderr', \Monolog\Logger::WARNING)
    );
});
@andersonn-roberto

This comment has been minimized.

Show comment
Hide comment
@andersonn-roberto

andersonn-roberto Aug 7, 2017

Tank you a lot @bbjay and @guiwoda !
I was spending a lot of time trying to figure out what was wrong with my app, the error only said that Laravel can't create a singleLog.
After inserting your code on bootstrap/app.php the truly error showed up.

Thanks!

andersonn-roberto commented Aug 7, 2017

Tank you a lot @bbjay and @guiwoda !
I was spending a lot of time trying to figure out what was wrong with my app, the error only said that Laravel can't create a singleLog.
After inserting your code on bootstrap/app.php the truly error showed up.

Thanks!

@gnumoksha

This comment has been minimized.

Show comment
Hide comment
@gnumoksha

gnumoksha Oct 26, 2017

For Lumen 5.4.*:

bootstrap/app.php

[...]

$app->configureMonologUsing(function ($monolog) {
    $monolog->pushHandler(new \Monolog\Handler\StreamHandler('php://stderr', \Monolog\Logger::WARNING));

    return $monolog;
});

return $app;

gnumoksha commented Oct 26, 2017

For Lumen 5.4.*:

bootstrap/app.php

[...]

$app->configureMonologUsing(function ($monolog) {
    $monolog->pushHandler(new \Monolog\Handler\StreamHandler('php://stderr', \Monolog\Logger::WARNING));

    return $monolog;
});

return $app;
@Yo1L

This comment has been minimized.

Show comment
Hide comment
@Yo1L

Yo1L Nov 23, 2017

A solution with no code, only configuration for Laravel and Apache.
I’ve just set up Monolog to log in errorlog and Apache redirects ErrorLog in stdout.

.env

APP_LOG=errorlog

apache2 site conf

  ErrorLog /dev/stderr
  TransferLog /dev/stdout

I think you could do the same with nginx.

Yo1L commented Nov 23, 2017

A solution with no code, only configuration for Laravel and Apache.
I’ve just set up Monolog to log in errorlog and Apache redirects ErrorLog in stdout.

.env

APP_LOG=errorlog

apache2 site conf

  ErrorLog /dev/stderr
  TransferLog /dev/stdout

I think you could do the same with nginx.

@lifeofguenter

This comment has been minimized.

Show comment
Hide comment
@lifeofguenter

lifeofguenter Feb 9, 2018

thanks @YoannLeViavant - this also works very well with docker/php-fpm

lifeofguenter commented Feb 9, 2018

thanks @YoannLeViavant - this also works very well with docker/php-fpm

@taylorotwell

This comment has been minimized.

Show comment
Hide comment
@taylorotwell

taylorotwell Mar 9, 2018

Member

New logging should be able to handle this.

Member

taylorotwell commented Mar 9, 2018

New logging should be able to handle this.

@taylorotwell

This comment has been minimized.

Show comment
Hide comment
@taylorotwell

taylorotwell Mar 14, 2018

Member

I've added an example to the default logging configuration file of how to do this.

Member

taylorotwell commented Mar 14, 2018

I've added an example to the default logging configuration file of how to do this.

@kisanme

This comment has been minimized.

Show comment
Hide comment
@kisanme

kisanme Aug 13, 2018

@taylorotwell Is there any handlers for us to route logs to STDOUT? (I can see StreamHandler is for STDERR messages)

kisanme commented Aug 13, 2018

@taylorotwell Is there any handlers for us to route logs to STDOUT? (I can see StreamHandler is for STDERR messages)

@mfn

This comment has been minimized.

Show comment
Hide comment
@mfn

mfn Aug 13, 2018

@kisanme The streamhandler is for streams, it does not matter where to.

You can use 'driver' => 'custom' and use 'via' => SetupYourLogger::class to set it up in any way, e.g. create a StreamHandler for stdout, etc.

Also see https://laravel.com/docs/5.6/logging#creating-channels-via-factories .

mfn commented Aug 13, 2018

@kisanme The streamhandler is for streams, it does not matter where to.

You can use 'driver' => 'custom' and use 'via' => SetupYourLogger::class to set it up in any way, e.g. create a StreamHandler for stdout, etc.

Also see https://laravel.com/docs/5.6/logging#creating-channels-via-factories .

@kisanme

This comment has been minimized.

Show comment
Hide comment
@kisanme

kisanme Aug 14, 2018

Figured that out @mfn
I've edited what @taylorotwell 's configuration looks like for STDERR to STDOUT. It looks like the following, without need of creating a factory class:

       # In config/logging.php

       'stdout' => [
           'driver'  => 'monolog',
           'handler' => StreamHandler::class,
           'with'    => [
               'stream' => 'php://stdout',
           ],
       ],

kisanme commented Aug 14, 2018

Figured that out @mfn
I've edited what @taylorotwell 's configuration looks like for STDERR to STDOUT. It looks like the following, without need of creating a factory class:

       # In config/logging.php

       'stdout' => [
           'driver'  => 'monolog',
           'handler' => StreamHandler::class,
           'with'    => [
               'stream' => 'php://stdout',
           ],
       ],
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment