Logging is essential for debugging and monitoring AI applications. LangChain4j provides built-in internal logging capabilities to track requests sent to AI models and responses received from them. This internal logging helps developers understand what's happening during API calls and troubleshoot issues effectively.
Enabling Logging
LangChain4j provides model builder level logging methods that are available across all model implementations. The following two methods are consistently provided by all model builder classes (such as OpenAiChatModel.Builder, OllamaChatModel.Builder, and others) for enabling internal logging:
OpenAiChatModel.Builder logRequests(boolean logRequests);
OpenAiChatModel.Builder logResponses(boolean logResponses);
The logRequests() method enables internal logging of requests sent to the AI model, while logResponses() enables internal logging of responses received from the model. These consistent builder methods allow for uniform logging configuration across different AI model providers in your LangChain4j applications.
Example: Basic Logging with Ollama
package com.logicbig.example;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.ollama.OllamaChatModel;
public class BasicLoggingExample {
public static void main(String[] args) {
ChatModel model = OllamaChatModel.builder()
.baseUrl("http://localhost:11434")
.modelName("phi3:mini-128k")
.logRequests(true)
.logResponses(true)
.build();
String response = model.chat("Explain AI in one sentence");
//todo process response
}
}
Output$ mvn compile exec:java -Dexec.mainClass="com.logicbig.example.BasicLoggingExample" [INFO] Scanning for projects... [INFO] [INFO] --------------------< com.logicbig.example:logging >-------------------- [INFO] Building logging 1.0-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- resources:3.3.1:resources (default-resources) @ logging --- [INFO] skip non existing resourceDirectory D:\example-projects\ai\lang-chain-4j\logging\src\main\resources [INFO] [INFO] --- compiler:3.11.0:compile (default-compile) @ logging --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- exec:3.6.3:java (default-cli) @ logging --- [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 10.578 s [INFO] Finished at: 2026-01-23T11:54:10+08:00 [INFO] ------------------------------------------------------------------------ [com.logicbig.example.BasicLoggingExample.main()] INFO dev.langchain4j.http.client.log.LoggingHttpClient - HTTP request: - method: POST - url: http://localhost:11434/api/chat - headers: [Content-Type: application/json] - body: { "model" : "phi3:mini-128k", "messages" : [ { "role" : "user", "content" : "Explain AI in one sentence" } ], "options" : { "stop" : [ ] }, "stream" : false, "tools" : [ ] }
[com.logicbig.example.BasicLoggingExample.main()] INFO dev.langchain4j.http.client.log.LoggingHttpClient - HTTP response: - status code: 200 - headers: [content-length: 520], [content-type: application/json; charset=utf-8], [date: Fri, 23 Jan 2026 03:54:11 GMT] - body: {"model":"phi3:mini-128k","created_at":"2026-01-23T03:54:11.348989556Z","message":{"role":"assistant","content":"Artificial Intelligence is the development of computer systems that can perform tasks which typically require human intelligence, such as visual perception, speech recognition, decision-making, and language translation."},"done":true,"done_reason":"stop","total_duration":6758266273,"load_duration":41099462,"prompt_eval_count":16,"prompt_eval_duration":213325420,"eval_count":39,"eval_duration":6478933363}
Conclusion
The output from our examples shows how LangChain4j's internal logging provides detailed information about API calls. In the example, both requests and responses are logged, giving us complete visibility into the communication with the Ollama model. These internal logging capabilities are crucial for debugging complex AI applications and ensuring they work as expected.
Example ProjectDependencies and Technologies Used: - langchain4j 1.10.0 (Build LLM-powered applications in Java: chatbots, agents, RAG, and much more)
- langchain4j-ollama 1.10.0 (LangChain4j :: Integration :: Ollama)
- slf4j-simple 2.0.9 (SLF4J Simple Provider)
- JDK 17
- Maven 3.9.11
|
|