Skip to content

Commit

Permalink
Merge pull request #1 from gabfr/feat/support-monolog-2
Browse files Browse the repository at this point in the history
Support monolog v. 2
  • Loading branch information
klaussilveira committed Dec 12, 2019
2 parents b3b3f04 + 7fa3b7a commit 0fe8458
Show file tree
Hide file tree
Showing 38 changed files with 475 additions and 241 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ env:
matrix:
include:
- php: 5.3
dist: precise
- php: 5.4
env: COVERALLS=1 PHPCS=1
- php: 5.5
Expand Down
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ Cascade::fileConfig($config);

Then just use your logger as shown below
```php
Cascade::getLogger('myApp')->info('Well, that works!');
Cascade::getLogger('myApp')->error('Maybe not...');
Cascade::getLogger('myLogger')->info('Well, that works!');
Cascade::getLogger('myLogger')->error('Maybe not...');
```

Configuring your loggers
Expand Down Expand Up @@ -222,7 +222,7 @@ You may want to have your Formatters and/or Handlers consume values other than v

```php
self::$extraOptionHandlers = array(
'\Monolog\Formatter\LineFormatter' => array(
'Monolog\Formatter\LineFormatter' => array(
'includeStacktraces' => function ($instance, $include) {
$instance->includeStacktraces($include);
}
Expand Down Expand Up @@ -260,3 +260,8 @@ What's next?
Symfony Users
-------------
You may want to use [MonologBundle](https://github.com/symfony/MonologBundle) as it integrates directly with your favorite framework.


Under The Hood
--------------
Here is a [Medium post](https://medium.com/orchard-technology/enhancing-monolog-699efff1051d#.dw6qu1c2p) if you want to know more about the implementation.
24 changes: 12 additions & 12 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
],
"homepage": "https://github.com/theorchard/monolog-cascade",
"require": {
"php": ">=5.3.9",
"symfony/config": "~2.7 || ^3.1.2",
"symfony/options-resolver": "~2.7 || ^3.1.2",
"symfony/serializer": "~2.7 || ^3.1.2",
"symfony/yaml": "~2.7 || ^3.1.2",
"monolog/monolog": "~1.13"
"php": "^7.0",
"symfony/config": "^2.7 || ^3.0 || ^4.0",
"symfony/options-resolver": "^2.7 || ^3.0 || ^4.0",
"symfony/serializer": "^2.7 || ^3.0 || ^4.0",
"symfony/yaml": "^2.7 || ^3.0 || ^4.0",
"monolog/monolog": "^1.13 || ^2.0"
},
"autoload": {
"psr-4": {
Expand All @@ -31,16 +31,16 @@
}
},
"require-dev": {
"mikey179/vfsStream": "~1.6",
"phpunit/phpcov": "~2.0",
"phpunit/phpunit": "~4.8",
"satooshi/php-coveralls": "~1.0",
"squizlabs/php_codesniffer": "~2.5"
"mikey179/vfsstream": "^1.6",
"phpunit/phpcov": "~4.0.5",
"phpunit/phpunit": "~6.5.14",
"satooshi/php-coveralls": "~2.2",
"squizlabs/php_codesniffer": "~3.0"
},
"prefer-stable": true,
"extra": {
"branch-alias": {
"dev-master": "0.4.x-dev"
"dev-master": "0.5.x-dev"
}
}
}
53 changes: 36 additions & 17 deletions src/Cascade.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,18 @@
*/
namespace Cascade;

use Cascade\Config;
use Cascade\Config\ConfigLoader;
use Monolog\Handler\HandlerInterface;
use Monolog\Logger;
use Monolog\Registry;

use Cascade\Config\ConfigLoader;

/**
* Module class that manages Monolog Logger object
* @see Logger
* @see Registry
*
* @author Raphael Antonmattei <[email protected]>
*
* @see \Monolog\Logger
* @see \Monolog\Registry
*
*/
class Cascade
{
Expand All @@ -36,17 +34,16 @@ class Cascade

/**
* Create a new Logger object and push it to the registry
* @see Logger::__construct
*
* @see Monolog\Logger::__construct
* @throws \InvalidArgumentException if no name is given
*
* @param string $name The logging channel
* @param HandlerInterface[] $handlers Optional stack of handlers, the first one in the array is called first,
* etc.
* @param callable[] $processors Optional array of processors
* @param string $name The logging channel
* @param HandlerInterface[] $handlers Optional stack of handlers, the first
* one in the array is called first, etc.
* @param callable[] $processors Optional array of processors
*
* @throws \InvalidArgumentException: if no name is given
*
* @return Logger newly created Logger
* @return Logger Newly created Logger
*/
public static function createLogger(
$name,
Expand Down Expand Up @@ -93,22 +90,44 @@ public static function logger($name)
/**
* Return the config options
*
* @return array array with configuration options
* @return Config Array with configuration options
*/
public static function getConfig()
{
return self::$config;
}

/**
* Load configuration options from a file or a string
* Load configuration options from a file, a JSON or Yaml string or an array.
*
* @param string $resource path to config file or string or array
* @param string|array $resource Path to config file or configuration as string or array
*/
public static function fileConfig($resource)
{
self::$config = new Config($resource, new ConfigLoader());
self::$config->load();
self::$config->configure();
}

/**
* Load configuration options from a JSON or Yaml string. Alias of fileConfig.
* @see fileConfig
*
* @param string $configString Configuration in string form
*/
public static function loadConfigFromString($configString)
{
self::fileConfig($configString);
}

/**
* Load configuration options from an array. Alias of fileConfig.
* @see fileConfig
*
* @param array $configArray Configuration in array form
*/
public static function loadConfigFromArray($configArray)
{
self::fileConfig($configArray);
}
}
27 changes: 14 additions & 13 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@
*/
namespace Cascade;

use Monolog;

use Cascade\Config\ConfigLoader;
use Cascade\Config\Loader\ClassLoader\FormatterLoader;
use Cascade\Config\Loader\ClassLoader\HandlerLoader;
use Cascade\Config\Loader\ClassLoader\LoggerLoader;
use Cascade\Config\Loader\ClassLoader\ProcessorLoader;
use Monolog\Formatter\FormatterInterface;
use Monolog\Handler\HandlerInterface;
use Monolog\Registry;

/**
* Config class that takes a config resource (file, JSON, Yaml, etc.) and configure Loggers with
Expand All @@ -29,7 +28,7 @@ class Config
{
/**
* Input from user. This is either a file path, a string or an array
* @var string | array
* @var string|array
*/
protected $input = null;

Expand All @@ -41,13 +40,13 @@ class Config

/**
* Array of Formatter objects
* @var FormatterInterface[]
* @var Monolog\Formatter\FormatterInterface[]
*/
protected $formatters = array();

/**
* Array of Handler objects
* @var HandlerInterface[]
* @var Monolog\Handler\HandlerInterface[]
*/
protected $handlers = array();

Expand All @@ -59,7 +58,7 @@ class Config

/**
* Array of logger objects
* @var \Monolog\Logger[]
* @var Monolog\Logger[]
*/
protected $loggers = array();

Expand All @@ -72,7 +71,7 @@ class Config
/**
* Instantiate a Config object
*
* @param string | array $input user input
* @param string|array $input User input
* @param ConfigLoader $loader Config loader object
*/
public function __construct($input, ConfigLoader $loader)
Expand Down Expand Up @@ -100,7 +99,7 @@ public function configure()
}

if ($this->options['disable_existing_loggers']) {
Registry::clear();
Monolog\Registry::clear();
}

if (isset($this->options['formatters'])) {
Expand All @@ -126,7 +125,8 @@ public function configure()

/**
* Configure the formatters
* @param array $formatters array of formatter options
*
* @param array $formatters Array of formatter options
*/
protected function configureFormatters(array $formatters = array())
{
Expand All @@ -138,7 +138,8 @@ protected function configureFormatters(array $formatters = array())

/**
* Configure the handlers
* @param array $handlers array of handler options
*
* @param array $handlers Array of handler options
*/
protected function configureHandlers(array $handlers)
{
Expand All @@ -151,7 +152,7 @@ protected function configureHandlers(array $handlers)
/**
* Configure the processors
*
* @param array $processors array of processor options
* @param array $processors Array of processor options
*/
protected function configureProcessors(array $processors)
{
Expand All @@ -164,7 +165,7 @@ protected function configureProcessors(array $processors)
/**
* Configure the loggers
*
* @param array $loggers array of logger options
* @param array $loggers Array of logger options
*/
protected function configureLoggers(array $loggers)
{
Expand Down
14 changes: 7 additions & 7 deletions src/Config/ConfigLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,22 @@
use Symfony\Component\Config\Loader\DelegatingLoader;
use Symfony\Component\Config\Loader\LoaderResolver;

use Cascade\Config\Loader\PhpArray as ArrayLoader;
use Cascade\Config\Loader\FileLoader\PhpArray as ArrayFromFileLoader;
use Cascade\Config\Loader\FileLoader\Json as JsonLoader;
use Cascade\Config\Loader\FileLoader\PhpArray as ArrayFromFileLoader;
use Cascade\Config\Loader\FileLoader\Yaml as YamlLoader;
use Cascade\Config\Loader\PhpArray as ArrayLoader;

/**
* Loader class that loads Yaml, JSON and array from various resources (file, php array, string)
* @see DelegatingLoader
*
* @author Raphael Antonmattei <[email protected]>
* @see DelegatingLoader
*/
class ConfigLoader extends DelegatingLoader
{
/**
* Locator
* @var \Symfony\Component\Config\FileLocator
* @var FileLocator
*/
protected $locator = null;

Expand All @@ -56,10 +56,10 @@ public function __construct()
/**
* Loads a configuration resource: file, array, string
*
* @param mixed $resource resource to load
* @param mixed $type not used
* @param mixed $resource Resource to load
* @param string|null $type Not used
*
* @return array array of config options
* @return array Array of config options
*/
public function load($resource, $type = null)
{
Expand Down
Loading

0 comments on commit 0fe8458

Please sign in to comment.