Php

Logging on to STDOUT Using Lumen/Laravel

This quick tutorial help to send lumen log to stdout into the system.Logging is an important part of modern application development, regardless of the platform targeted or framework used.

Lumen is integrated with the Monolog logging library, which provides support for a variety of powerful log handlers. You can use lumen logging for information, error and exception handling etc.

The .env file has APP_DEBUG configuration option to control error detail will display through the browser.The Lumen logger provides the eight logging levels defined in RFC 5424, that is emergency, alert, critical, error, warning, notice, info and debug.

  • Log::emergency($error);
  • Log::alert($error);
  • Log::critical($error);
  • Log::error($error);
  • Log::warning($error);
  • Log::notice($error);
  • Log::info($error);
  • Log::debug($error);

So let’s begin, How to stdout error, info, etc in the lumen, There are a lot of questions and answer posted about configured monolog, We will cover both type options:

You can also check other recommended tutorials of Lumen/Laravel,

Option 1: Configure Monolog in Lumen 5.2

You need to add below code into bootstrap/app.php at the end of the file and before the $app return.

Related Post
$app->configureMonologUsing(function($monolog) {
    $monolog->pushHandler(...);

    return $monolog;
});

Option 2: Configure Monolog in Lumen 5.6

Option 1: will not work with lumen 5.6, You will get undefined configureMonologUsing() method exception, So question is, How to stdout error in lumen 5.6+? You can override logger service or create your own custom logger, or use inbuilt simple logger option.

We will implement third option, We will use lumen in-built option, We will copy logger.php file from project\vendor\laravel\lumen-framework\config to project/config/ folder.This file have many log channel to logs information in lumen, default is stack.

We will open .env file and add error log channel to stdout logs.

LOG_CHANNEL=errorlog

Now run application, You will get stdout message as like below:

5/16/2018 11:48:57 PM2018/05/17 05:48:57 [error] 13#13: *3 FastCGI sent in stderr: "PHP message: [2018-05-17 05:48:58] production.INFO: testing stdout logs" while reading response header from upstream, client: xx.xx.xx.xx, server: , request: "GET /api/v1/hello HTTP/1.1", upstream: "fastcgi://xx.xx.xx.xx:9000", host: "phpflow.com"
5/16/2018 11:48:57 PM10.42.147.141 - - [17/May/2018:05:48:57 +0000] "GET /api/v1/hello HTTP/1.1" 200 35 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.119 Safari/537.36"

Updated:

Option 3: Print Laravel Logs into Docker Console

Sometimes above solutions not worked, Continuously getting Call to undefined method App\Application::configureMonologUsing() message as a php Fatal error into Laravel or Lumen application.I have found this solution worked with laravel 4, laravel 5.1 and lumen5.1.We will add below code at the end of the bootstrap/app.php file and before the $app return.

$json = new \Monolog\Formatter\JsonFormatter();
$stdouthandler = new \Monolog\Handler\StreamHandler('php://stdout', 'info');
$stdouthandler->setFormatter($json);
Log::pushHandler($stdouthandler);

Recent Posts

Configure and Retrieve S3 Information Using Laravel PHP-AWS-SDK

This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More

4 months ago

What is the Purpose of php_eol in PHP?

in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More

10 months ago

Laravel Table Relationship Methods With Example

This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More

10 months ago

Exploring the Power of Laravel Eloquent Join?

We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More

10 months ago

Quick and Easy Installation of Laravel Valet

in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More

11 months ago

What is Laravel Soft Delete and How Does it Work?

I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More

11 months ago

Categories