├── LICENSE ├── composer.json ├── config └── logger.php └── src ├── Facades └── Logger.php ├── LevelAwareLogger.php ├── LoggerServiceProvider.php └── LoggerWrapper.php /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Graham Campbell 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "graham-campbell/logger", 3 | "description": "Provides Multiple Loggers At Once", 4 | "keywords": ["laravel", "framework", "multiple", "logging", "logs", "log", "Logger", "Laravel Logger", "Laravel-Logger", "Graham Campbell", "GrahamCampbell"], 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "Graham Campbell", 9 | "email": "graham@alt-three.com" 10 | } 11 | ], 12 | "require": { 13 | "php": ">=5.5.9", 14 | "graham-campbell/logger-core": "~1.1", 15 | "illuminate/contracts": "5.0.*|5.1.*", 16 | "illuminate/support": "5.0.*|5.1.*", 17 | "psr/log": "~1.0" 18 | }, 19 | "require-dev": { 20 | "graham-campbell/testbench": "~3.0", 21 | "phpunit/phpunit": "^4.7.6" 22 | }, 23 | "autoload": { 24 | "psr-4": { 25 | "GrahamCampbell\\Logger\\": "src/" 26 | } 27 | }, 28 | "autoload-dev": { 29 | "psr-4": { 30 | "GrahamCampbell\\Tests\\Logger\\": "tests/" 31 | } 32 | }, 33 | "config": { 34 | "preferred-install": "dist" 35 | }, 36 | "extra": { 37 | "branch-alias": { 38 | "dev-master": "1.1-dev" 39 | } 40 | }, 41 | "minimum-stability": "dev", 42 | "prefer-stable": true 43 | } 44 | -------------------------------------------------------------------------------- /config/logger.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | return [ 13 | 14 | /* 15 | |-------------------------------------------------------------------------- 16 | | Loggers 17 | |-------------------------------------------------------------------------- 18 | | 19 | | Here are each of the loggers to call under the hood while logging. 20 | | 21 | */ 22 | 23 | 'loggers' => [ 24 | 'Illuminate\Log\Writer' => ['*'], 25 | ], 26 | 27 | ]; 28 | -------------------------------------------------------------------------------- /src/Facades/Logger.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace GrahamCampbell\Logger\Facades; 13 | 14 | use Illuminate\Support\Facades\Facade; 15 | 16 | /** 17 | * This is the logger facade class. 18 | * 19 | * @author Graham Campbell 20 | */ 21 | class Logger extends Facade 22 | { 23 | /** 24 | * Get the registered name of the component. 25 | * 26 | * @return string 27 | */ 28 | protected static function getFacadeAccessor() 29 | { 30 | return 'logger'; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/LevelAwareLogger.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace GrahamCampbell\Logger; 13 | 14 | use GrahamCampbell\LoggerCore\LoggerTrait; 15 | use Illuminate\Contracts\Logging\Log; 16 | use Psr\Log\LoggerInterface; 17 | 18 | /** 19 | * This is the level aware logger class. 20 | * 21 | * @author Graham Campbell 22 | */ 23 | class LevelAwareLogger implements LoggerInterface, Log 24 | { 25 | use LoggerTrait; 26 | 27 | /** 28 | * The underlying logger instance. 29 | * 30 | * @var \Psr\Log\LoggerInterface 31 | */ 32 | protected $logger; 33 | 34 | /** 35 | * The levels to log. 36 | * 37 | * @var string[] 38 | */ 39 | protected $levels; 40 | 41 | /** 42 | * Create a new level aware logger instance. 43 | * 44 | * @param \Psr\Log\LoggerInterface $logger 45 | * @param string[] $levels 46 | * 47 | * @return void 48 | */ 49 | public function __construct(LoggerInterface $logger, array $levels) 50 | { 51 | $this->logger = $logger; 52 | $this->levels = $levels; 53 | } 54 | 55 | /** 56 | * Log a message to the logs. 57 | * 58 | * @param string $level 59 | * @param mixed $message 60 | * @param array $context 61 | * 62 | * @return void 63 | */ 64 | public function log($level, $message, array $context = []) 65 | { 66 | if ($this->shouldLog($level)) { 67 | $this->logger->log($level, $this->formatMessage($message), $context); 68 | } 69 | } 70 | 71 | /** 72 | * Should the message be logged? 73 | * 74 | * @param string $level 75 | * 76 | * @return bool 77 | */ 78 | protected function shouldLog($level) 79 | { 80 | if ($this->levels === ['*'] || $this->levels === ['all']) { 81 | return true; 82 | } 83 | 84 | return in_array($level, $this->levels); 85 | } 86 | 87 | /** 88 | * Register a file log handler. 89 | * 90 | * @param string $path 91 | * @param string $level 92 | * 93 | * @return void 94 | */ 95 | public function useFiles($path, $level = 'debug') 96 | { 97 | if ($this->logger instanceof Log) { 98 | $this->logger->useFiles($path, $level); 99 | } 100 | } 101 | 102 | /** 103 | * Register a daily file log handler. 104 | * 105 | * @param string $path 106 | * @param int $days 107 | * @param string $level 108 | * 109 | * @return void 110 | */ 111 | public function useDailyFiles($path, $days = 0, $level = 'debug') 112 | { 113 | if ($this->logger instanceof Log) { 114 | $this->logger->useDailyFiles($path, $days, $level); 115 | } 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /src/LoggerServiceProvider.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace GrahamCampbell\Logger; 13 | 14 | use Illuminate\Contracts\Foundation\Application; 15 | use Illuminate\Contracts\Logging\Log; 16 | use Illuminate\Support\ServiceProvider; 17 | use Psr\Log\LoggerInterface; 18 | 19 | /** 20 | * This is the logger service provider class. 21 | * 22 | * @author Graham Campbell 23 | */ 24 | class LoggerServiceProvider extends ServiceProvider 25 | { 26 | /** 27 | * Boot the service provider. 28 | * 29 | * @return void 30 | */ 31 | public function boot() 32 | { 33 | $this->setupConfig(); 34 | } 35 | 36 | /** 37 | * Setup the config. 38 | * 39 | * @return void 40 | */ 41 | protected function setupConfig() 42 | { 43 | $source = realpath(__DIR__.'/../config/logger.php'); 44 | 45 | if (class_exists('Illuminate\Foundation\Application', false)) { 46 | $this->publishes([$source => config_path('logger.php')]); 47 | } 48 | 49 | $this->mergeConfigFrom($source, 'logger'); 50 | } 51 | 52 | /** 53 | * Register the service provider. 54 | * 55 | * @return void 56 | */ 57 | public function register() 58 | { 59 | $this->registerLogger($this->app); 60 | } 61 | 62 | /** 63 | * Register the logger class. 64 | * 65 | * @param \Illuminate\Contracts\Foundation\Application $app 66 | * 67 | * @return void 68 | */ 69 | protected function registerLogger(Application $app) 70 | { 71 | $app->singleton('logger', function ($app) { 72 | $loggers = []; 73 | 74 | foreach ($app->config->get('logger.loggers', []) as $logger => $levels) { 75 | $loggers[] = new LevelAwareLogger($app->make($logger), (array) $levels); 76 | } 77 | 78 | return new LoggerWrapper($loggers); 79 | }); 80 | 81 | $app->alias('logger', LoggerWrapper::class); 82 | $app->alias('logger', LoggerInterface::class); 83 | $app->alias('logger', Log::class); 84 | } 85 | 86 | /** 87 | * Get the services provided by the provider. 88 | * 89 | * @return string[] 90 | */ 91 | public function provides() 92 | { 93 | return [ 94 | 'logger', 95 | ]; 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /src/LoggerWrapper.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace GrahamCampbell\Logger; 13 | 14 | use GrahamCampbell\LoggerCore\LoggerTrait; 15 | use Illuminate\Contracts\Logging\Log; 16 | use Psr\Log\LoggerInterface; 17 | 18 | /** 19 | * This is the logger wrapper class. 20 | * 21 | * @author Graham Campbell 22 | */ 23 | class LoggerWrapper implements LoggerInterface, Log 24 | { 25 | use LoggerTrait; 26 | 27 | /** 28 | * The registered loggers. 29 | * 30 | * @var \Illuminate\Contracts\Logging\Log[] 31 | */ 32 | protected $loggers; 33 | 34 | /** 35 | * Create a new logger wrapper instance. 36 | * 37 | * @param \Illuminate\Contracts\Logging\Log[] $loggers 38 | * 39 | * @return void 40 | */ 41 | public function __construct(array $loggers) 42 | { 43 | $this->loggers = $loggers; 44 | } 45 | 46 | /** 47 | * Log a message to the logs. 48 | * 49 | * @param string $level 50 | * @param mixed $message 51 | * @param array $context 52 | * 53 | * @return void 54 | */ 55 | public function log($level, $message, array $context = []) 56 | { 57 | foreach ($this->loggers as $logger) { 58 | $logger->log($level, $this->formatMessage($message), $context); 59 | } 60 | } 61 | 62 | /** 63 | * Register a file log handler. 64 | * 65 | * @param string $path 66 | * @param string $level 67 | * 68 | * @return void 69 | */ 70 | public function useFiles($path, $level = 'debug') 71 | { 72 | foreach ($this->loggers as $logger) { 73 | $logger->useFiles($path, $level); 74 | } 75 | } 76 | 77 | /** 78 | * Register a daily file log handler. 79 | * 80 | * @param string $path 81 | * @param int $days 82 | * @param string $level 83 | * 84 | * @return void 85 | */ 86 | public function useDailyFiles($path, $days = 0, $level = 'debug') 87 | { 88 | foreach ($this->loggers as $logger) { 89 | $logger->useDailyFiles($path, $days, $level); 90 | } 91 | } 92 | } 93 | --------------------------------------------------------------------------------