TL;DR
This paper presents the first comprehensive empirical study on using Large Language Models (LLMs) to automate the generation of logging statements. The researchers found that while LLMs significantly outperform traditional specialized models, they still struggle with generating complex log text and generalizing to "unseen" code, highlighting a need for better reasoning capabilities and context-aware prompting in software maintenance tools.
What Problem Does This Paper Address?
Logging is a cornerstone of software reliability, aiding in everything from performance analysis to post-mortem failure identification. However, writing effective logging statements—deciding what to log (the level, the variables, and the descriptive text)—is a manual, error-prone task that developers often neglect or perform inconsistently.
Previous attempts to automate this (retrieval-based or small neural network models) were limited. They often focused on just one "ingredient" of a log (e.g., just predicting the log level) and struggled to generalize across different codebases. With the rise of LLMs like GPT-4 and StarCoder, there is a natural question: Can these models generate complete logging statements that feel natural and useful to developers? This paper seeks to answer that by evaluating LLMs' effectiveness and their ability to generalize beyond their training data.
Methods
The authors developed a framework to evaluate 11 different LLMs across three categories:
- General-purpose LLMs: GPT-3.5 (Davinci, ChatGPT) and Llama2.
- Code-based LLMs: InCoder, StarCoder, CodeLlama, and commercial tools like GitHub Copilot and Amazon CodeWhisperer.
- Logging-specific LLMs: LANCE (a T5-based model fine-tuned specifically for logging).
The study formulated logging as a "statement completion" task: given a method with a specific "logging point," the model must predict the missing statement.
The researchers investigated several influencing factors:
- Prompt Construction: The impact of different instructions and the number of few-shot examples.
- External Context: Whether providing code comments or the entire file (file-level context) improves performance.
- Generalization: Testing models on code that has been syntactically transformed to ensure they aren't just "memorizing" GitHub.
Datasets
The authors introduced LogBench, a new benchmark dataset divided into two parts:
- LogBench-O (Original): A collection of 6,849 logging statements from 3,870 Java methods, mined from high-quality GitHub repositories.
- LogBench-T (Transformed): To test generalization and avoid "data leakage" (where the model might have seen the test data during training), the authors applied eight semantics-preserving transformations to LogBench-O. These include swapping condition elements or extracting constants into local variables, creating "unseen" but functionally identical code.
Key Insights and Ideas
- LLMs Outperform Specialists: Even without specific fine-tuning, LLMs beat state-of-the-art conventional logging models (like DeepLV for levels or WhichVar for variables).
- The "Few-Shot" Sweet Spot: While demonstrations help, more is not always better. Performance generally peaks at 5–7 examples. Beyond that, the "model distraction" problem kicks in, where long inputs degrade the model's ability to analyze specific program structures.
- File-Level Context is Critical: Providing the model with the entire file rather than just the target method led to a massive performance boost (e.g., a 49.3% increase in BLEU-4 scores). This allows the model to learn the specific "logging style" and variable definitions used elsewhere in the project.
- The Generalization Gap: All models showed a significant performance drop (8.2% to 16.2%) when faced with the transformed code in LogBench-T. This suggests current LLMs rely heavily on pattern matching and memorization of common coding idioms rather than deep logical reasoning about program state.
Key Results
- Log Levels: Models are quite good at this, with GitHub Copilot correctly predicting the log level (e.g.,
INFOvsDEBUG) 74.3% of the time. - Log Variables and Text: These remain the "hard" parts. While models can identify relevant variables, generating the exact natural language text developers use is difficult, with maximum BLEU-4 scores reaching only ~0.25.
- Model Comparison: Code-based LLMs generally outperformed general-purpose ones. Commercial plugins (Copilot, CodeWhisperer) were the top performers, likely due to their integration of broader project context.
Limitations and Future Work
- Metrics: The authors noted that standard NLP metrics like BLEU and ROUGE are imperfect for logging, as they penalize synonymous text that might be perfectly acceptable to an engineer. They suggest developing unified, semantics-aware metrics.
- Reasoning: The drop in performance on transformed code highlights a need for better inference. The authors suggest that Chain-of-Thought (CoT) prompting—asking the model to first reason about the program's data flow before generating the log—could be a promising future direction.
- Scope: The study was limited to Java. While Java is a dominant language for logging research, future work should explore if these findings hold for other languages like Python or C++.
Summary updated