Skip to content

Instantly share code, notes, and snippets.

@technoknol
Created May 4, 2017 13:04
Show Gist options
  • Save technoknol/abb25433c89101515d1ac0fcc5d26456 to your computer and use it in GitHub Desktop.
Save technoknol/abb25433c89101515d1ac0fcc5d26456 to your computer and use it in GitHub Desktop.
Working log4php in Laravel (5.4)
<?php
// Middleware:
// app\Http\Middleware\log4php.php
namespace App\Http\Middleware;
use Closure;
class log4php
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$logger_config = \Config::get('log4php.main_logger');
\Logger::configure($logger_config);
$logger = \Logger::getLogger('html');
$log = [];
$log['url'] = $request->getUri();
$log['method'] = $request->getMethod();
$log['client_ip'] = $request->getClientIp();
$log['request'] = $request->all();
if(isset($request->header()['authorization'])) {
$log['headers'] = $request->header()['authorization'] ;
}
$logger->info(json_encode($log));
return $next($request);
}
}
// Kernel.php
// Location : \app\Http\Kernel.php
// Add following
protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
\App\Http\Middleware\TrimStrings::class,
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
\App\Http\Middleware\CORS::class,
\App\Http\Middleware\log4php::class // This is added, Adjust other elements according to you.
];
// Config file
// Location : config\log4php.php
<?php
return [
/*
|--------------------------------------------------------------------------
| Constants
|--------------------------------------------------------------------------
|
| Added By: Shyam Makwana
|
| This file is for storing the credentials for third party services such
| as Stripe, Mailgun, SparkPost and others. This file provides a sane
| default location for this type of information, allowing packages
| to have a conventional place to find your various credentials.
|
*/
'main_logger' => array(
'rootLogger' => array(
'appenders' => array('html'),
),
'appenders' => array(
// 'default' => array(
// 'class' => 'LoggerAppenderFile',
// 'layout' => array(
// 'class' => 'LoggerLayoutSimple'
// ),
// 'params' => array(
// 'file' => '../storage/logs/log4php-'. date('d-m-y'). '-log.log' ,
// 'append' => true
// )
// ),
'html' => array(
'class' => 'LoggerAppenderFile',
'layout' => array(
'class' => 'LoggerLayoutHtml'
),
'params' => array(
'file' => '../storage/logs/log4php-'. date('d-m-y'). '-log.html' ,
'append' => true
)
)
)
)
];
@subaKumaran3
Copy link

Hi, i am new to Laraval. i install log4php. I updated app\Http\Middleware\log4php.php and config\log4php.php files.
I want to know how to use it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment