Skip to content

Commit

Permalink
Merge pull request #48 from nextcloud/feat/verbose-logging
Browse files Browse the repository at this point in the history
  • Loading branch information
provokateurin authored Dec 13, 2023
2 parents bf7adc0 + 7a527bb commit 618132c
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
14 changes: 8 additions & 6 deletions generate-spec
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ $command
->option('--no-tags', 'Use no tags')
->option('--continue-on-error', 'Continue on error')
->option('--openapi-version', 'OpenAPI version to use', null, '3.0.3')
->option('--verbose', 'Verbose logging')
->parse($_SERVER["argv"]);

$dir = $command->dir ?? "";
Expand All @@ -49,6 +50,7 @@ $firstContentType = $command->firstContentType ?? false;
$allowMissingDocs = $command->allowMissingDocs ?? false;
$useTags = $command->tags ?? true;
Logger::$exitOnError = !($command->continueOnError ?? false);
Logger::$verbose = $command->verbose ?? false;
$openapiVersion = $command->openapiVersion ?? '3.0.3';

if ($dir == "") {
Expand Down Expand Up @@ -146,7 +148,7 @@ if (file_exists($definitionsPath)) {
$schemas[cleanSchemaName($name)] = OpenApiType::resolve("Response definitions", $definitions, $definitions[$name])->toArray($openapiVersion);
}
} else {
Logger::warning("Response definitions", "No response definitions were loaded");
Logger::debug("Response definitions", "No response definitions were loaded");
}

$capabilities = null;
Expand Down Expand Up @@ -220,7 +222,7 @@ if ($publicCapabilities != null) {
$schemas["PublicCapabilities"] = $publicCapabilities;
}
if ($capabilities == null && $publicCapabilities == null) {
Logger::warning("Capabilities", "No capabilities were loaded");
Logger::debug("Capabilities", "No capabilities were loaded");
}

$parsedRoutes = file_exists($appinfoDir . "/routes.php") ? Route::parseRoutes($appinfoDir . "/routes.php") : [];
Expand Down Expand Up @@ -274,7 +276,7 @@ foreach ($parsedRoutes as $key => $value) {
continue;
}
if (Helpers::classMethodHasAnnotationOrAttribute($controllerClass, "IgnoreOpenAPI")) {
Logger::info($routeName, "Controller '" . $controllerName . "' ignored because of IgnoreOpenAPI attribute");
Logger::debug($routeName, "Controller '" . $controllerName . "' ignored because of IgnoreOpenAPI attribute");
continue;
}

Expand Down Expand Up @@ -317,7 +319,7 @@ foreach ($parsedRoutes as $key => $value) {
$isCSRFRequired = !Helpers::classMethodHasAnnotationOrAttribute($classMethod, "NoCSRFRequired");
$isOCS = $controllerClass->extends != "Controller" && $controllerClass->extends != "ApiController";
if ($isCSRFRequired && !$isOCS) {
Logger::info($routeName, "Route ignored because of required CSRF in a non-OCS controller");
Logger::debug($routeName, "Route ignored because of required CSRF in a non-OCS controller");
continue;
}

Expand All @@ -328,7 +330,7 @@ foreach ($parsedRoutes as $key => $value) {
$isIgnored = Helpers::classMethodHasAnnotationOrAttribute($classMethod, "IgnoreOpenAPI");

if ($isIgnored) {
Logger::info($routeName, "Route ignored because of IgnoreOpenAPI attribute");
Logger::debug($routeName, "Route ignored because of IgnoreOpenAPI attribute");
continue;
}

Expand Down Expand Up @@ -396,7 +398,7 @@ foreach ($parsedRoutes as $key => $value) {
$isPublic,
);

Logger::info($routeName, "Route generated");
Logger::debug($routeName, "Route generated");
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/ControllerMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ static function parse(string $context, array $definitions, ClassMethod $method,
),
);
} catch (LoggerException $e) {
Logger::warning($context, "Unable to parse parameter " . $methodParameterName . ": " . $e->message . "\n" . $e->getTraceAsString());
Logger::debug($context, "Unable to parse parameter " . $methodParameterName . ": " . $e->message . "\n" . $e->getTraceAsString());
// Fallback to the @param annotation
$type = OpenApiType::resolve(
$context,
Expand Down
10 changes: 9 additions & 1 deletion src/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,28 @@

class Logger {
static bool $exitOnError = true;
static bool $verbose = false;

protected static function log(LoggerLevel $level, string $context, string $text): void {
print(self::format($level, $context, $text));
}

protected static function format(LoggerLevel $level, string $context, string $text): string {
$colorCode = match ($level) {
LoggerLevel::Info => "",
LoggerLevel::Debug => "",
LoggerLevel::Info => "\e[32m",
LoggerLevel::Warning => "\e[33m",
LoggerLevel::Error => "\e[91m",
};
return $colorCode . $level->value . ": " . $context . ": " . $text . "\n\e[0m";
}

public static function debug(string $context, string $text): void {
if (Logger::$verbose) {
self::log(LoggerLevel::Debug, $context, $text);
}
}

public static function info(string $context, string $text): void {
self::log(LoggerLevel::Info, $context, $text);
}
Expand Down
1 change: 1 addition & 0 deletions src/LoggerLevel.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace OpenAPIExtractor;

enum LoggerLevel: string {
case Debug = "Debug";
case Info = "Info";
case Warning = "Warning";
case Error = "Error";
Expand Down

0 comments on commit 618132c

Please sign in to comment.