Skip to content

Commit

Permalink
Add --verbose flag that prints all logs to stdout (#1987)
Browse files Browse the repository at this point in the history
* add --verbose flag to all commands

* fix `maestro hierarchy` outputting tons of logs
  • Loading branch information
bartekpacia authored Sep 2, 2024
1 parent 3ec7cac commit 9cd39a6
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 13 deletions.
4 changes: 3 additions & 1 deletion maestro-cli/src/main/java/maestro/cli/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ import kotlin.system.exitProcess
]
)
class App {

@CommandLine.Mixin
var disableANSIMixin: DisableAnsiMixin? = null

Expand All @@ -84,6 +83,9 @@ class App {
description = ["(Optional) Device ID to run on explicitly, can be a comma separated list of IDs: --device \"Emulator_1,Emulator_2\" "],
)
var deviceId: String? = null

@Option(names = ["--verbose"], description = ["Enable verbose logging"])
var verbose: Boolean = false
}

private fun printVersion() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import maestro.cli.App
import maestro.cli.DisableAnsiMixin
import maestro.cli.ShowHelpMixin
import maestro.cli.report.TestDebugReporter
import maestro.cli.session.MaestroSessionManager
import maestro.cli.view.green
import maestro.cli.view.yellow
Expand Down Expand Up @@ -51,6 +52,12 @@ class PrintHierarchyCommand : Runnable {
private val parent: App? = null

override fun run() {
TestDebugReporter.install(
debugOutputPathAsString = null,
flattenDebugOutput = false,
printToConsole = parent?.verbose == true,
)

MaestroSessionManager.newSession(
host = parent?.host,
port = parent?.port,
Expand All @@ -73,7 +80,5 @@ class PrintHierarchyCommand : Runnable {

println(hierarchy)
}

System.err.println("Have you tried running “maestro studio” to visually inspect your app’s UI elements?".green())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class RecordCommand : Callable<Int> {
}


TestDebugReporter.install(debugOutputPathAsString = debugOutput)
TestDebugReporter.install(debugOutputPathAsString = debugOutput, printToConsole = parent?.verbose == true)
val path = TestDebugReporter.getDebugOutputPath()

return MaestroSessionManager.newSession(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class StartDeviceCommand : Callable<Int> {
private var forceCreate: Boolean = false

override fun call(): Int {
TestDebugReporter.install(null)
TestDebugReporter.install(null, printToConsole = parent?.verbose == true)

if (EnvUtils.isWSL()) {
throw CliError("This command is not supported in Windows WSL. You can launch your emulator manually.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class StudioCommand : Callable<Int> {
throw CliError("--platform option was deprecated. You can remove it to run your test.")
}

TestDebugReporter.install(debugOutputPathAsString = debugOutput)
TestDebugReporter.install(debugOutputPathAsString = debugOutput, printToConsole = parent?.verbose == true)

MaestroSessionManager.newSession(
host = parent?.host,
Expand Down
8 changes: 6 additions & 2 deletions maestro-cli/src/main/java/maestro/cli/command/TestCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,11 @@ class TestCommand : Callable<Int> {

env = env.withInjectedShellEnvVars()

TestDebugReporter.install(debugOutputPathAsString = debugOutput, flattenDebugOutput = flattenDebugOutput)
TestDebugReporter.install(
debugOutputPathAsString = debugOutput,
flattenDebugOutput = flattenDebugOutput,
printToConsole = parent?.verbose == true,
)
val debugOutputPath = TestDebugReporter.getDebugOutputPath()

return handleSessions(debugOutputPath, executionPlan)
Expand Down Expand Up @@ -300,7 +304,7 @@ class TestCommand : Callable<Int> {

} else {
val resultView =
if (DisableAnsiMixin.ansiEnabled) AnsiResultView()
if (DisableAnsiMixin.ansiEnabled && parent?.verbose == false) AnsiResultView()
else PlainTextResultView()
val resultSingle = TestRunner.runSingle(
maestro,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,11 @@ object TestDebugReporter {
logger.info("---------------------")
}

fun install(debugOutputPathAsString: String?, flattenDebugOutput: Boolean = false) {
fun install(debugOutputPathAsString: String?, flattenDebugOutput: Boolean = false, printToConsole: Boolean) {
this.debugOutputPathAsString = debugOutputPathAsString
this.flattenDebugOutput = flattenDebugOutput
val path = getDebugOutputPath()
LogConfig.configure(path.absolutePathString() + "/maestro.log")
LogConfig.configure(logFileName = path.absolutePathString() + "/maestro.log", printToConsole = printToConsole)
logSystemInfo()
DebugLogStore.logSystemInfo()
}
Expand All @@ -163,7 +163,6 @@ object TestDebugReporter {
val foldername = DateTimeFormatter.ofPattern("yyyy-MM-dd_HHmmss").format(LocalDateTime.now())
return Paths.get(debugRootPath, *preamble, foldername)
}

}

private data class CommandDebugWrapper(
Expand Down
6 changes: 4 additions & 2 deletions maestro-client/src/main/java/maestro/debuglog/LogConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@ import java.util.Properties
object LogConfig {
private const val LOG_PATTERN = "[%-5level] %logger{36} - %msg%n"

fun configure(logFileName: String) {
fun configure(logFileName: String, printToConsole: Boolean) {
val loggerContext = LoggerFactory.getILoggerFactory() as LoggerContext
loggerContext.statusManager.add(NopStatusListener())
loggerContext.reset()

val encoder = createEncoder(loggerContext)
// createAndAddConsoleAppender(loggerContext, encoder) // un-comment to enable console logs
createAndAddFileAppender(loggerContext, encoder, logFileName)
if (printToConsole) {
createAndAddConsoleAppender(loggerContext, encoder)
}

loggerContext.getLogger("ROOT").level = Level.INFO
}
Expand Down

0 comments on commit 9cd39a6

Please sign in to comment.