Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Monolog listener #1204

Merged
merged 6 commits into from
Dec 13, 2019
Merged
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
154 changes: 154 additions & 0 deletions classes/phing/listener/MonologListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
<?php
/**
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information please see
* <http://phing.info>.
*/

use Monolog\Logger;

/**
* Listener which sends events to Monolog.
*
* @author Siad Ardroumli <siad.ardroumli@gmail.com>
* @package phing.listener
*/
class MonologListener implements BuildListener
{
/**
* log category we log into
*/
public const LOG_PHING = 'phing';

/**
* @var Logger
*/
private $log;

/**
* Construct the listener
*/
public function __construct()
{
$this->log = new Logger(self::LOG_PHING);
}

/**
* @see BuildListener#buildStarted
* {@inheritDoc}.
*/
public function buildStarted(BuildEvent $event)
{
$log = $this->log->withName(Project::class);
$log->info('Build started.');
}

/**
* @see BuildListener#buildFinished
* {@inheritDoc}.
*/
public function buildFinished(BuildEvent $event)
{
$log = $this->log->withName(Project::class);
if ($event->getException() === null) {
$log->info('Build finished.');
} else {
$log->error('Build finished with error. ' . $event->getException());
}
}

/**
* @see BuildListener#targetStarted
* {@inheritDoc}.
*/
public function targetStarted(BuildEvent $event)
{
$log = $this->log->withName(Target::class);
$log->info("Target \"{$event->getTarget()->getName()}\" started.");
}

/**
* @see BuildListener#targetFinished
* {@inheritDoc}.
*/
public function targetFinished(BuildEvent $event)
{
$targetName = $event->getTarget()->getName();
$cat = $this->log->withName(Target::class);
if ($event->getException() === null) {
$cat->info("Target \"{$targetName}\" finished.");
} else {
$cat->error("Target \"{$targetName}\" finished with error. " . $event->getException());
}
}

/**
* @see BuildListener#taskStarted
* {@inheritDoc}.
*/
public function taskStarted(BuildEvent $event)
{
$task = $event->getTask();
$log = $this->log->withName(get_class($task));
$log->info("Task \"{$task->getTaskName()}\" started.");
}

/**
* @see BuildListener#taskFinished
* {@inheritDoc}.
*/
public function taskFinished(BuildEvent $event)
{
$task = $event->getTask();
$log = $this->log->withName(get_class($task));
if ($event->getException() === null) {
$log->info("Task \"{$task->getTaskName()}\" finished.");
} else {
$log->error("Task \"{$task->getTaskName()}\" finished with error. {$event->getException()}");
}
}

/**
* @see BuildListener#messageLogged
* {@inheritDoc}.
*/
public function messageLogged(BuildEvent $event)
{
$categoryObject = $event->getTask();
if ($categoryObject === null) {
$categoryObject = $event->getTarget();
if ($categoryObject === null) {
$categoryObject = $event->getProject();
}
}

$log = $this->log->withName(get_class($categoryObject));
switch ($event->getPriority()) {
case Project::MSG_WARN:
$log->warn($event->getMessage());
break;
case Project::MSG_INFO:
$log->info($event->getMessage());
break;
case Project::MSG_VERBOSE:
case Project::MSG_DEBUG:
$log->debug($event->getMessage());
break;
default:
$log->error($event->getMessage());
break;
}
}
}
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@
"squizlabs/php_codesniffer": "^2.9 || ^3.5",
"symfony/stopwatch": "~2.8 || ^3.1 || ^4.0 || ^5.0",
"tedivm/jshrink": "^1.3",
"symfony/dependency-injection": "^2.8.50 || ^3.4.26 || ~4.1.12 || >4.2.7 <6.0.0"
"symfony/dependency-injection": "^2.8.50 || ^3.4.26 || ~4.1.12 || >4.2.7 <6.0.0",
"monolog/monolog": "^1.25 || ^2.0"
},
"autoload": {
"classmap": [
Expand Down Expand Up @@ -95,7 +96,8 @@
"symfony/stopwatch": "Needed by the StopwatchTask",
"aws/aws-sdk-php": "AWS SDK used by S3*Tasks",
"scssphp/scssphp": "A compiler for SCSS written in PHP, used by SassTask",
"jawira/plantuml-encoding": "Required by VisualizerTask"
"jawira/plantuml-encoding": "Required by VisualizerTask",
"monolog/monolog": "Required by the MonologListener"
},
"prefer-stable": true
}
Loading