Skip to content

Commit

Permalink
issue #116 Добавил логирование в бД
Browse files Browse the repository at this point in the history
  • Loading branch information
irvis committed Oct 24, 2016
1 parent 3ac65a7 commit 1a6fbbd
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 3 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"firebase/php-jwt": "v4.0.0",
"guzzlehttp/guzzle": "6.2.1",
"packaged/querybuilder":"0.16.1",
"ramsey/uuid": "^3.5"
"ramsey/uuid": "^3.5",
"monolog/monolog": "1.21.0"
},
"require-dev": {
"phpunit/phpunit": "5.5.5",
Expand Down
82 changes: 80 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions sql/2016-10-10-01.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CREATE TABLE `kv_log` (
`gameId` char(36) DEFAULT NULL,
`groupId` char(36) NOT NULL,
`userId` char(36) DEFAULT NULL,
`created` datetime,
`content` blob
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
8 changes: 8 additions & 0 deletions sql/kubikvest.sql
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,12 @@ CREATE TABLE `kv_group` (
`active` tinyint(2) unsigned DEFAULT 1
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `kv_log` (
`gameId` char(36) DEFAULT NULL,
`groupId` char(36) NOT NULL,
`userId` char(36) DEFAULT NULL,
`created` datetime,
`content` blob
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

insert kv_user (userId, provider, uid, accessToken) values ('adff5c92-008c-47ac-bad8-11be43ea1469', 'vk', 1111, 'token');
36 changes: 36 additions & 0 deletions src/Kubikvest/Logger/MysqlHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Kubikvest\Logger;

use Monolog\Handler\AbstractProcessingHandler;
use Monolog\Logger;
use Packaged\QueryBuilder\Assembler\QueryAssembler;
use Packaged\QueryBuilder\Builder\QueryBuilder;

class MysqlHandler extends AbstractProcessingHandler
{
/**
* @var \PDO
*/
protected $pdo;

/**
* @var QueryBuilder
*/
protected $queryBuilder;
protected $table = 'kv_log';

public function __construct(\PDO $pdo, QueryBuilder $queryBuilder, $level = Logger::DEBUG, $bubble = true)
{
$this->pdo = $pdo;
$this->queryBuilder = $queryBuilder;

parent::__construct($level, $bubble);
}
protected function write(array $record)
{
var_dump($record);
//$query = $this->queryBuilder->insertInto($this->table);
//$this->pdo->exec(QueryAssembler::stringify($query));
}
}
39 changes: 39 additions & 0 deletions tests/Kubikvest/Logger/MysqlHandlerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Kubikvest\Logger;

use \Packaged\QueryBuilder\Builder\QueryBuilder;
use Monolog\Logger;

class MysqlHandlerTest extends \PHPUnit_Extensions_Database_TestCase
{
public function getConnection()
{
$dsn = 'mysql:dbname=kubikvest;host='.getenv('DB_HOST').';charset=UTF8';
$pdo = new \PDO($dsn, 'root');

return $this->createDefaultDBConnection($pdo);
}

public function getDataSet()
{
return $this->createFlatXMLDataSet(__DIR__.'/../../fixtures/kv_log.xml');
}

public function testGetInstance()
{
$actual = new MysqlHandler($this->getConnection()->getConnection(), new QueryBuilder());

$this->assertInstanceOf('\\Kubikvest\\Logger\\MysqlHandler', $actual);
}

public function testLog()
{
$handler = new MysqlHandler($this->getConnection()->getConnection(), new QueryBuilder());
$logger = new Logger('user.track');
$logger->setHandlers([$handler]);
$logger->log(Logger::INFO, 'Checkpoint', [
'gameId' => 'sdf',
]);
}
}
8 changes: 8 additions & 0 deletions tests/fixtures/kv_log.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" ?>
<dataset>
<kv_log gameId="adff5c92-008c-47ac-bad8-11be43ea1469"
groupId="adff5c92-008c-47ac-bad8-11be43ea1469"
userId="adff5c92-008c-47ac-bad8-11be43ea1469"
created="2010-04-26 12:14:20"
content="{}"/>
</dataset>

0 comments on commit 1a6fbbd

Please sign in to comment.