Class LogContext

java.lang.Object
io.karatelabs.output.LogContext

public class LogContext extends Object
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.
  • Field Details

    • RUNTIME_LOGGER

      public static final org.slf4j.Logger RUNTIME_LOGGER
      Logger for core framework (Suite, Runner, StepExecutor, reports, config, process utils)
    • HTTP_LOGGER

      public static final org.slf4j.Logger HTTP_LOGGER
      Logger for HTTP request/response logs
    • MOCK_LOGGER

      public static final org.slf4j.Logger MOCK_LOGGER
      Logger for mock server logs
    • SERVER_LOGGER

      public static final org.slf4j.Logger SERVER_LOGGER
      Logger for embedded HTTP server request/response logs
    • SCENARIO_LOGGER

      public static final org.slf4j.Logger SCENARIO_LOGGER
      Logger for test logs (karate.log, karate.logger, print statements)
    • CONSOLE_LOGGER

      public static final org.slf4j.Logger CONSOLE_LOGGER
      Logger for console output (test summary)
  • Constructor Details

    • LogContext

      public LogContext()
  • Method Details

    • get

      public static LogContext get()
    • set

      public static void set(LogContext ctx)
    • clear

      public static void clear()
    • with

      public static LogContext.LogWriter with(org.slf4j.Logger logger)
      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

      public static void setLogLevel(LogLevel level)
      Set the minimum log level for report capture. Logs below this level will be filtered out.
    • getLogLevel

      public static LogLevel getLogLevel()
      Get the current log level threshold.
    • setRuntimeLogLevel

      public static boolean setRuntimeLogLevel(String level)
      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 — see RUNTIME_LOGGERS for why the parent alone is not enough.

      Accepts the standard levels (trace, debug, info, warn, error) plus off and none (an alias for off) 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

      public static LogContext.Snapshot snapshot()
    • getMask

      public LogMask getMask()
    • setMask

      public void setMask(LogMask mask)
    • isPretty

      public boolean isPretty()
    • setPretty

      public void setPretty(boolean pretty)
    • isCapture

      public boolean isCapture()
      Whether the report buffer collects anything on this thread — see isCapturing(io.karatelabs.output.LogLevel).
    • setCapture

      public void setCapture(boolean capture)
    • isCapturing

      public boolean isCapturing(LogLevel level)
      Would a message at this level actually land in the report buffer? Producers of expensive log text — above all HttpLogger, which re-parses and pretty-prints every response body — must ask this before building the string, rather than relying on the log(io.karatelabs.output.LogLevel, java.lang.String) calls below to drop it once the cost has already been paid.
    • log

      public void log(LogLevel level, String message)
      Log a message at the specified level. Message is filtered if level is below threshold.
    • log

      public void log(LogLevel level, String format, Object... args)
      Log a message at the specified level with format arguments.
    • log

      public void log(Object message)
      Log a message at INFO level. Used by karate.log() and print statements.
    • log

      public void log(String format, Object... args)
      Log a message at INFO level with format arguments.
    • embed

      public void embed(byte[] data, String mimeType, String name)
      Add an embed (HTML, image, etc.) to be included in step result.
    • embed

      public void embed(byte[] data, String mimeType)
      Add an embed with just data and mime type.
    • embed

      public void embed(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. Thread-local, so it targets the step executing on this thread.
    • collectEmbeds

      public List<StepResult.Embed> collectEmbeds()
      Collect and clear embeds.
    • step

      public StepResult step(String text, boolean passed, List<StepResult.Embed> embeds)
      Append a synthetic step to the scenario currently executing on this thread — symmetric with embed(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 status is reported and, for FAILED, fails the scenario (it joins the step list ScenarioResult.isFailed() scans) — so a producer can signal a pass/fail purely by appending a step, no exception required.

      Parameters:
      text - the step's display text
      passed - true → a passed (green) step; false → a failed step that fails the scenario
      embeds - optional embeds attached to THIS step (e.g. a screenshot or a JSON payload); may be null
      Returns:
      the created StepResult (for further customization, e.g. setLog)
    • step

      public StepResult step(String text, boolean passed)
      Append a synthetic step with no embeds (see step(String, boolean, List)).
    • step

      public StepResult step(StepResult sr)
      Append a fully-built synthetic step — the escape hatch for full control (a failure message via StepResult.synthetic(java.lang.String, io.karatelabs.core.StepResult.Status, long, java.lang.Throwable) with a Throwable, a custom log, multi-part embeds). See step(String, boolean, List).
    • collectPendingSteps

      public List<StepResult> collectPendingSteps()
      Collect and clear the synthetic steps buffered since the last call (flushed per step by the runtime).
    • collect

      public String collect()
      Get accumulated log and clear buffer (for step/scenario end).
    • appendCaptured

      public void appendCaptured(String content)
      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

      public String peek()
      Get accumulated log without clearing.