Skip to content

Commit

Permalink
Merge pull request #6 from hirokinoue/logging
Browse files Browse the repository at this point in the history
処理の進行がわかるログを出力する
  • Loading branch information
hirokinoue authored Feb 26, 2024
2 parents e9db6f4 + e03bd15 commit ad9481f
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 16 deletions.
25 changes: 9 additions & 16 deletions bin/dependency-visualizer
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@ use Hirokinoue\DependencyVisualizer\Config\Config;
use Hirokinoue\DependencyVisualizer\Exporter\Exporter;
use Hirokinoue\DependencyVisualizer\Exporter\PlantUmlExporter;
use Hirokinoue\DependencyVisualizer\Exporter\StringExporter;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\NullHandler;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;

foreach ([__DIR__ . '/../../../autoload.php', __DIR__ . '/../vendor/autoload.php'] as $autoload) {
if (file_exists($autoload)) {
$loadedFile = $autoload;
require $autoload;
break;
}
Expand All @@ -35,30 +32,26 @@ Config::initialize(\getcwd());

ini_set('memory_limit', Config::memoryLimit());

$logger = new Logger('dependency visualizer');
if ($options['log']) {
$handler = new StreamHandler(getcwd() . '/app.log');
$handler->setFormatter(new LineFormatter(null, null, true));
$logger->pushHandler($handler);
} else {
$logger->pushHandler(new NullHandler());
}
Logger::initialize($options['log']);

$logger->info('start analyzing', ['target' => $file]);
Logger::info('autoload file has been loaded', ['path' => $loadedFile ?? '']);
Logger::info('start analyzing', ['target' => $file]);

try {
$dependencyVisualizer = DependencyVisualizer::create($file);
$diagramUnit = $dependencyVisualizer->analyze();
} catch (\Exception $e) {
$logger->error($e->getMessage());
$logger->error('finish analyzing abnormally');
Logger::error($e->getMessage());
Logger::error('finish analyzing abnormally');
exit(1);
}

Logger::info('start drawing');

$exporter = exporterFactory($options);
echo $exporter->export($diagramUnit);

$logger->info('finish analyzing');
Logger::info('finish analyzing');
exit(0);

function exporterFactory($options): Exporter {
Expand Down
47 changes: 47 additions & 0 deletions src/Logger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php declare(strict_types=1);

namespace Hirokinoue\DependencyVisualizer;

use Monolog\Formatter\LineFormatter;
use Monolog\Handler\NullHandler;
use Monolog\Handler\StreamHandler;
use Monolog\Logger as MonologLogger;

final class Logger
{
private static ?MonologLogger $logger = null;

public static function initialize(bool $enable): void
{
self::$logger = new MonologLogger('dependency visualizer');
if ($enable) {
$handler = new StreamHandler(getcwd() . '/app.log');
$handler->setFormatter(new LineFormatter(null, null, true));
self::$logger->pushHandler($handler);
} else {
self::$logger->pushHandler(new NullHandler());
}
}

/**
* @param mixed[] $context
*/
public static function info(string $message, array $context = []): void
{
if (self::$logger === null) {
return;
}
self::$logger->info($message, $context);
}

/**
* @param mixed[] $context
*/
public static function error(string $message, array $context = []): void
{
if (self::$logger === null) {
return;
}
self::$logger->error($message, $context);
}
}
3 changes: 3 additions & 0 deletions src/Visitor/ClassVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Hirokinoue\DependencyVisualizer\ClassManipulator\ClassLoader;
use Hirokinoue\DependencyVisualizer\Config\Config;
use Hirokinoue\DependencyVisualizer\DiagramUnit;
use Hirokinoue\DependencyVisualizer\Logger;
use PhpParser\Node;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\NodeTraverser;
Expand All @@ -19,6 +20,7 @@ final class ClassVisitor extends NodeVisitorAbstract
public function __construct(DiagramUnit $diagramUnit) {
$diagramUnit->registerVisitedClass();
$this->diagramUnit = $diagramUnit;
Logger::info('instantiate ClassVisitor', ['name' => $diagramUnit->fullyQualifiedClassName()]);
}

public function enterNode(Node $node) {
Expand All @@ -36,6 +38,7 @@ public function enterNode(Node $node) {

$classFile = ClassLoader::create($node);
if ($classFile->isClass()) {
Logger::info('load class', ['name' => $classFile->className()]);
$ancestors = $this->diagramUnit->ancestors();
$ancestors[] = $node->toCodeString();

Expand Down

0 comments on commit ad9481f

Please sign in to comment.