Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Cody Mays committed May 16, 2016
0 parents commit 10019bf
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
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.
43 changes: 43 additions & 0 deletions README.md
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.
19 changes: 19 additions & 0 deletions composer.json
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"}
}
}
92 changes: 92 additions & 0 deletions src/PommPGHandler/PommPGHandler.php
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);
}
}

0 comments on commit 10019bf

Please sign in to comment.