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.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
    Fluent log writer that captures to LogContext buffer AND cascades to SLF4J.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final org.slf4j.Logger
    Logger for console output (test summary)
    static final org.slf4j.Logger
    Logger for HTTP request/response logs
    static final org.slf4j.Logger
    Logger for mock server logs
    static final org.slf4j.Logger
    Logger for core framework (Suite, Runner, StepExecutor, reports, config, process utils)
    static final org.slf4j.Logger
    Logger for test logs (karate.log, karate.logger, print statements)
    static final org.slf4j.Logger
    Logger for embedded HTTP server request/response logs
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
     
    Get accumulated log and clear buffer (for step/scenario end).
    Collect and clear embeds.
    void
    embed(byte[] data, String mimeType)
    Add an embed with just data and mime type.
    void
    embed(byte[] data, String mimeType, String name)
    Add an embed (HTML, image, etc.) to be included in step result.
    static LogContext
    get()
     
    static LogLevel
    Get the current log level threshold.
    void
    log(LogLevel level, String message)
    Log a message at the specified level.
    void
    log(LogLevel level, String format, Object... args)
    Log a message at the specified level with format arguments.
    void
    log(Object message)
    Log a message at INFO level.
    void
    log(String format, Object... args)
    Log a message at INFO level with format arguments.
    Get accumulated log without clearing.
    static void
     
    static void
    Set the minimum log level for report capture.
    static boolean
    Set the runtime log level for SLF4J/Logback.
    with(org.slf4j.Logger logger)
    Create a LogWriter that captures to the thread-local LogContext AND cascades to SLF4J.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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" logger, which affects all subcategories (karate.runtime, karate.http, karate.mock, karate.server, karate.scenario, karate.console).
      Parameters:
      level - the log level (trace, debug, info, warn, error)
      Returns:
      true if the level was set successfully, false if Logback is not available
    • 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.
    • collectEmbeds

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

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

      public String peek()
      Get accumulated log without clearing.