Package io.karatelabs.output
Class LogContext
java.lang.Object
io.karatelabs.output.LogContext
Thread-local log collector for scenario execution.
All user-script logging (print, karate.log, HTTP) goes here.
Also collects embeds (HTML from doc, images, etc.) for reports.
The collected log/embeds are written to karate-json reports.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classFluent log writer that captures to LogContext buffer AND cascades to SLF4J.static final classSnapshot of the report-buffer threshold, the log mask/pretty settings and — only if something changes them while it is live — the per-category Logback levels. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final org.slf4j.LoggerLogger for console output (test summary)static final org.slf4j.LoggerLogger for HTTP request/response logsstatic final org.slf4j.LoggerLogger for mock server logsstatic final org.slf4j.LoggerLogger for core framework (Suite, Runner, StepExecutor, reports, config, process utils)static final org.slf4j.LoggerLogger for test logs (karate.log, karate.logger, print statements)static final org.slf4j.LoggerLogger for embedded HTTP server request/response logs -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidappendCaptured(String content) Append previously-captured log content verbatim, bypassing level filtering.static voidclear()collect()Get accumulated log and clear buffer (for step/scenario end).Collect and clear embeds.Collect and clear the synthetic steps buffered since the last call (flushed per step by the runtime).voidAdd an embed with just data and mime type.voidAdd an embed (HTML, image, etc.) to be included in step result.voidembed(StepResult.Embed embed) Add a pre-built embed (e.g. a multi-part{name, parts[], meta}from an ext like image-comparison) to the current step.static LogContextget()static LogLevelGet the current log level threshold.getMask()booleanWhether the report buffer collects anything on this thread — seeisCapturing(io.karatelabs.output.LogLevel).booleanisCapturing(LogLevel level) Would a message at this level actually land in the report buffer?booleanisPretty()voidLog a message at the specified level.voidLog a message at the specified level with format arguments.voidLog a message at INFO level.voidLog a message at INFO level with format arguments.peek()Get accumulated log without clearing.static voidset(LogContext ctx) voidsetCapture(boolean capture) static voidsetLogLevel(LogLevel level) Set the minimum log level for report capture.voidvoidsetPretty(boolean pretty) static booleansetRuntimeLogLevel(String level) Set the runtime log level for SLF4J/Logback.static LogContext.Snapshotsnapshot()step(StepResult sr) Append a fully-built synthetic step — the escape hatch for full control (a failure message viaStepResult.synthetic(java.lang.String, io.karatelabs.core.StepResult.Status, long, java.lang.Throwable)with aThrowable, a custom log, multi-part embeds).Append a synthetic step with no embeds (seestep(String, boolean, List)).step(String text, boolean passed, List<StepResult.Embed> embeds) Append a synthetic step to the scenario currently executing on this thread — symmetric withembed(byte[], java.lang.String, java.lang.String): an out-of-band producer (e.g. running inside an* eval) contributes a real, named step row to the report/JSONL without parsing a.feature.static LogContext.LogWriterwith(org.slf4j.Logger logger) Create a LogWriter that captures to the thread-local LogContext AND cascades to SLF4J.
-
Field Details
-
RUNTIME_LOGGER
public static final org.slf4j.Logger RUNTIME_LOGGERLogger for core framework (Suite, Runner, StepExecutor, reports, config, process utils) -
HTTP_LOGGER
public static final org.slf4j.Logger HTTP_LOGGERLogger for HTTP request/response logs -
MOCK_LOGGER
public static final org.slf4j.Logger MOCK_LOGGERLogger for mock server logs -
SERVER_LOGGER
public static final org.slf4j.Logger SERVER_LOGGERLogger for embedded HTTP server request/response logs -
SCENARIO_LOGGER
public static final org.slf4j.Logger SCENARIO_LOGGERLogger for test logs (karate.log, karate.logger, print statements) -
CONSOLE_LOGGER
public static final org.slf4j.Logger CONSOLE_LOGGERLogger for console output (test summary)
-
-
Constructor Details
-
LogContext
public LogContext()
-
-
Method Details
-
get
-
set
-
clear
public static void clear() -
with
Create a LogWriter that captures to the thread-local LogContext AND cascades to SLF4J. Use this for category-aware logging.- Parameters:
logger- the SLF4J logger (determines the category)- Returns:
- a LogWriter for fluent logging
-
setLogLevel
Set the minimum log level for report capture. Logs below this level will be filtered out. -
getLogLevel
Get the current log level threshold. -
setRuntimeLogLevel
Set the runtime log level for SLF4J/Logback. Uses reflection to avoid compile-time dependency on Logback. Sets the level on the "karate" parent AND every child category (karate.runtime, karate.http, karate.mock, karate.server, karate.scenario, karate.console) plus io.karatelabs — seeRUNTIME_LOGGERSfor why the parent alone is not enough.Accepts the standard levels (trace, debug, info, warn, error) plus
offandnone(an alias foroff) to silence the console completely.- Parameters:
level- the log level (trace, debug, info, warn, error, off, none)- Returns:
- true if the level was set successfully, false if Logback is not available
-
snapshot
-
getMask
-
setMask
-
isPretty
public boolean isPretty() -
setPretty
public void setPretty(boolean pretty) -
isCapture
public boolean isCapture()Whether the report buffer collects anything on this thread — seeisCapturing(io.karatelabs.output.LogLevel). -
setCapture
public void setCapture(boolean capture) -
isCapturing
Would a message at this level actually land in the report buffer? Producers of expensive log text — above allHttpLogger, which re-parses and pretty-prints every response body — must ask this before building the string, rather than relying on thelog(io.karatelabs.output.LogLevel, java.lang.String)calls below to drop it once the cost has already been paid. -
log
Log a message at the specified level. Message is filtered if level is below threshold. -
log
Log a message at the specified level with format arguments. -
log
Log a message at INFO level. Used by karate.log() and print statements. -
log
Log a message at INFO level with format arguments. -
embed
Add an embed (HTML, image, etc.) to be included in step result. -
embed
Add an embed with just data and mime type. -
embed
Add a pre-built embed (e.g. a multi-part{name, parts[], meta}from an ext like image-comparison) to the current step. Thread-local, so it targets the step executing on this thread. -
collectEmbeds
Collect and clear embeds. -
step
Append a synthetic step to the scenario currently executing on this thread — symmetric withembed(byte[], java.lang.String, java.lang.String): an out-of-band producer (e.g. running inside an* eval) contributes a real, named step row to the report/JSONL without parsing a.feature. The buffered steps are flushed into the scenario's result immediately after the in-flight step completes, so they appear as sibling step rows right where they were produced.The step's
statusis reported and, forFAILED, fails the scenario (it joins the step listScenarioResult.isFailed()scans) — so a producer can signal a pass/fail purely by appending a step, no exception required.- Parameters:
text- the step's display textpassed-true→ a passed (green) step;false→ a failed step that fails the scenarioembeds- optional embeds attached to THIS step (e.g. a screenshot or a JSON payload); may benull- Returns:
- the created
StepResult(for further customization, e.g.setLog)
-
step
Append a synthetic step with no embeds (seestep(String, boolean, List)). -
step
Append a fully-built synthetic step — the escape hatch for full control (a failure message viaStepResult.synthetic(java.lang.String, io.karatelabs.core.StepResult.Status, long, java.lang.Throwable)with aThrowable, a custom log, multi-part embeds). Seestep(String, boolean, List). -
collectPendingSteps
Collect and clear the synthetic steps buffered since the last call (flushed per step by the runtime). -
collect
Get accumulated log and clear buffer (for step/scenario end). -
appendCaptured
Append previously-captured log content verbatim, bypassing level filtering. Used to carry log output across a LogContext reset (e.g. preserving karate-config.js logs into the fresh per-scenario LogContext). -
peek
Get accumulated log without clearing.
-