Giter Club home page Giter Club logo

monolog-mysql's Introduction

Laravel Monolog MySQL Handler.

This package will log errors into MySQL database instead storage/log/laravel.log file.

Installation

composer require markhilton/monolog-mysql

Open up config/app.php and find the providers key.

'providers' => array(
    // ...
    Logger\Laravel\Provider\MonologMysqlHandlerServiceProvider::class,
);

Publish config using Laravel Artisan CLI.

php artisan vendor:publish

Migrate tables - you may want to configure enviornment beforehand.

php artisan migrate

Application Integration

In your application config/logging.php add:

use Logger\Monolog\Handler\MysqlHandler;

// ...

'channels' => [
    // ...
    'mysql' => [
        'driver' => 'monolog',
        'handler' => MysqlHandler::class,
        'level' => 'debug',
    ],
];

Application Integration (Laravel >= 5.6)

In your application config/logging.php add:

<?php
    // [...]
    'channels' => [
        'stack' => [
            'driver' => 'stack',
            'channels' => ['mysql'],
        ],
        // [...]
        'mysql' => [
            'driver' => 'custom',
            'via' => App\Logging\CreateMySQLLogger::class,
        ],
    ],

In your application app/Logging/CreateMySQLLogger.php add:

<?php
namespace App\Logging;
use Exception;
use Monolog\Logger;
use Logger\Monolog\Handler\MysqlHandler;
class CreateMySQLLogger
{
    /**
     * Create a custom Monolog instance.
     *
     * @param  array $config
     * @return Logger
     * @throws Exception
     */
    public function __invoke(array $config)
    {
        $channel = $config['name'] ?? env('APP_ENV');
        $monolog = new Logger($channel);
        $monolog->pushHandler(new MysqlHandler());
        return $monolog;
    }
}

Environment configuration

If you wish to change default table name to write the log into or database connection use following definitions in your .env file

DB_LOG_TABLE=logs
DB_LOG_CONNECTION=mysql

Credits

Based on:

monolog-mysql's People

Contributors

enaah avatar ilumos avatar kacperaniolek avatar markhilton avatar nafizcayli avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

monolog-mysql's Issues

New tag for master

Could you create a new version tag to be able to get the last fixes ?

logs context is empty and timezone is wrong

D:\phpStudy\WWW\little-feature\vendor\markhilton\monolog-mysql\src\Logger\Monolog\Handler\MysqlHandler.php
this file write the log ,then I have two question :
1、context is almost empty;
2、timeZone is not my .env APP_TIMEZONE,then I changed as follow :
do you have a better way ?

    protected function write(array $record)
    {
        dd($record['context']['exception']);
        $data = [
            'instance' => gethostname(),
            'message' => $record['message'],
            'channel' => $record['channel'],
            'level' => $record['level'],
            'level_name' => $record['level_name'],
            'context' => $record['formatted'], #my change
            // 'context'     => json_encode($record['context']), #old
            'remote_addr' => isset($_SERVER['REMOTE_ADDR']) ? ip2long($_SERVER['REMOTE_ADDR']) : null,
            'user_agent' => isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : null,
            'created_by' => Auth::id() > 0 ? Auth::id() : null,
            'created_at' => $record['datetime']->setTimeZone(new \DateTimeZone(env('APP_TIMEZONE') ?: 'UTC'))->format('Y-m-d H:i:s'), #I changed this
        ];

        DB::connection($this->connection)->table($this->table)->insert($data);
    }

Laravel 5.6

when will be supported for laravel 5.6

I tried to install and gave error

IPv6 Support

Hi there,

Great log handler!

Currently the handler uses ip2long() to record the remote IP, and this function does not support IPv6 addresses.

Please consider using inet_ntop and MySQL's VARBINARY field type to store IPv4 and IPv6 addresses.

Many thanks!

Laravel 5.5 Error on migration

I did follow all the installation steps and get following error when im at the step:

php artisan migrate

[Symfony\Component\Debug\Exception\FatalThrowableError] Class '' not found

Log::debug 如何兼容

// 这样主动记录日志
\Illuminate\Support\Facades\Log::debug('test');

就会报错:
SQLSTATE[HY000] [2002] Connection refused (SQL: insert into logs (instance, message, channel, level, level_name, context, remote_addr, user_agent, created_by, created_at) values (49494f8720a8, Allowed memory size of 268435456 bytes exhausted (tried to allocate 43042376 bytes), local, 400, ERROR, {"exception":{}}, 2887122945, Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36, ?, 2023-11-02 11:42:41))

Symfony\Component\Debug\Exception\FatalErrorException : Declaration of Logger\Monolog\Handler\MysqlHandler::write(array $record) must be compatible with Monolog\Handler\AbstractProcessingHandler::write(array $record): void

Hi, i got this error while run command php artisan migrate

Symfony\Component\Debug\Exception\FatalErrorException  : Declaration of Logger\Monolog\Handler\MysqlHandler::write(array $record) must be compatible with Monolog\Handler\AbstractProcessingHandler::write(array $record): void 

PHP VERSION :

Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.13, Copyright (c), by Zend Technologies

LARAVEL VERSION

Laravel Framework 6.20.12

and i came across to fix this error by editing this file https://github.com/markhilton/monolog-mysql/blob/master/src/Logger/Monolog/Handler/MysqlHandler.php

 protected function wirte(array $record)

to

protected function wirte(array $record):void 

#solve
using dev-master branch instead

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.