Skip to content
This repository has been archived by the owner on Dec 9, 2020. It is now read-only.

Many changes #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 59 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
# Laravel Multilog

This project is a fork of [karlomikus/multilog](https://github.com/karlomikus/multilog)

[![Build Status](https://travis-ci.org/karlomikus/multilog.svg?branch=master)](https://travis-ci.org/karlomikus/multilog)
[![Latest Stable Version](https://poser.pugx.org/karlomikus/multilog/v/stable)](https://packagist.org/packages/karlomikus/multilog)
[![License](https://poser.pugx.org/karlomikus/multilog/license)](https://packagist.org/packages/karlomikus/multilog)

Easily add multiple monolog channels to your Laravel 5.2.* application.
Easily add multiple Monolog channels to your Laravel or Lumen application.

## Install

Via Composer

``` bash
$ composer require karlomikus/multilog
composer require karlomikus/multilog
```

Or add the package to your composer file:

``` json
"karlomikus/multilog": "1.*"
"karlomikus/multilog": "2.*"
```

Next register service provider and facade in your `config/app.php` file:
Expand All @@ -29,38 +31,46 @@ Karlomikus\Multilog\MultilogServiceProvider::class
'Multilog' => Karlomikus\Multilog\Facade::class
```

And finally publish the config file:
## Configuration

With Laravel, you can publish the configuration file with:

``` bash
$ php artisan vendor:publish
php artisan vendor:publish
```

## Configuration
If you are using Lumen, please copy the example of
[configuration file](src/Multilog/config/multilog.php)
to your application `config` directory. Then add the following line to
`bootstrap/app.php`:

```php
$app->configure('multilog');
```

All your channels are defined in `config/multilog.php` file.

By default you have two channels (request and info):

``` php
// Request channel
'request' => [
'stream' => 'request.log',
'daily' => true,
'format' => [
'date' => 'Y-m-d H:i:s',
'output' => "[%datetime%] %message% %context% %extra%\n",
],
],
'request' => function ($channel) {
$logger = new Logger($channel);
...
return $logger;
},
// Info channel
'info' => [
'stream' => 'info.log',
'daily' => false
]
'info' => function ($channel) {
$logger = new Logger($channel);
...
return $logger;
}
```

## Usage

Using dependency injection:

``` php
use Karlomikus\Multilog\Contracts\MultilogInterface;

Expand All @@ -75,19 +85,49 @@ public function __construct(MultilogInterface $multilog)
```

Using facade:

``` php
Multilog::channel('channel-name')->info('Information here...');

// Channel shorthand is also available
Multilog::c('channel-name')->warning('Warning here...');
```

## Redirecting Log facade calls to a Multilog channel

If you wanna make sure Multilog is used, you can redirect all Log facade calls
to Multilog by adding the following service provider:

```php
// Service provider
Karlomikus\Multilog\LogServiceProvider::class
```

Then configure the default channel in your `config/multilog.php` file:

```php
'defaultChannel' => 'global',
'channels' => [
'request' => function ($channel) {
$logger = new Logger($channel);
...
return $logger;
},
]
```

## Change log

### [2.0.0] - 2017-08-28

* Change configuration format to use closures
* Compatibility with Lumen
* Possibility to redirect `Log` calls to a `Multilog` default channel

### [1.0.0] - 2016-03-06

* Initial release

## License

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
9 changes: 5 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@
}
],
"require": {
"php" : "~5.5",
"monolog/monolog": "~1.11"
"php" : ">=7.1",
"monolog/monolog": ">=1.11",
"psr/log": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "5.*",
"mockery/mockery": "~0.9.2",
"illuminate/contracts": "5.2.*",
"illuminate/support": "5.2.*"
"illuminate/contracts": "5.*",
"illuminate/support": "5.*"
},
"autoload": {
"psr-4": {
Expand Down
161 changes: 161 additions & 0 deletions src/Multilog/LogRedirect.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
<?php
namespace Karlomikus\Multilog;

use Illuminate\Contracts\Config\Repository;
use Psr\Log\LoggerInterface;
use Karlomikus\Multilog\Contracts\MultilogInterface;

class LogRedirect implements LoggerInterface
{
protected $config;
protected $multilog;

/**
* Create a new log writer instance.
*
* @param \Illuminate\Contracts\Config\Repository $config
* @param \Karlomikus\Multilog\Contracts\MultilogInterface $multilog
* @return void
*/
public function __construct(Repository $config, MultilogInterface $multilog)
{
$this->config = $config;
$this->multilog = $multilog;
}

/**
* Log an alert message to the logs.
*
* @param string $message
* @param array $context
* @return void
*/
public function alert($message, array $context = [])
{
$this->logWithDefaultChannel('alert', $message, $context);
}

/**
* Log a critical message to the logs.
*
* @param string $message
* @param array $context
* @return void
*/
public function critical($message, array $context = [])
{
$this->logWithDefaultChannel('critical', $message, $context);
}

/**
* Log an error message to the logs.
*
* @param string $message
* @param array $context
* @return void
*/
public function error($message, array $context = [])
{
$this->logWithDefaultChannel('error', $message, $context);
}

/**
* Log a warning message to the logs.
*
* @param string $message
* @param array $context
* @return void
*/
public function warning($message, array $context = [])
{
$this->logWithDefaultChannel('warning', $message, $context);
}

/**
* Log a notice to the logs.
*
* @param string $message
* @param array $context
* @return void
*/
public function notice($message, array $context = [])
{
$this->logWithDefaultChannel('notice', $message, $context);
}

/**
* Log an informational message to the logs.
*
* @param string $message
* @param array $context
* @return void
*/
public function info($message, array $context = [])
{
$this->logWithDefaultChannel('info', $message, $context);
}

/**
* Log a debug message to the logs.
*
* @param string $message
* @param array $context
* @return void
*/
public function debug($message, array $context = [])
{
$this->logWithDefaultChannel('debug', $message, $context);
}

/**
* Log a message to the logs.
*
* @param string $level
* @param string $message
* @param array $context
* @return void
*/
public function log($level, $message, array $context = [])
{
$this->logWithDefaultChannel($level, $message, $context);
}

/**
* Register a file log handler.
*
* @param string $path
* @param string $level
* @return void
*/
public function useFiles($path, $level = 'debug')
{
throw new \Exception('useFiles() cannot be called with MonologRedirection');
}

/**
* Register a daily file log handler.
*
* @param string $path
* @param int $days
* @param string $level
* @return void
*/
public function useDailyFiles($path, $days = 0, $level = 'debug')
{
throw new \Exception('useDailyFiles() cannot be called with MonologRedirection');
}

/**
* Proxy message through Multilog default channel
*
* @param string $level
* @param string $message
* @param array $context
* @return void
*/
protected function logWithDefaultChannel($level, $message, array $context = [])
{
$channel = $this->config->get('multilog.defaultChannel');
$this->multilog->channel($channel)->$level($message, $context);
}
}
24 changes: 24 additions & 0 deletions src/Multilog/LogServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
namespace Karlomikus\Multilog;

use Illuminate\Support\ServiceProvider;

class LogServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->singleton('log', function () {
return $this->createLogger();
});
}

/**
* Create the logger.
*
* @return \Illuminate\LogRedirect
*/
public function createLogger()
{
return $this->app->make(LogRedirect::class);
}
}
Loading