Fixed logging (Now logging autostarting without special loggerconfig.php). Issue #59.

This commit is contained in:
Dmitry Khomutov 2017-05-23 23:17:39 +07:00
parent 5a761dcddb
commit 7e2f63142d
No known key found for this signature in database
GPG key ID: 7EB36C9576F9ECB9
6 changed files with 9 additions and 95 deletions

1
.gitignore vendored
View file

@ -1,6 +1,5 @@
/vendor
/composer.phar
/runtime
/app/loggerconfig.php
/app/config.yml
/public/assets/vendor

View file

@ -1,16 +0,0 @@
<?php
return [
/** Loggers attached to every command */
"_" => function() {
return [
new \Monolog\Handler\StreamHandler(__DIR__ . DIRECTORY_SEPARATOR . 'errors.log', \Monolog\Logger::ERROR),
];
},
/** Loggers for the RunCommand */
'RunCommand' => function() {
return [
new \Monolog\Handler\RotatingFileHandler(__DIR__ . DIRECTORY_SEPARATOR . 'everything', 3, \Monolog\Logger::DEBUG),
];
},
];

View file

@ -9,7 +9,6 @@ Getting Started
* [Run builds using a worker](workers/worker.md)
* [Run builds using cronjob](workers/cron.md)
* [Adding PHP Censor Support to Your Projects](configuring_project.md)
* [Setting up Logging](logging.md)
* Updating PHP Censor (See [README](../../README.md))
* [Configuring PHP Censor](configuring.md)

View file

@ -1,59 +0,0 @@
Setting up Logging
==================
Basics
------
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.
How to Setup Logging (For people running a PHP Censor instance)
---------------------------------------------------------------
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
return [
/** Loggers attached to every command */
"_" => function () {
return [
new \Monolog\Handler\StreamHandler('path/to/log', \Monolog\Logger::ERROR),
];
}
];
```
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.
Once this file is created all plugins and core PHP Censor functionality should start writing to the configured handlers.
How to write to the Log (For people creating a new plugin)
----------------------------------------------------------
### Using the plugin constructor to get a logger directly
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
{
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");
}
}
```
### Using convenience methods provided by the Builder
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.

View file

@ -4,6 +4,8 @@ namespace PHPCensor\Console;
use b8\Config;
use b8\Store\Factory;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use PHPCensor\Command\CreateAdminCommand;
use PHPCensor\Command\CreateBuildCommand;
use PHPCensor\Command\InstallCommand;
@ -40,7 +42,13 @@ class Application extends BaseApplication
{
parent::__construct($name, $version);
$loggerConfig = LoggerConfig::newFromFile(APP_DIR . 'loggerconfig.php');
$loggerConfig = new LoggerConfig([
"_" => function() {
return [
new StreamHandler(RUNTIME_DIR . 'console.log', Logger::DEBUG),
];
}
]);
$applicationConfig = Config::getInstance();
$databaseSettings = $applicationConfig->get('b8.database', []);

View file

@ -13,23 +13,6 @@ class LoggerConfig
private $config;
private $cache = [];
/**
* The filepath is expected to return an array which will be
* passed to the normal constructor.
*
* @param string $filePath
* @return LoggerConfig
*/
public static function newFromFile($filePath)
{
if (file_exists($filePath)) {
$configArray = require($filePath);
} else {
$configArray = [];
}
return new self($configArray);
}
/**
* Each key of the array is the name of a logger. The value of
* each key should be an array or a function that returns an