Skip to content

Commit

Permalink
fix: add stderr logging (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
eithed authored Aug 24, 2020
1 parent e318d37 commit b22a4a9
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 6 deletions.
11 changes: 11 additions & 0 deletions src/Console/Commands/EventStoreWorker.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,17 @@ public function handle(): void
}
}
}

$errorOutput = $entry['process']->getIncrementalErrorOutput();
if (!empty($errorOutput)) {
foreach (explode(PHP_EOL, $errorOutput) as $line) {
$line = trim($line);

if (!empty($line)) {
(LaravelEventStore::$workerErrorLogger)($line);
}
}
}
}

sleep(1);
Expand Down
24 changes: 22 additions & 2 deletions src/EventStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ class EventStore
*/
public static $workerLogger;

/**
* Variable for logger.
*
* @var callable
*/
public static $workerErrorLogger;

/**
* Variable for logger.
*
Expand Down Expand Up @@ -51,11 +58,24 @@ public static function eventToClass(?callable $callback = null)
*/
public static function workerLogger(?callable $logger = null)
{
static::$workerLogger = $logger ?: function($message, $context = []){
static::$workerLogger = $logger ?: function ($message, $context = []) {
Log::info($message, $context);
};
}

/**
* Set the logger environment.
*
* @param callable $callback
* @return void
*/
public static function workerErrorLogger(?callable $logger = null)
{
static::$workerErrorLogger = $logger ?: function ($message, $context = []) {
Log::error($message, $context);
};
}

/**
* Set the logger environment.
*
Expand All @@ -64,7 +84,7 @@ public static function workerLogger(?callable $logger = null)
*/
public static function threadLogger(?callable $logger = null)
{
static::$threadLogger = $logger ?: function($message, $context = []){
static::$threadLogger = $logger ?: function ($message, $context = []) {
Log::channel('stdout')->info($message, $context);
};

Expand Down
24 changes: 20 additions & 4 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function boot()
$this->eventClasses();
$this->threadLogger();
$this->workerLogger();
$this->workerErrorLogger();

Event::listen(ShouldBeStored::class, SendToEventStoreListener::class);
}
Expand All @@ -44,8 +45,9 @@ public function boot()
*/
public function eventClasses()
{
if (empty(EventStore::$eventToClass))
if (empty(EventStore::$eventToClass)) {
EventStore::eventToClass();
}
}

/**
Expand All @@ -55,19 +57,33 @@ public function eventClasses()
*/
public function threadLogger()
{
if (empty(EventStore::$threadLogger))
if (empty(EventStore::$threadLogger)) {
EventStore::threadLogger();
}
}

/**
* Handle logging when event is triggered.
* Handle passing of std::out output from thread
*
* @return void
*/
public function workerLogger()
{
if (empty(EventStore::$workerLogger))
if (empty(EventStore::$workerLogger)) {
EventStore::workerLogger();
}
}

/**
* Handle passing of std:err output from thread
*
* @return void
*/
public function workerErrorLogger()
{
if (empty(EventStore::$workerErrorLogger)) {
EventStore::workerErrorLogger();
}
}

/**
Expand Down

0 comments on commit b22a4a9

Please sign in to comment.