GithubHelp home page GithubHelp logo

waza-ari / monolog-mysql Goto Github PK

View Code? Open in Web Editor NEW
141.0 141.0 85.0 71 KB

MySQL Handler for Monolog, which allows to store log messages to a MySQL Table

License: MIT License

PHP 81.85% Makefile 5.22% Dockerfile 12.93%
help-wanted

monolog-mysql's People

Contributors

clement-garrigou avatar cr-lgl avatar dzuelke avatar kermorgant avatar kovenant avatar lpotherat avatar mbacodes avatar nickgravel avatar sashotoster avatar squirrelcrmhub avatar stefandoorn avatar waza-ari 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  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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

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

monolog-mysql's Issues

Include Level name

Hi
is it possible to include the level name and write this into the database automaticly?
array(8) {
["DEBUG"]=>
int(100)
["INFO"]=>
int(200)
["NOTICE"]=>
int(250)
["WARNING"]=>
int(300)
["ERROR"]=>
int(400)
["CRITICAL"]=>
int(500)
["ALERT"]=>
int(550)
["EMERGENCY"]=>
int(600)
}
thanks

SQLSTATE[HY093]: Invalid parameter number: parameter was not defined

The error below has started to occur when running PHPUnit tests in Laravel (5.3.*) . I know this error has been mentioned before but this has only just appeared within the last 48 hours after upgrading from 1.0.3 to 1,0.4. I'm not using Silex or its Monolog Service Provider.

1) ConsoleHelperTest::testReportSuccess
Doctrine\DBAL\Driver\PDOException: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined

C:\wamp\www\vendor\doctrine\dbal\lib\Doctrine\DBAL\Driver\PDOStatement.php:93
C:\wamp\www\vendor\wazaari\monolog-mysql\src\MySQLHandler\MySQLHandler.php:214
C:\wamp\www\vendor\monolog\monolog\src\Monolog\Handler\AbstractProcessingHandler.php:37
C:\wamp\www\vendor\monolog\monolog\src\Monolog\Logger.php:337
C:\wamp\www\packages\logging\src\Logging.php:190
C:\wamp\www\packages\logging\src\Logging.php:93
C:\wamp\www\vendor\laravel\framework\src\Illuminate\Support\Facades\Facade.php:237
C:\wamp\www\app\Traits\ConsoleHelper.php:20
C:\wamp\www\app\Traits\ConsoleHelper.php:20
C:\wamp\www\tests\Traits\ConsoleHelperTest.php:24

Caused by
PDOException: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined

I do have a custom wrapper to allow extra parameters to be passed into the MySQL logging table, and it's been working just fine for the last 18 months or so.

Rolling back to 1.0.3 fixes this.

Multiinsert query issue

Hello, i am have installed and setup this addon for mysql but i have issue that one insert in db is multiinserted for 15 times. i am using MVC php framework of my own i dont have clue what can be issue. I have debug mysql insert query and it only shows 1 insert and what is strange there all have some timeframe.

Can you help me with this issue ?

Thank you very much.

Write 'extra' passed by Processors to db

Default monolog processors pass their info in $record['extra'] not only $record['context'].

If you want them to be written to db to, merging them into context before write could be a way to go.

Implicit committing may interrupt transactions

First, thanks for the library, it's handy and doing well out-of-box.

However the CREATE TABLE IF NOT EXISTS... SQL in the initialize method may interrupt transactions unintentionally.

For the flow as following:

  1. beginTransaction
  2. execute some sql
  3. log with monolog-mysql
  4. rollback transaction

The rollback in step 4. will do nothing, because at step 3. the initialize method will be called and after the execution of SQL CREATE TABLE..., the transaction will be committed implicitly.

errors in handler

Hello,

i will use the monolog-mysql to write messages in the 'logs' table.
With composer i have installed the last version of monolog-mysql.
The version is 1.0.4 .

When i start the first time, i get an error.
In the class 'MySQLHandler' -> write() need ' : void'

I will send same information with Monolog.
Here my test skript

try{
// MySQL Logger
$dsn = 'mysql:dbname=test;host=localhost';
$user = 'test';
$password = 'test';

$pdo = new \PDO($dsn, $user, $password);

$mySQLHandler = new MySQLHandler($pdo, "logs", array('errorCode', 'line', 'file'), \Monolog\Logger::DEBUG);
$logger_mysql = new Logger('MyApp2');
$logger_mysql->pushHandler($mySQLHandler);

throw new \Exception('ein Fehler', 1005);

}
catch(\Throwable $e){
$info = [
'errorCode' => $e->getCode(),
'line' => $e->getLine(),
'file' => $e->getFile()
];

$logger_mysql->alert($e->getMessage(), $info);

}

In the database exists a 'logs' table.
After a test the table has the columns 'id' , 'errorCode' , 'line' and 'file' .
I missing the standard columns of Monolog.

In the method write is the line:

$this->statement->execute($contentArray);

This executed the query:
INSERT INTO logs (channel, level, message, time, errorCode, line, file) VALUES (:channel, :level, :message, :time, :errorCode, :line, :file)

But nothing happened.

Can you help ?

Your sincerly

Stephan

"PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number" when used with Silex and Monolog Service Provider

I'm trying to integrate monolog-mysql with MonologServiceProvider of Silex. I followed the guide for pushing this handler into Monolog Service Provider, but as I call a Silex route, I get:

    Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in <myPath>/vendor/wazaari/monolog-mysql/src/MySQLHandler/MySQLHandler.php on line 131

    Notice: Array to string conversion in <myPath>/vendor/wazaari/monolog-mysql/src/MySQLHandler/MySQLHandler.php on line 131

    Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in <myPath>/vendor/wazaari/monolog-mysql/src/MySQLHandler/MySQLHandler.php on line 131

    Notice: Array to string conversion in <myPath>/vendor/wazaari/monolog-mysql/src/MySQLHandler/MySQLHandler.php on line 131

    Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in <myPath>/vendor/wazaari/monolog-mysql/src/MySQLHandler/MySQLHandler.php on line 131

    Notice: Array to string conversion in <myPath>/vendor/wazaari/monolog-mysql/src/MySQLHandler/MySQLHandler.php on line 131

    Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in <myPath>/vendor/wazaari/monolog-mysql/src/MySQLHandler/MySQLHandler.php on line 131

    Notice: Array to string conversion in <myPath>/vendor/wazaari/monolog-mysql/src/MySQLHandler/MySQLHandler.php on line 131

    Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in <myPath>/vendor/wazaari/monolog-mysql/src/MySQLHandler/MySQLHandler.php on line 131

    Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in <myPath>/vendor/wazaari/monolog-mysql/src/MySQLHandler/MySQLHandler.php on line 131

    Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in <myPath>/vendor/wazaari/monolog-mysql/src/MySQLHandler/MySQLHandler.php on line 131

    Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in <myPath>/vendor/wazaari/monolog-mysql/src/MySQLHandler/MySQLHandler.php on line 131

    Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in <myPath>/vendor/wazaari/monolog-mysql/src/MySQLHandler/MySQLHandler.php on line 131

    Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in <myPath>/vendor/wazaari/monolog-mysql/src/MySQLHandler/MySQLHandler.php on line 131

    Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in <myPath>/vendor/wazaari/monolog-mysql/src/MySQLHandler/MySQLHandler.php on line 131

    Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in <myPath>/vendor/wazaari/monolog-mysql/src/MySQLHandler/MySQLHandler.php on line 131

    Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in <myPath>/vendor/wazaari/monolog-mysql/src/MySQLHandler/MySQLHandler.php on line 131

MonologServiceProvider and monolog-mysql are registered this way:

    $app->register(new Silex\Provider\MonologServiceProvider(), array(
        'monolog.logfile' => __DIR__.'/logs/myApp.log',
        'monolog.name' => 'myApp'
    ));
    /**
     * Add the MySQL handler.
     * See: https://github.com/waza-ari/monolog-mysql
     */
    $app->extend('monolog', function($monolog,$app){
        //Create MysqlHandler
        $dbInstance = new PDO('mysql:host=localhost;dbname=myApp', 'root', '');
        $mySQLHandler = new MySQLHandler\MySQLHandler($dbInstance, "logs", array('type'), \Monolog\Logger::DEBUG);
        $monolog->pushHandler($mySQLHandler);
        return $monolog;
    });

The service provider is somewhat working because it logged something:

    [2016-05-17 19:16:22] myApp.INFO: Monolog service Registered {"logText":"Monolog service Registered","timeRecord":1463505382,"type":"0"} []
    [2016-05-17 19:16:22] myApp.INFO: Matched route "GET_". {"route_parameters":{"_controller":"[object] (Closure: {})","_route":"GET_"},"request_uri":"http://localhost/"} []
    [2016-05-17 19:16:22] myApp.INFO: > GET / [] []
    [2016-05-17 19:16:22] myApp.INFO: < 200 [] []
    [2016-05-17 19:16:23] myApp.INFO: Monolog service Registered {"logText":"Monolog service Registered","timeRecord":1463505383,"type":"0"} []
    [2016-05-17 19:16:23] myApp.INFO: Matched route "GET_". {"route_parameters":{"_controller":"[object] (Closure: {})","_route":"GET_"},"request_uri":"http://localhost/"} []
    [2016-05-17 19:16:23] myApp.INFO: > GET / [] []
    [2016-05-17 19:16:23] myApp.INFO: < 200 [] []
    [2016-05-17 19:18:36] myApp.INFO: Monolog service Registered [] []
    [2016-05-17 19:18:36] myApp.INFO: Matched route "GET_". {"route_parameters":{"_controller":"[object] (Closure: {})","_route":"GET_"},"request_uri":"http://localhost/"} []
    [2016-05-17 19:18:36] myApp.INFO: > GET / [] []
    [2016-05-17 19:18:36] myApp.INFO: < 200 [] []
    [2016-05-17 19:18:36] myApp.INFO: Monolog service Registered [] []
    [2016-05-17 19:18:36] myApp.INFO: Matched route "GET_". {"route_parameters":{"_controller":"[object] (Closure: {})","_route":"GET_"},"request_uri":"http://localhost/"} []
    [2016-05-17 19:18:36] myApp.INFO: > GET / [] []
    [2016-05-17 19:18:36] myApp.INFO: < 200 [] []
    [2016-05-17 19:21:29] myApp.INFO: Monolog service Registered [] []
    [2016-05-17 19:21:29] myApp.INFO: Matched route "GET_". {"route_parameters":{"_controller":"[object] (Closure: {})","_route":"GET_"},"request_uri":"http://localhost/"} []
    [2016-05-17 19:21:29] myApp.INFO: > GET / [] []
    [2016-05-17 19:21:29] myApp.INFO: < 200 [] []
    [2016-05-17 19:22:36] myApp.INFO: Monolog service Registered [] []
    [2016-05-17 19:22:36] myApp.INFO: Matched route "GET_". {"route_parameters":{"_controller":"[object] (Closure: {})","_route":"GET_"},"request_uri":"http://localhost/"} []
    [2016-05-17 19:22:36] myApp.INFO: > GET / [] []
    [2016-05-17 19:22:36] myApp.INFO: < 200 [] []
    [2016-05-17 19:23:17] myApp.INFO: Monolog service Registered [] []
    [2016-05-17 19:23:17] myApp.INFO: Matched route "GET_". {"route_parameters":{"_controller":"[object] (Closure: {})","_route":"GET_"},"request_uri":"http://localhost/"} []
    [2016-05-17 19:23:17] myApp.INFO: > GET / [] []
    [2016-05-17 19:23:17] myApp.INFO: < 200 [] []
    [2016-05-17 19:24:45] myApp.INFO: Monolog service Registered [] []
    [2016-05-17 19:24:45] myApp.INFO: Matched route "GET_". {"route_parameters":{"_controller":"[object] (Closure: {})","_route":"GET_"},"request_uri":"http://localhost/"} []
    [2016-05-17 19:24:45] myApp.INFO: > GET / [] []
    [2016-05-17 19:24:45] myApp.INFO: < 200 [] []
    [2016-05-17 19:27:01] myApp.INFO: Monolog service Registered [] []
    [2016-05-17 19:27:01] myApp.INFO: Matched route "GET_". {"route_parameters":{"_controller":"[object] (Closure: {})","_route":"GET_"},"request_uri":"http://localhost/"} []
    [2016-05-17 19:27:01] myApp.INFO: > GET / [] []
    [2016-05-17 19:27:01] myApp.INFO: < 200 [] []
    [2016-05-17 19:28:10] myApp.INFO: Monolog service Registered [] []
    [2016-05-17 19:28:10] myApp.INFO: Matched route "GET_". {"route_parameters":{"_controller":"[object] (Closure: {})","_route":"GET_"},"request_uri":"http://localhost/"} []
    [2016-05-17 19:28:10] myApp.INFO: > GET / [] []
    [2016-05-17 19:28:10] myApp.INFO: < 200 [] []
    [2016-05-17 19:28:11] myApp.INFO: Monolog service Registered [] []
    [2016-05-17 19:28:11] myApp.INFO: Matched route "GET_". {"route_parameters":{"_controller":"[object] (Closure: {})","_route":"GET_"},"request_uri":"http://localhost/"} []
    [2016-05-17 19:28:11] myApp.INFO: > GET / [] []
    [2016-05-17 19:28:11] myApp.INFO: < 200 [] []
    [2016-05-17 19:30:43] myApp.INFO: Monolog service Registered [] []
    [2016-05-17 19:30:43] myApp.INFO: Matched route "GET_". {"route_parameters":{"_controller":"[object] (Closure: {})","_route":"GET_"},"request_uri":"http://localhost/"} []
    [2016-05-17 19:30:43] myApp.INFO: > GET / [] []
    [2016-05-17 19:30:43] myApp.INFO: < 200 [] []
    [2016-05-17 19:30:44] myApp.INFO: Monolog service Registered [] []
    [2016-05-17 19:30:44] myApp.INFO: Matched route "GET_". {"route_parameters":{"_controller":"[object] (Closure: {})","_route":"GET_"},"request_uri":"http://localhost/"} []
    [2016-05-17 19:30:44] myApp.INFO: > GET / [] []
    [2016-05-17 19:30:44] myApp.INFO: < 200 [] []
    [2016-05-17 19:32:55] myApp.INFO: Monolog service Registered [] []
    [2016-05-17 19:32:55] myApp.INFO: Matched route "GET_". {"route_parameters":{"_controller":"[object] (Closure: {})","_route":"GET_"},"request_uri":"http://localhost/"} []
    [2016-05-17 19:32:55] myApp.INFO: > GET / [] []
    [2016-05-17 19:32:55] myApp.INFO: < 200 [] []
    [2016-05-17 19:32:56] myApp.INFO: Monolog service Registered [] []
    [2016-05-17 19:32:56] myApp.INFO: Matched route "GET_". {"route_parameters":{"_controller":"[object] (Closure: {})","_route":"GET_"},"request_uri":"http://localhost/"} []
    [2016-05-17 19:32:56] myApp.INFO: > GET / [] []
    [2016-05-17 19:32:56] myApp.INFO: < 200 [] []

What am I doing wrong? Is this plugin compatible with Silex+Monolog Service Provider?

Data should not be deleted when additional field is not provided anymore

As pointed out in Pull Request #8 it is rather unintuitive that additional fields are deleted when not provided at class initialization. One example by FoxxMD:

What if I pushed two separate handlers with different additionalFields because I only wanted to include a subset of information on one handler? When that handler is initialized it would remove the missing columns and all my data!

There are two obvious ways:

  1. When new additionalFields are used, create the missing ones, but to not remove stale fields. Fill them with null if not provided (which is done anyway by now). They can still be deleted by hand
  2. Do not alter the table anyway. The user must create the logging table and specify which addionalFields can be used. If there are any addionalFields, which do not exist, then (2a) just ignore them or (2b) throw an exception.

Option 1 would give more flexibility, but option 2 maybe more stability plus the support for SQlite (see #8), as meta information need not to be queried from database.

What do you think?

Create indexes

If we want to sort on the time field, or filter by channel or level, and the log table has many records, it could take a looong time...
Also, if one needs to periodically truncate the log (for instance, discarding entries older than a given treshold) it would be slow.
You should add indexes to those fields.
You may also give the option of creating them if one needs them, for those people that, for some reason, may not want them.

Dropping or getting around the PDO type hint in MySQLHandler::__construct()?

I'm trying to use this in a project where database connection can remain open for a very long time. I've thus wrapped PDO in a ReconnectingPDO class, which catches the "connection has gone away" error, reconnects, then re-runs the query.

When I give a ReconnectingPDO instance to __construct(), I get the error that I've passed in an object of the wrong type. Fair enough, but I know it's the wrong type and want to proceed anyway. Since PHP doesn't check inheritance when using type hints (to my knowledge at least), is there any way to get around this restriction? I'm all for type hinting, and use it in everytyhing I write. In this case, though, it's causing a problem. Indeed, I had to go back and remove all my project's PDO type hints when I implemented ReconnectingPDO for this same reason.

PDOStatement::execute(): SQLSTATE[HY093]

Hi
Just had to revert back to 1.0.4 because in 1.0.5 was getting
PHP Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in

any ideas

PHP 5 Support

Hello how are you?

Does this extension work with PHP 5?

Thanks

Columns channel, level, message and time are deleted

Hi,
When I started using monolog-mysql, I swear that all was working fine. But suddenly, no logs are being created. The problem is that only the additional fields are present in the database table. The channel, level, message and time columns are being deleted by this code:

$removedColumns = array_diff($actualFields, $this->additionalFields);

if (!empty($removedColumns)) foreach ($removedColumns as $c) {
    $this->pdo->exec('ALTER TABLE `'.$this->table.'` DROP `'.$c.'`;');
}

$actualFields are channel, level, message and time, and are not present in $this->additionalFiels, so of course they are stored in $removedColumns and deleted in the foreach loop.

Am I doing something wrong?

Thanks!

additional fields types

I've implemented additional fields to have types and lengths.

$mySQLHandler = new MySQLHandler($dbo, "log", array('username' => array('type' => 'varchar', 'length' => '100'), 'user_id' => array('type' => 'int'))

I wonder why this is not implemented yet, seems to me people would like a little bit more control over the types of additional fields added, rather than having multiple TEXT fields generated for them.

Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens

Error message:

Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in **/vendor/wazaari/monolog-mysql/src/MySQLHandler/MySQLHandler.php on line 214

Situation:

When some additionalFields are provided with empty values.
No logs are recorded to database when this warning shows.

Code problem observed:

While $contentArray contains the full list of key-values,
$this->statement is only prepared with keys that has non-empty values. Thus it is true that number of bound variables does not match number of tokens.

information to logs table

Hello,

if i correct the columns of the table manually,
the application works.

Is there a way to the value of time in the ISO8601 standard.
Is better ro read.

Your sincerly

Stephan

Logging fails if the caller does not supply all additional fields

If I define an additional field A, I can no longer issue a call like:

$logger->log($msg);

It will fail with SQLSTATE[HY093]: Invalid parameter number
I would have to always pass an extra array on all logging calls, like this:

$logger->log($msg, ['A'=>'something']);

Not all logging calls in an application can be made to always supply all the extra fields, sometimes that is not even under the control of the application developer (a third-party library may be making it's own logging calls, for instance).

question regarding capabilities

i am looking to obfuscate specifically an error on Mysql and so that when logged it does not leak any values persisted to the database, just the shape or form of the tables and fields but never values.

I believe we can extend this to do just that.

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.