Skip to content
Permalink
Browse files

Merge pull request #5 from twilio/logging-control

Allow setting guardrail.loglevel to control guardrail logging
  • Loading branch information...
blast-hardcheese committed May 1, 2019
2 parents 172a6c1 + fbdb7b2 commit 7ac3fdb5364dd476fe4ed730ec38318254220b03
Showing with 15 additions and 3 deletions.
  1. +6 −0 README.md
  2. +9 −3 src/main/scala/com/twilio/guardrail/AbstractGuardrailCodegenMojo.scala
@@ -49,3 +49,9 @@ To generate multiple clients, specify multiple `<execution>` sections.
| tracing | Whether or not to generate clients that accept a `TracingContext` which will send tracing headers to the remote service (defaults to `false`) |
| customImports | A list of `<customImport>`s that will be added to the top of all generated files. Useful for providing additional typeclass instances or domain-specific types |
| framework | The framework to generate the code for (defaults to `akka-http`) |

The plugin also interprets a single system property:

| Property | Description |
|:---------|:------------|
| `guardrail.loglevel` | Changes Guardrail's log level. Possible options are "debug", "info", "warning" (the default), "error", and "silent". |
@@ -9,14 +9,15 @@ import com.twilio.guardrail.core.CoreTermInterp
import com.twilio.guardrail.languages.{ ScalaLanguage, LA }
import com.twilio.guardrail.terms.CoreTerms
import com.twilio.guardrail.terms.{CoreTerm, CoreTerms, GetDefaultFramework}
import com.twilio.swagger.core.StructuredLogger._
import com.twilio.swagger.core.{LogLevel, LogLevels}
import java.io.File
import org.apache.maven.plugin.{AbstractMojo, MojoExecutionException, MojoFailureException}
import org.apache.maven.plugins.annotations.Parameter
import org.apache.maven.project.MavenProject
import scala.collection.JavaConverters._
import scala.io.AnsiColor
import scala.language.higherKinds
import scala.meta._
import scala.util.control.NonFatal

class CodegenFailedException extends Exception
@@ -91,9 +92,11 @@ abstract class AbstractGuardrailCodegenMojo(phase: Phase) extends AbstractMojo {
imports=Option(customImports).fold[List[String]](List.empty)(_.asScala.toList.map(_.toString))
)

val logLevel = Option(System.getProperty("guardrail.loglevel")).flatMap(LogLevels.apply).getOrElse(LogLevels.Warning)

getLog.info(s"Generating ${_kind} from ${specPath.getName}")

guardrailTask(List((_language, arg)), outputPath)
guardrailTask(List((_language, arg)), outputPath)(logLevel)
} catch {
case NonFatal(e) =>
getLog.error("Failed to generate client", e)
@@ -102,7 +105,7 @@ abstract class AbstractGuardrailCodegenMojo(phase: Phase) extends AbstractMojo {
}

type Language = String
def guardrailTask(tasks: List[(Language, Args)], sourceDir: java.io.File): Seq[java.io.File] = {
def guardrailTask(tasks: List[(Language, Args)], sourceDir: java.io.File)(implicit logLevel: LogLevel): Seq[java.io.File] = {
val preppedTasks: Map[String, NonEmptyList[Args]] = tasks.foldLeft(Map.empty[String, NonEmptyList[Args]]) { case (acc, (language, args)) =>
val prepped = args.copy(outputPath=Some(sourceDir.getPath))
acc.updated(language, acc.get(language).fold(NonEmptyList.one(prepped))(_ :+ prepped))
@@ -139,6 +142,9 @@ abstract class AbstractGuardrailCodegenMojo(phase: Phase) extends AbstractMojo {
throw new CodegenFailedException()
}, identity)
.runEmpty

print(logger.show)

paths.map(_.toFile).distinct
}
}

0 comments on commit 7ac3fdb

Please sign in to comment.
You can’t perform that action at this time.