2017 © Pedro Peláez
 

library laravel-logman

Adds manageable container of context data for logs.

image

tekord/laravel-logman

Adds manageable container of context data for logs.

  • Monday, July 23, 2018
  • by tekord
  • Repository
  • 1 Watchers
  • 0 Stars
  • 0 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

Extra context data logging for Laravel 5

This package allows you to add global context data for logging in easy way., (*1)

Installation

Add the tekord/laravel-logman package in your composer.json and update your dependencies or add it via command line:, (*2)

$ composer require tekord/laravel-logman

If you are using Laravel < 5.5, you also need to add Tekord\Logman\ServiceProvider to your config/app.php providers array:, (*3)

'providers' => [
    ...
    \Tekord\Logman\ServiceProvider::class,
]

and add Tekord\Logman\Facades\Logman to 'aliases' array:, (*4)

'aliases' => [
    ...
    'Logman' => \Tekord\Logman\Facades\Logman::class,
]

Usage examples

Collect context data before request handling

Suppose we want to add incoming request information like request method, URI, user agent and referer to error log. Add the following middleware to your App\Http\Middleware namespace:, (*5)

<?php

namespace App\Http\Middleware;

class WatchWebRequest {
    /**
     * @param \Illuminate\Http\Request $request
     * @param \Closure $next
     * @param string|null $guard
     *
     * @return mixed
     */
    public function handle($request, \Closure $next, $guard = null) {
        Logman::put('request', [
            'method' => $_SERVER['REQUEST_METHOD'],
            'uri' => $_SERVER['REQUEST_URI'],
            'user_agent' => isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : null,
            'referer' => isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : null
        ]);

        return $next($request);
    }
}

Add it to Kernel's middleware list:, (*6)

protected $middleware = [
    ...
    \App\Http\Middleware\WatchWebRequest::class,
];

Go to \App\Exceptions\Handler class and override the context method like this:, (*7)

protected function context() {
    try {
        $context = \Tekord\Logman\Facades\Logman::getContextWith([
            // put additional custom context records here if you want
        ]);
    } catch (\Throwable $e) {
        $context = [];
    }

    return array_merge($context, parent::context());
}

Now if something goes wrong you will see request information in error log files., (*8)

Collect context data for specific controller's action

Add the following code to your controller class:, (*9)

public function callAction($method, $parameters) {
    Logman::put('request', [
        'method' => $_SERVER['REQUEST_METHOD'],
        'uri' => $_SERVER['REQUEST_URI'],
        'user_agent' => isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : null,
        'referer' => isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : null
    ]);

    return parent::callAction($method, $parameters);
}

The Versions

23/07 2018

dev-master

9999999-dev

Adds manageable container of context data for logs.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Cyrill Tekord

laravel logger log logging

23/07 2018

0.9

0.9.0.0

Adds manageable container of context data for logs.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Cyrill Tekord

laravel logger log logging