php-censor/docs/en/logging.md

60 lines
2.3 KiB
Markdown
Raw Normal View History

2016-07-19 11:12:28 +02:00
Setting up Logging
2017-01-04 13:22:20 +01:00
==================
2016-07-19 11:12:28 +02:00
Basics
2017-01-04 13:22:20 +01:00
------
2016-07-19 11:12:28 +02:00
2016-07-21 19:02:11 +02:00
The PHP Censor codebase makes use of the [PSR3](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md) logging standard. By default we use [Monolog](https://github.com/Seldaek/monolog) to handle the actual work implementing this standard.
2016-07-17 16:20:35 +02:00
2016-07-19 13:05:02 +02:00
How to Setup Logging (For people running a PHP Censor instance)
2017-01-04 13:22:20 +01:00
---------------------------------------------------------------
2016-07-19 11:12:28 +02:00
2016-07-17 16:20:35 +02:00
The only step required to activate logging is to create a file in the root directory called loggerconfig.php with content like the following:
```php
<?php
2016-08-25 18:13:19 +02:00
return [
2016-07-17 16:20:35 +02:00
/** Loggers attached to every command */
"_" => function () {
2016-08-25 18:13:19 +02:00
return [
2016-07-17 16:20:35 +02:00
new \Monolog\Handler\StreamHandler('path/to/log', \Monolog\Logger::ERROR),
2016-08-25 18:13:19 +02:00
];
2016-07-17 16:20:35 +02:00
}
2016-08-25 18:13:19 +02:00
];
2016-07-17 16:20:35 +02:00
```
2016-07-21 19:02:11 +02:00
This file should return an array of key value pairs. Each key tells PHP Censor which command to attach the logger to (the underscore is a special value which matches all commands). For each command an array of [Monolog](https://github.com/Seldaek/monolog) handlers should be returned. In the example above we've used one that simply writes to the file system but in practise this could be any handler written for monolog.
2016-07-17 16:20:35 +02:00
2016-07-21 19:02:11 +02:00
Once this file is created all plugins and core PHP Censor functionality should start writing to the configured handlers.
2016-07-17 16:20:35 +02:00
2016-07-19 11:12:28 +02:00
How to write to the Log (For people creating a new plugin)
2017-01-04 13:22:20 +01:00
----------------------------------------------------------
2016-07-19 11:12:28 +02:00
### Using the plugin constructor to get a logger directly
2016-07-17 16:20:35 +02:00
For plugin creators the simplest way to get hold of an error logger is to add a parameter to the constructor and typehint on 'Psr\Log\LoggerInterface'. The code that loads your plugin will automatically inject the logger when it sees this. For example:
```php
class ExampleLoggingPlugin implements \PHPCensor\Plugin
2016-07-17 16:20:35 +02:00
{
protected $log;
public function __construct(Psr\Log\LoggerInterface $log)
{
$this->log = $log;
}
public function execute()
{
$this->log->notice("You'll notice this in the log");
}
}
```
2016-07-19 11:12:28 +02:00
### Using convenience methods provided by the Builder
2016-07-17 16:20:35 +02:00
Your plugin can also call a couple of messages on the Builder object:
logSuccess()
logFailure()
log()
All calls will get piped through to the appropriate logger.