├── src ├── InvalidArgumentException.php ├── LoggerAwareInterface.php ├── LogLevel.php ├── LoggerAwareTrait.php ├── AbstractLogger.php ├── NullLogger.php ├── LoggerTrait.php └── LoggerInterface.php ├── composer.json ├── LICENSE └── README.md /src/InvalidArgumentException.php: -------------------------------------------------------------------------------- 1 | logger = $logger; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/AbstractLogger.php: -------------------------------------------------------------------------------- 1 | =8.0.0" 15 | }, 16 | "autoload": { 17 | "psr-4": { 18 | "Psr\\Log\\": "src" 19 | } 20 | }, 21 | "extra": { 22 | "branch-alias": { 23 | "dev-master": "3.x-dev" 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/NullLogger.php: -------------------------------------------------------------------------------- 1 | logger) { }` 11 | * blocks. 12 | */ 13 | class NullLogger extends AbstractLogger 14 | { 15 | /** 16 | * Logs with an arbitrary level. 17 | * 18 | * @param mixed[] $context 19 | * 20 | * @throws \Psr\Log\InvalidArgumentException 21 | */ 22 | public function log($level, string|\Stringable $message, array $context = []): void 23 | { 24 | // noop 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2012 PHP Framework Interoperability Group 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | PSR Log 2 | ======= 3 | 4 | This repository holds all interfaces/classes/traits related to 5 | [PSR-3](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md). 6 | 7 | Note that this is not a logger of its own. It is merely an interface that 8 | describes a logger. See the specification for more details. 9 | 10 | Installation 11 | ------------ 12 | 13 | ```bash 14 | composer require psr/log 15 | ``` 16 | 17 | Usage 18 | ----- 19 | 20 | If you need a logger, you can use the interface like this: 21 | 22 | ```php 23 | logger = $logger; 34 | } 35 | 36 | public function doSomething() 37 | { 38 | if ($this->logger) { 39 | $this->logger->info('Doing work'); 40 | } 41 | 42 | try { 43 | $this->doSomethingElse(); 44 | } catch (Exception $exception) { 45 | $this->logger->error('Oh no!', array('exception' => $exception)); 46 | } 47 | 48 | // do something useful 49 | } 50 | } 51 | ``` 52 | 53 | You can then pick one of the implementations of the interface to get a logger. 54 | 55 | If you want to implement the interface, you can require this package and 56 | implement `Psr\Log\LoggerInterface` in your code. Please read the 57 | [specification text](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md) 58 | for details. 59 | -------------------------------------------------------------------------------- /src/LoggerTrait.php: -------------------------------------------------------------------------------- 1 | log(LogLevel::EMERGENCY, $message, $context); 21 | } 22 | 23 | /** 24 | * Action must be taken immediately. 25 | * 26 | * Example: Entire website down, database unavailable, etc. This should 27 | * trigger the SMS alerts and wake you up. 28 | */ 29 | public function alert(string|\Stringable $message, array $context = []): void 30 | { 31 | $this->log(LogLevel::ALERT, $message, $context); 32 | } 33 | 34 | /** 35 | * Critical conditions. 36 | * 37 | * Example: Application component unavailable, unexpected exception. 38 | */ 39 | public function critical(string|\Stringable $message, array $context = []): void 40 | { 41 | $this->log(LogLevel::CRITICAL, $message, $context); 42 | } 43 | 44 | /** 45 | * Runtime errors that do not require immediate action but should typically 46 | * be logged and monitored. 47 | */ 48 | public function error(string|\Stringable $message, array $context = []): void 49 | { 50 | $this->log(LogLevel::ERROR, $message, $context); 51 | } 52 | 53 | /** 54 | * Exceptional occurrences that are not errors. 55 | * 56 | * Example: Use of deprecated APIs, poor use of an API, undesirable things 57 | * that are not necessarily wrong. 58 | */ 59 | public function warning(string|\Stringable $message, array $context = []): void 60 | { 61 | $this->log(LogLevel::WARNING, $message, $context); 62 | } 63 | 64 | /** 65 | * Normal but significant events. 66 | */ 67 | public function notice(string|\Stringable $message, array $context = []): void 68 | { 69 | $this->log(LogLevel::NOTICE, $message, $context); 70 | } 71 | 72 | /** 73 | * Interesting events. 74 | * 75 | * Example: User logs in, SQL logs. 76 | */ 77 | public function info(string|\Stringable $message, array $context = []): void 78 | { 79 | $this->log(LogLevel::INFO, $message, $context); 80 | } 81 | 82 | /** 83 | * Detailed debug information. 84 | */ 85 | public function debug(string|\Stringable $message, array $context = []): void 86 | { 87 | $this->log(LogLevel::DEBUG, $message, $context); 88 | } 89 | 90 | /** 91 | * Logs with an arbitrary level. 92 | * 93 | * @param mixed $level 94 | * 95 | * @throws \Psr\Log\InvalidArgumentException 96 | */ 97 | abstract public function log($level, string|\Stringable $message, array $context = []): void; 98 | } 99 | -------------------------------------------------------------------------------- /src/LoggerInterface.php: -------------------------------------------------------------------------------- 1 |