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. -
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 TypeMethodDescriptionstatic voidclear()collect()Get accumulated log and clear buffer (for step/scenario end).Collect and clear embeds.voidAdd an embed with just data and mime type.voidAdd an embed (HTML, image, etc.) to be included in step result.static LogContextget()static LogLevelGet the current log level threshold.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) static voidsetLogLevel(LogLevel level) Set the minimum log level for report capture.static booleansetRuntimeLogLevel(String level) Set the runtime log level for SLF4J/Logback.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" 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
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. -
collectEmbeds
Collect and clear embeds. -
collect
Get accumulated log and clear buffer (for step/scenario end). -
peek
Get accumulated log without clearing.
-