From 65dd55653358efc020d92df6d4ea5aed1a4af1f3 Mon Sep 17 00:00:00 2001 From: Adam Date: Mon, 7 Dec 2020 12:24:10 +0100 Subject: [PATCH] Added docs, tests, fix existing tests --- docs/logging.md | 7 +++ test_runner/src/main/kotlin/ftl/args/IArgs.kt | 2 +- .../src/main/kotlin/ftl/log/OutputLogger.kt | 10 ++--- .../src/main/kotlin/ftl/run/DumpShards.kt | 2 +- .../kotlin/ftl/run/common/FetchArtifacts.kt | 10 ++--- .../ftl/run/status/ExecutionStatusPrinter.kt | 2 +- .../src/main/kotlin/ftl/util/ProgressBar.kt | 6 +-- .../test/kotlin/ftl/log/OutputLoggerTest.kt | 45 +++++++++++++++++++ .../test/kotlin/ftl/util/ProgressBarTest.kt | 2 +- 9 files changed, 69 insertions(+), 17 deletions(-) create mode 100644 docs/logging.md create mode 100644 test_runner/src/test/kotlin/ftl/log/OutputLoggerTest.kt diff --git a/docs/logging.md b/docs/logging.md new file mode 100644 index 0000000000..5becdfe8c3 --- /dev/null +++ b/docs/logging.md @@ -0,0 +1,7 @@ +# Logs in Flank + +1. Log level depends on the output style. +1. ```Simple, multi``` and ```verbose``` output style prints logs from ```SIMPLE``` and ```DETAILED``` levels. +1. ```Compact``` style prints log only from ```SIMPLE``` level. +1. If you want a print message for all output styles uses ```log``` or ```logLn``` with only ```message``` parameter. +1. If you want print message more detailed message use ```log``` or ```logLn``` and set ```level``` to ```OutputLogLevel.DETAILED``` diff --git a/test_runner/src/main/kotlin/ftl/args/IArgs.kt b/test_runner/src/main/kotlin/ftl/args/IArgs.kt index ba8d60fea9..33d751f9ad 100644 --- a/test_runner/src/main/kotlin/ftl/args/IArgs.kt +++ b/test_runner/src/main/kotlin/ftl/args/IArgs.kt @@ -85,7 +85,7 @@ interface IArgs { } val IArgs.logLevel - get() = if (outputStyle == OutputStyle.Compact) OutputLogLevel.LOW else OutputLogLevel.All + get() = if (outputStyle == OutputStyle.Compact) OutputLogLevel.SIMPLE else OutputLogLevel.DETAILED fun IArgs.setLogLevel() = also { ftl.log.setLogLevel(logLevel) diff --git a/test_runner/src/main/kotlin/ftl/log/OutputLogger.kt b/test_runner/src/main/kotlin/ftl/log/OutputLogger.kt index dbe464d580..9d9fb9de4f 100644 --- a/test_runner/src/main/kotlin/ftl/log/OutputLogger.kt +++ b/test_runner/src/main/kotlin/ftl/log/OutputLogger.kt @@ -4,17 +4,17 @@ fun setLogLevel(logLevel: OutputLogLevel) { minimumLogLevel = logLevel } -fun log(message: Any, level: OutputLogLevel = OutputLogLevel.LOW) = +fun log(message: Any, level: OutputLogLevel = OutputLogLevel.SIMPLE) = if (minimumLogLevel >= level) print(message) else Unit -fun logLn(message: Any = "", level: OutputLogLevel = OutputLogLevel.LOW) = +fun logLn(message: Any = "", level: OutputLogLevel = OutputLogLevel.SIMPLE) = if (minimumLogLevel >= level) println(message) else Unit -private var minimumLogLevel: OutputLogLevel = OutputLogLevel.LOW +private var minimumLogLevel: OutputLogLevel = OutputLogLevel.DETAILED enum class OutputLogLevel { - LOW, - All + SIMPLE, + DETAILED } diff --git a/test_runner/src/main/kotlin/ftl/run/DumpShards.kt b/test_runner/src/main/kotlin/ftl/run/DumpShards.kt index 57c5fd5065..63f2e8b823 100644 --- a/test_runner/src/main/kotlin/ftl/run/DumpShards.kt +++ b/test_runner/src/main/kotlin/ftl/run/DumpShards.kt @@ -52,7 +52,7 @@ fun saveShardChunks( Paths.get(shardFilePath), getGson(obfuscatedOutput).toJson(shards).toByteArray() ) - logLn("Saved $size shards to $shardFilePath", OutputLogLevel.All) + logLn("Saved $size shards to $shardFilePath", OutputLogLevel.DETAILED) } private fun getGson(obfuscatedOutput: Boolean) = if (obfuscatedOutput) obfuscatePrettyPrinter else prettyPrint diff --git a/test_runner/src/main/kotlin/ftl/run/common/FetchArtifacts.kt b/test_runner/src/main/kotlin/ftl/run/common/FetchArtifacts.kt index 0b84855068..cf2ebd4a20 100644 --- a/test_runner/src/main/kotlin/ftl/run/common/FetchArtifacts.kt +++ b/test_runner/src/main/kotlin/ftl/run/common/FetchArtifacts.kt @@ -20,7 +20,7 @@ import java.nio.file.Paths // TODO needs refactor internal suspend fun fetchArtifacts(matrixMap: MatrixMap, args: IArgs) = coroutineScope { - logLn("FetchArtifacts", OutputLogLevel.All) + logLn("FetchArtifacts", OutputLogLevel.DETAILED) val fields = Storage.BlobListOption.fields(Storage.BlobField.NAME) var dirty = false @@ -43,7 +43,7 @@ internal suspend fun fetchArtifacts(matrixMap: MatrixMap, args: IArgs) = corouti if (matched) { val downloadFile = getDownloadPath(args, blobPath) - log(".", OutputLogLevel.All) + log(".", OutputLogLevel.DETAILED) if (!downloadFile.toFile().exists()) { val parentFile = downloadFile.parent.toFile() parentFile.mkdirs() @@ -56,12 +56,12 @@ internal suspend fun fetchArtifacts(matrixMap: MatrixMap, args: IArgs) = corouti filtered[index] = matrix.copy(downloaded = true) jobs }.joinAll() - logLn(level = OutputLogLevel.All) + logLn(level = OutputLogLevel.DETAILED) if (dirty) { - logLn(FtlConstants.indent + "Updating matrix file", level = OutputLogLevel.All) + logLn(FtlConstants.indent + "Updating matrix file", level = OutputLogLevel.DETAILED) args.updateMatrixFile(matrixMap) - logLn(level = OutputLogLevel.All) + logLn(level = OutputLogLevel.DETAILED) } } diff --git a/test_runner/src/main/kotlin/ftl/run/status/ExecutionStatusPrinter.kt b/test_runner/src/main/kotlin/ftl/run/status/ExecutionStatusPrinter.kt index 4ea0630c67..f3308d0b7e 100644 --- a/test_runner/src/main/kotlin/ftl/run/status/ExecutionStatusPrinter.kt +++ b/test_runner/src/main/kotlin/ftl/run/status/ExecutionStatusPrinter.kt @@ -66,7 +66,7 @@ internal class MultiLinePrinter( @VisibleForTesting internal object VerbosePrinter : (List) -> Unit { override fun invoke(changes: List) { - changes.map(ExecutionStatus.Change::views).flatten().forEach { logLn(it, OutputLogLevel.All) } + changes.map(ExecutionStatus.Change::views).flatten().forEach { logLn(it, OutputLogLevel.DETAILED) } } } diff --git a/test_runner/src/main/kotlin/ftl/util/ProgressBar.kt b/test_runner/src/main/kotlin/ftl/util/ProgressBar.kt index 850a59bb77..d674ba9a0a 100644 --- a/test_runner/src/main/kotlin/ftl/util/ProgressBar.kt +++ b/test_runner/src/main/kotlin/ftl/util/ProgressBar.kt @@ -11,12 +11,12 @@ class ProgressBar { private val timer = Timer(true) fun start(msg: String) { - log(" $msg ", OutputLogLevel.All) + log(" $msg ", OutputLogLevel.DETAILED) timer.scheduleAtFixedRate(task, 0, 10_000) } fun stop() { - logLn(level = OutputLogLevel.All) + logLn(level = OutputLogLevel.DETAILED) timer.cancel() task.cancel() } @@ -24,7 +24,7 @@ class ProgressBar { private class ProgressBarTask : TimerTask() { override fun run() { - log(".", OutputLogLevel.All) + log(".", OutputLogLevel.DETAILED) } } diff --git a/test_runner/src/test/kotlin/ftl/log/OutputLoggerTest.kt b/test_runner/src/test/kotlin/ftl/log/OutputLoggerTest.kt new file mode 100644 index 0000000000..a8852ba7d9 --- /dev/null +++ b/test_runner/src/test/kotlin/ftl/log/OutputLoggerTest.kt @@ -0,0 +1,45 @@ +package ftl.log + +import com.google.common.truth.Truth +import ftl.test.util.TestHelper.normalizeLineEnding +import org.junit.After +import org.junit.Rule +import org.junit.Test +import org.junit.contrib.java.lang.system.SystemOutRule + +class OutputLoggerTest { + + @Rule + @JvmField + val systemOutRule: SystemOutRule = SystemOutRule().enableLog().muteForSuccessfulTests() + + private val simpleMessage = "print for simple" + private val detailedMessage = "print for detailed" + + @After + fun afterTest() { + setLogLevel(OutputLogLevel.DETAILED) + } + + @Test + fun `should print messages from all output levels if log level set to detailed`() { + setLogLevel(OutputLogLevel.DETAILED) + + logLn(simpleMessage) + logLn(detailedMessage, OutputLogLevel.DETAILED) + + Truth.assertThat(systemOutRule.log.normalizeLineEnding()).contains(simpleMessage) + Truth.assertThat(systemOutRule.log.normalizeLineEnding()).contains(detailedMessage) + } + + @Test + fun `should print messages only simple output level if log level not set`() { + setLogLevel(OutputLogLevel.SIMPLE) + + logLn(simpleMessage) + logLn(detailedMessage, OutputLogLevel.DETAILED) + + Truth.assertThat(systemOutRule.log.normalizeLineEnding()).contains(simpleMessage) + Truth.assertThat(systemOutRule.log.normalizeLineEnding()).doesNotContain(detailedMessage) + } +} diff --git a/test_runner/src/test/kotlin/ftl/util/ProgressBarTest.kt b/test_runner/src/test/kotlin/ftl/util/ProgressBarTest.kt index 831bfdc01a..0b7dadba1d 100644 --- a/test_runner/src/test/kotlin/ftl/util/ProgressBarTest.kt +++ b/test_runner/src/test/kotlin/ftl/util/ProgressBarTest.kt @@ -20,7 +20,7 @@ class ProgressBarTest { @Before fun beforeTest() { - setLogLevel(OutputLogLevel.All) + setLogLevel(OutputLogLevel.DETAILED) } @Test