Skip to content

Commit

Permalink
Clean up log component. Introduce MessageLogged event class.
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Dec 20, 2016
1 parent e83703e commit 57c82d0
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 17 deletions.
42 changes: 42 additions & 0 deletions src/Illuminate/Log/Events/MessageLogged.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Illuminate\Log\Events;

class MessageLogged
{
/**
* The log "level".
*
* @var string
*/
public $level;

/**
* The log message.
*
* @var string
*/
public $message;

/**
* The log context.
*
* @var array
*/
public $context;

/**
* Create a new event instance.
*
* @param string $level
* @param string $message
* @param array $context
* @return void
*/
public function __construct($level, $message, array $context = [])
{
$this->level = $level;
$this->message = $message;
$this->context = $context;
}
}
44 changes: 29 additions & 15 deletions src/Illuminate/Log/LogServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,37 +27,37 @@ public function register()
public function createLogger()
{
$log = new Writer(
new Monolog($this->app->bound('env') ? $this->app->environment() : 'production'), $this->app['events']
new Monolog($this->channel()), $this->app['events']
);

if ($this->app->hasMonologConfigurator()) {
call_user_func(
$this->app->getMonologConfigurator(), $log->getMonolog()
);
call_user_func($this->app->getMonologConfigurator(), $log->getMonolog());
} else {
$this->configureHandlers($log);
$this->configureHandler($log);
}

return $log;
}

/**
* Get the name of the log "channel".
*
* @return string
*/
protected function channel()
{
return $this->app->bound('env') ? $this->app->environment() : 'production';
}

/**
* Configure the Monolog handlers for the application.
*
* @param \Illuminate\Log\Writer $log
* @return void
*/
protected function configureHandlers(Writer $log)
protected function configureHandler(Writer $log)
{
if ($this->app->bound('config')) {
$handler = $this->app->make('config')->get('app.log');
} else {
$handler = 'single';
}

$method = 'configure'.ucfirst($handler).'Handler';

$this->{$method}($log);
$this->{'configure'.ucfirst($this->handler()).'Handler'}($log);
}

/**
Expand Down Expand Up @@ -110,6 +110,20 @@ protected function configureErrorlogHandler(Writer $log)
$log->useErrorLog($this->logLevel());
}

/**
* Get the default log handler.
*
* @return string
*/
protected function handler()
{
if ($this->app->bound('config')) {
return $this->app->make('config')->get('app.log');
}

return 'single';
}

/**
* Get the log level for the application.
*
Expand Down
5 changes: 3 additions & 2 deletions src/Illuminate/Log/Writer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Monolog\Handler\ErrorLogHandler;
use Monolog\Logger as MonologLogger;
use Monolog\Handler\RotatingFileHandler;
use Illuminate\Log\Events\MessageLogged;
use Illuminate\Contracts\Support\Jsonable;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\Support\Arrayable;
Expand Down Expand Up @@ -275,7 +276,7 @@ public function listen(Closure $callback)
throw new RuntimeException('Events dispatcher has not been set.');
}

$this->dispatcher->listen('illuminate.log', $callback);
$this->dispatcher->listen(MessageLogged::class, $callback);
}

/**
Expand All @@ -292,7 +293,7 @@ protected function fireLogEvent($level, $message, array $context = [])
// log listeners. These are useful for building profilers or other tools
// that aggregate all of the log messages for a given "request" cycle.
if (isset($this->dispatcher)) {
$this->dispatcher->fire('illuminate.log', compact('level', 'message', 'context'));
$this->dispatcher->fire(new MessageLogged($level, $message, $context));
}
}

Expand Down

2 comments on commit 57c82d0

@martindilling
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is also a breaking change (has broken the laravel debugbar).
Didn't see any comment about it being a breaking change in the commit message as I've seen in some other commits, so just a friendly reminder just in case ;)

@GrahamCampbell
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Laravel does not follow semver, so expect breaking changes. All will be documented before the 5.4 release. :)

Please sign in to comment.