Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
robinsonjohn committed Aug 18, 2023
0 parents commit 70bbc5f
Show file tree
Hide file tree
Showing 10 changed files with 335 additions and 0 deletions.
44 changes: 44 additions & 0 deletions .github/ISSUE_TEMPLATE/Bug.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
name: 🛠️ Bug
about: Does something need fixed?
---

# Bug Report

## Description

<!-- Provide a brief description of the issue. -->

## Expected behavior

<!-- A summary of what you expected, should this issue not have arisen. -->

## Current behavior

<!-- A summary of what happened due to this issue as opposed to the expected behavior. -->

## Possible solution

<!-- Optional possible solution/fix to this issue -->

## Steps to reproduce

<!--
Can provide links to a live example, code snippets,
screenshots, or simple steps to reproduce this issue.
-->

1.
2.
3.

## Environment

<!--
Details of the environment which experienced this issue.
e.g., software version, OS, browser, language, etc.
-->

## Additional information

<!--- Optional additional information/details. -->
22 changes: 22 additions & 0 deletions .github/ISSUE_TEMPLATE/Feature_Request.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
name: 💡 Feature Request
about: Should something great be added?
---

# Feature Request

## Description

<!-- Provide a brief description of the proposed feature. -->

## Scenario / Use-case

<!-- Provide an explanation how/when the feature would be helpful. -->

## Possible solution

<!-- Optional possible solution/fix to this issue -->

## Additional information

<!--- Optional additional information/details. -->
8 changes: 8 additions & 0 deletions .github/ISSUE_TEMPLATE/Question.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
name: ❓ Question
about: Does something need clarified?
---

# Question

<!-- Provide a description of the question. -->
5 changes: 5 additions & 0 deletions .github/SECURITY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Security Policy

**PLEASE DO NOT DISCLOSE SECURITY-RELATED ISSUES PUBLICLY**

If you discover a security vulnerability, please send an email to [email protected]. All security vulnerabilities will be promptly addressed.
32 changes: 32 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Project-specific

*.log

# Local files

_local

# Dotenv

.env
.env.*
!.env.example

# Dependencies

/node_modules
/vendor
composer.lock

# IDE's

.idea
.vscode

# OS

.DS_Store
.Spotlight-V100
.Trashes
ehthumbs.db
Thumbs.db
20 changes: 20 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

- `[Unreleased]` for upcoming features.
- `Added` for new features.
- `Changed` for changes in existing functionality.
- `Deprecated` for soon-to-be removed features.
- `Removed` for now removed features.
- `Fixed` for any bug fixes.
- `Security` in case of vulnerabilities

## [1.0.0] - 2023.08.18

### Added

- Initial release.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Bayfront Media

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.
64 changes: 64 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
## Monolog PDO

[Monolog](https://github.com/Seldaek/monolog) handler used to write log files to a MySQL database.

- [License](#license)
- [Author](#author)
- [Requirements](#requirements)
- [Installation](#installation)
- [Usage](#usage)

## License

This project is open source and available under the [MIT License](LICENSE).

## Author

<img src="https://cdn1.onbayfront.com/bfm/brand/bfm-logo.svg" alt="Bayfront Media" width="250" />

- [Bayfront Media homepage](https://www.bayfrontmedia.com?utm_source=github&amp;utm_medium=direct)
- [Bayfront Media GitHub](https://github.com/bayfrontmedia)

## Requirements

* PHP >= 8.0
* `PDO` PHP extension

## Installation

```
composer require bayfrontmedia/monolog-pdo
```

## Usage

Before pushing this handler to a `Logger` instance, you must first create the necessary database table to store the records.

The table can be created using the `up` method, and removed using the `down` method.

The constructor requires a `PDO` instance, and the table name you wish to use:

```php
use Bayfront\MonologPDO\PDOHandler;

/** @var PDO $pdo */
$handler = new PDOHandler($pdo, 'table_name');

$handler->up();
```

Once the table has been created, the handler can be pushed to a `Logger` instance:

```php
use Bayfront\MonologPDO\PDOHandler;
use Monolog\Logger;

$log = new Logger('channel_name');

/** @var PDO $pdo */
$handler = new PDOHandler($pdo, 'table_name');

$log->pushHandler($handler);
```

From here, your log records should appear in the MySQL table.
39 changes: 39 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "bayfrontmedia/monolog-pdo",
"description": "Monolog handler used to write log files to a MySQL database.",
"homepage": "https://github.com/bayfrontmedia/monolog-pdo",
"license" : "MIT",
"type": "library",
"keywords": [
"monolog",
"log",
"logger",
"mysql",
"pdo"
],
"authors": [
{
"name": "John Robinson",
"email": "[email protected]",
"homepage": "https://www.bayfrontmedia.com"
}
],
"support": {
"issues": "https://github.com/bayfrontmedia/monolog-pdo/issues"
},
"config": {
"optimize-autoloader": true,
"preferred-install": "dist"
},
"require": {
"php": ">=8.0",
"ext-pdo": "*",
"bayfrontmedia/php-array-helpers": "^2.0",
"monolog/monolog": "^3.4"
},
"autoload": {
"psr-4": {
"Bayfront\\MonologPDO\\": "src/"
}
}
}
80 changes: 80 additions & 0 deletions src/PDOHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

namespace Bayfront\MonologPDO;

use Bayfront\ArrayHelpers\Arr;
use Monolog\Handler\AbstractProcessingHandler;
use Monolog\Level;
use Monolog\LogRecord;
use PDO;

class PDOHandler extends AbstractProcessingHandler
{

private PDO $pdo;
private string $table_name;

public function __construct(PDO $pdo, string $table_name = 'logs', int|string|Level $level = Level::Debug, bool $bubble = true)
{
$this->pdo = $pdo;
$this->table_name = $table_name;
parent::__construct($level, $bubble);
}

/**
* @inheritDoc
*/
protected function write(LogRecord $record): void
{

$arr = $record->toArray();

$sql = "INSERT INTO $this->table_name (channel, level, levelName, message, context, extra, createdAt) VALUES (?,?,?,?,?,?,?)";
$stmt = $this->pdo->prepare($sql);
$stmt->execute([
$record->channel,
Arr::get($arr, 'level'),
Arr::get($arr, 'level_name'),
$record->message,
empty($record->context) ? null : json_encode($record->context),
empty($record->extra) ? null : json_encode($record->extra),
$record->datetime->format('Y-m-d H:i:s')
]);

}

/**
* Create database table for handler.
*
* @return void
*/
public function up(): void
{

$query = $this->pdo->prepare("CREATE TABLE IF NOT EXISTS $this->table_name (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
`channel` VARCHAR(255),
`level` INT(3),
`levelName` VARCHAR(10),
`message` TEXT,
`context` JSON,
`extra` JSON,
`createdAt` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci");

$query->execute();

}

/**
* Remove database table for handler.
*
* @return void
*/
public function down(): void
{
$query = $this->pdo->prepare("DROP TABLE IF EXISTS $this->table_name");
$query->execute();
}

}

0 comments on commit 70bbc5f

Please sign in to comment.