-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Cody Mays
committed
May 16, 2016
0 parents
commit 10019bf
Showing
4 changed files
with
175 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2015 Stowarzyszenie WIOSNA | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
monolog-pomm | ||
============= | ||
|
||
PostgreSQL Handler for Monolog utilizing the POMM library (http://www.pomm-project.org/), which allows to store log messages in a Postgres Table. | ||
It can log text messages to a specific table, and creates the table automatically if it does not exist. | ||
|
||
Based on https://github.com/wiosna-dev/monolog-pg | ||
|
||
# Installation | ||
monolog-pomm is available via composer. Just add the following line to your required section in composer.json and do a `php composer.phar update`. | ||
|
||
``` | ||
"crxgames/monolog-pomm": ">1.0.0" | ||
``` | ||
|
||
# Usage | ||
Just use it as any other Monolog Handler, push it to the stack of your Monolog Logger instance. The Handler however needs some parameters: | ||
|
||
- **$pomm** PDO Instance of your database. Pass along the PDO instantiation of your database connection with your database selected. | ||
- **$table** The table name where the logs should be stored | ||
- **$level** can be any of the standard Monolog logging levels. Use Monologs statically defined contexts. _Defaults to Logger::DEBUG_ | ||
- **$bubble** _Defaults to true_ | ||
|
||
# Examples | ||
Given that $pomm is your database session instance, you could use the class as follows: | ||
|
||
```php | ||
//Import class | ||
use PommPGHandler\PommPGHandler; | ||
|
||
//Create MysqlHandler | ||
$pommHandler = new PommPGHandler($pomm, "log"); | ||
|
||
//Create logger | ||
$logger = new \Monolog\Logger($context); | ||
$logger->pushHandler($mySQLHandler); | ||
|
||
//Now you can use the logger, and further attach additional information | ||
$logger->addWarning("This is a great message, woohoo!", array('username' => 'John Doe', 'userid' => 245)); | ||
``` | ||
|
||
# License | ||
This tool is free software and is distributed under the MIT license. Please have a look at the LICENSE file for further information. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"name": "crxgames/monolog-pomm", | ||
"description": "A handler for Monolog that sends messages to Postgres via a POMM connection", | ||
"keywords": ["log", "logging", "monolog", "postgresql", "database", "pomm"], | ||
"homepage": "https://github.com/crxgames/monolog-pomm", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Cody Mays", | ||
"email": "crxgames@gmail.com" | ||
} | ||
], | ||
"require": { | ||
"monolog/monolog": ">1.4.0" | ||
}, | ||
"autoload": { | ||
"psr-4": {"PommPGHandler\\": "src/PommPGHandler"} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
|
||
namespace PommPGHandler; | ||
|
||
use Monolog\Logger; | ||
use Monolog\Handler\AbstractProcessingHandler; | ||
|
||
/** | ||
* This class is a handler for Monolog, which is used | ||
* to log records to a PostgreSQL database via Pomm | ||
*/ | ||
class PommPGHandler extends AbstractProcessingHandler { | ||
|
||
/** | ||
* @var bool defines whether the Postgres connection is been initialized | ||
*/ | ||
private $initialized = false; | ||
|
||
/** | ||
* @var resource | ||
*/ | ||
protected $pomm; | ||
|
||
/** | ||
* @var string pg statement name | ||
*/ | ||
private $statement; | ||
|
||
/** | ||
* @var string The default log storage table | ||
*/ | ||
private $table = 'logs'; | ||
|
||
/** | ||
* @param resource $connection | ||
* @param string $table | ||
* @param integer $level | ||
* @param bool $bubble | ||
*/ | ||
public function __construct($pomm, $table, $level = Logger::DEBUG, $bubble = true) | ||
{ | ||
if (get_class($pomm) != 'PommProject\ModelManager\Session') { | ||
throw new \InvalidArgumentException('A connection must be a POMM Session.'); | ||
} | ||
$this->pomm = $pomm; | ||
$this->table = $table; | ||
parent::__construct($level, $bubble); | ||
} | ||
|
||
/** | ||
* Initializes this handler by creating the table if it not exists | ||
*/ | ||
private function initialize() { | ||
$this->pomm->getQueryManager()->query( | ||
'CREATE TABLE IF NOT EXISTS '.$this->table.' (' | ||
. 'channel varchar(255),' | ||
. 'level_name varchar(10),' | ||
. 'message text,' | ||
. 'context json,' | ||
. 'extra json,' | ||
. 'datetime timestamp' | ||
. ')' | ||
); | ||
|
||
$this->statement = $this->pomm->getPreparedQuery('INSERT INTO '.$this->table.' (channel, level_name, message, context, extra, datetime) VALUES ($1, $2, $3, $4, $5, $6)'); | ||
|
||
$this->initialized = true; | ||
} | ||
|
||
/** | ||
* Writes the record down to the log of the implementing handler | ||
* | ||
* @param $record[] | ||
* @return void | ||
*/ | ||
protected function write(array $record) | ||
{ | ||
if (!$this->initialized) { | ||
$this->initialize(); | ||
} | ||
|
||
$content = [ | ||
'channel' => $record['channel'], | ||
'level_name' => $record['level_name'], | ||
'message' => $record['message'], | ||
'context' => json_encode($record['context']), | ||
'extra' => json_encode($record['extra']), | ||
'datetime' => $record['datetime']->format('Y-m-d G:i:s'), | ||
]; | ||
$this->statement->execute($content); | ||
} | ||
} |