|
1 | | -# Logger |
2 | | -InitPHP Logger Library |
| 1 | +# InitPHP Logger |
| 2 | + |
| 3 | +Logger class in accordance with PSR-3 standards |
| 4 | + |
| 5 | +[](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) |
| 6 | + |
| 7 | +## Features |
| 8 | + |
| 9 | +- Keeping logs to the database with PDO. |
| 10 | +- Printing log records to a file. |
| 11 | +- Logging feature with multiple drivers. |
| 12 | + |
| 13 | +## Requirements |
| 14 | + |
| 15 | +- PHP 5.6 or higher |
| 16 | +- [PSR-3 Interface Package](https://www.php-fig.org/psr/psr-3/) |
| 17 | +- PDO Extension (Only `PDOLogger`) |
| 18 | + |
| 19 | +## Installation |
| 20 | + |
| 21 | +``` |
| 22 | +composer require initphp/logger |
| 23 | +``` |
| 24 | + |
| 25 | +## Using |
| 26 | + |
| 27 | +### FileLogger |
| 28 | + |
| 29 | +```php |
| 30 | +require_once "vendor/autoload.php"; |
| 31 | +use \InitPHP\Logger\Logger; |
| 32 | +use \InitPHP\Logger\FileLogger; |
| 33 | + |
| 34 | +$logFile = __DIR__ . '/logfile.log'; |
| 35 | + |
| 36 | +$logger = new Logger(new FileLogger($logFile)); |
| 37 | +``` |
| 38 | + |
| 39 | +### PdoLogger |
| 40 | + |
| 41 | +```php |
| 42 | +require_once "vendor/autoload.php"; |
| 43 | +use \InitPHP\Logger\Logger; |
| 44 | +use \InitPHP\Logger\PDOLogger; |
| 45 | + |
| 46 | +$table = 'logs'; |
| 47 | +$pdo = new \PDO('mysql:dbname=project;host=localhost', 'root', ''); |
| 48 | + |
| 49 | +$logger = new Logger(new PDOLogger($pdo, $table)); |
| 50 | + |
| 51 | +$logger->error('User {user} caused an error.', array('user' => 'muhametsafak')); |
| 52 | +// INSERT INTO logs (level, message, date) VALUES ('ERROR', 'User muhametsafak caused an error.', '2022-03-11 13:05:45') |
| 53 | +``` |
| 54 | + |
| 55 | +You can use the following SQL statement to create a sample MySQL table. |
| 56 | + |
| 57 | +```sql |
| 58 | +CREATE TABLE `logs` ( |
| 59 | + `level` ENUM('EMERGENCY','ALERT','CRITICAL','ERROR','WARNING','NOTICE','INFO','DEBUG') NOT NULL, |
| 60 | + `message` TEXT NOT NULL, |
| 61 | + `date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP |
| 62 | +) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_general_ci; |
| 63 | +``` |
| 64 | + |
| 65 | +### Multi Logger |
| 66 | + |
| 67 | +```php |
| 68 | +require_once "vendor/autoload.php"; |
| 69 | +use \InitPHP\Logger\Logger; |
| 70 | +use \InitPHP\Logger\PDOLogger; |
| 71 | +use \InitPHP\Logger\FileLogger; |
| 72 | + |
| 73 | +$logFile = __DIR__ . '/logfile.log'; |
| 74 | + |
| 75 | +$table = 'logs'; |
| 76 | +$pdo = new \PDO('mysql:dbname=project;host=localhost', 'root', ''); |
| 77 | + |
| 78 | +$logger = new Logger(new FileLogger($logFile), new PDOLogger($pdo, $table)); |
| 79 | +``` |
| 80 | + |
| 81 | +## Methods |
| 82 | + |
| 83 | +```php |
| 84 | +public function emergency(string $msg, array $context = array()): void; |
| 85 | + |
| 86 | +public function alert(string $msg, array $context = array()): void; |
| 87 | + |
| 88 | +public function critical(string $msg, array $context = array()): void; |
| 89 | + |
| 90 | +public function error(string $msg, array $context = array()): void; |
| 91 | + |
| 92 | +public function warning(string $msg, array $context = array()): void; |
| 93 | + |
| 94 | +public function notice(string $msg, array $context = array()): void; |
| 95 | + |
| 96 | +public function info(string $msg, array $context = array()): void; |
| 97 | + |
| 98 | +public function debug(string $msg, array $context = array()): void; |
| 99 | + |
| 100 | +public function log(string $level, string $msg, array $context = array()): void; |
| 101 | +``` |
| 102 | + |
| 103 | +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. |
| 104 | + |
| 105 | +**Example 1 :** |
| 106 | + |
| 107 | +```php |
| 108 | +$logger->emergency("Something went wrong"); |
| 109 | +``` |
| 110 | + |
| 111 | +It prints an output like this to the log file. |
| 112 | + |
| 113 | +``` |
| 114 | +2021-09-29T13:34:47+02:00 [EMERGENCY] Something went wrong |
| 115 | +``` |
| 116 | + |
| 117 | +**Example 2:** |
| 118 | + |
| 119 | +```php |
| 120 | +$logger->error("User {username} caused an error.", ["username" => "john"]); |
| 121 | +``` |
| 122 | + |
| 123 | +It prints an output like this to the log file. |
| 124 | + |
| 125 | +``` |
| 126 | +2021-09-29T13:34:47+02:00 [ERROR] User john caused an error. |
| 127 | +``` |
| 128 | + |
| 129 | +That is all. |
| 130 | + |
| 131 | +*** |
| 132 | + |
| 133 | +## Getting Help |
| 134 | + |
| 135 | +If you have questions, concerns, bug reports, etc, please file an issue in this repository's Issue Tracker. |
| 136 | + |
| 137 | +## Credits |
| 138 | + |
| 139 | +- [Muhammet ŞAFAK](https://www.muhammetsafak.com.tr) |
| 140 | + |
| 141 | +## License |
| 142 | + |
| 143 | +Copyright © 2022 [MIT License](./LICENSE) |
0 commit comments