https://github.com/initphp/logger
Library developed for logging to file or database in PSR-3 standards.
https://github.com/initphp/logger
logger php php-logger php-logging php-logging-library psr-3
Last synced: 20 days ago
JSON representation
Library developed for logging to file or database in PSR-3 standards.
- Host: GitHub
- URL: https://github.com/initphp/logger
- Owner: InitPHP
- License: mit
- Created: 2022-03-15T09:25:25.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2023-12-15T17:30:50.000Z (over 2 years ago)
- Last Synced: 2025-10-06T17:59:09.927Z (6 months ago)
- Topics: logger, php, php-logger, php-logging, php-logging-library, psr-3
- Language: PHP
- Homepage:
- Size: 5.86 KB
- Stars: 2
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# InitPHP Logger
Logger class in accordance with PSR-3 standards
[](https://packagist.org/packages/initphp/logger) [](https://packagist.org/packages/initphp/logger) [](https://packagist.org/packages/initphp/logger) [](https://packagist.org/packages/initphp/logger) [](https://packagist.org/packages/initphp/logger)
## Features
- Keeping logs to the database with PDO.
- Printing log records to a file.
- Logging feature with multiple drivers.
## Requirements
- PHP 5.6 or higher
- [PSR-3 Interface Package](https://www.php-fig.org/psr/psr-3/)
- PDO Extension (Only `PDOLogger`)
## Installation
```
composer require initphp/logger
```
## Using
### FileLogger
```php
require_once "vendor/autoload.php";
use \InitPHP\Logger\Logger;
use \InitPHP\Logger\FileLogger;
$logFile = __DIR__ . '/logfile.log';
$logger = new Logger(new FileLogger(['path' => $logFile]));
```
### PdoLogger
```php
require_once "vendor/autoload.php";
use \InitPHP\Logger\Logger;
use \InitPHP\Logger\PDOLogger;
$table = 'logs';
$pdo = new \PDO('mysql:dbname=project;host=localhost', 'root', '');
$logger = new Logger(new PDOLogger(['pdo' => $pdo, 'table' => $table]));
$logger->error('User {user} caused an error.', array('user' => 'muhametsafak'));
// INSERT INTO logs (level, message, date) VALUES ('ERROR', 'User muhametsafak caused an error.', '2022-03-11 13:05:45')
```
You can use the following SQL statement to create a sample MySQL table.
```sql
CREATE TABLE `logs` (
`level` ENUM('EMERGENCY','ALERT','CRITICAL','ERROR','WARNING','NOTICE','INFO','DEBUG') NOT NULL,
`message` TEXT NOT NULL,
`date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_general_ci;
```
### Multi Logger
```php
require_once "vendor/autoload.php";
use \InitPHP\Logger\Logger;
use \InitPHP\Logger\PDOLogger;
use \InitPHP\Logger\FileLogger;
$logFile = __DIR__ . '/logfile.log';
$table = 'logs';
$pdo = new \PDO('mysql:dbname=project;host=localhost', 'root', '');
$logger = new Logger(new FileLogger(['path' => $logFile]), new PDOLogger(['pdo' => $pdo, 'table' => $table]));
```
## Methods
```php
public function emergency(string $msg, array $context = array()): void;
public function alert(string $msg, array $context = array()): void;
public function critical(string $msg, array $context = array()): void;
public function error(string $msg, array $context = array()): void;
public function warning(string $msg, array $context = array()): void;
public function notice(string $msg, array $context = array()): void;
public function info(string $msg, array $context = array()): void;
public function debug(string $msg, array $context = array()): void;
public function log(string $level, string $msg, array $context = array()): void;
```
All of the above methods are used the same way, except for the `log()` method. You can use the `log()` method for your own custom error levels.
**Example 1 :**
```php
$logger->emergency("Something went wrong");
```
It prints an output like this to the log file.
```
2021-09-29T13:34:47+02:00 [EMERGENCY] Something went wrong
```
**Example 2:**
```php
$logger->error("User {username} caused an error.", ["username" => "john"]);
```
It prints an output like this to the log file.
```
2021-09-29T13:34:47+02:00 [ERROR] User john caused an error.
```
That is all.
***
## Getting Help
If you have questions, concerns, bug reports, etc, please file an issue in this repository's Issue Tracker.
## Credits
- [Muhammet ŞAFAK](https://www.muhammetsafak.com.tr)
## License
Copyright © 2022 [MIT License](./LICENSE)