Logging practices have been extensively investigated to assist developers in writing appropriate logging statements for documenting software behaviors. Although numerous automatic logging approaches have been proposed, their performance remains unsatisfactory due to the constraint of the single-method input, without informative programming context outside the method. Specifically, we identify three inherent limitations with single-method context: limited static scope of logging statements, inconsistent logging styles, and missing type information of logging variables. To tackle these limitations, we propose SCLogger, the first contextualized logging statement generation approach with inter-method static contexts. First, SCLogger extracts inter-method contexts with static analysis to construct the contextualized prompt for language models to generate a tentative logging statement. The contextualized prompt consists of an extended static scope and sampled similar methods, ordered by the chain-of-thought (COT) strategy. Second, SCLogger refines the access of logging variables by formulating a new refinement prompt for language models, which incorporates detailed type information of variables in the tentative logging statement. The evaluation results show that SCLogger surpasses the state-of-the-art approach by 8.7% in logging position accuracy, 32.1% in level accuracy, 19.6% in variable precision, and 138.4% in text BLEU-4 score. Furthermore, SCLogger consistently boosts the performance of logging statement generation across a range of large language models, thereby showcasing the generalizability of this approach.
翻译:日志实践已被广泛研究,以帮助开发者编写合适的日志语句来记录软件行为。尽管已有大量自动化日志方法被提出,但由于受限于单方法输入(缺乏方法外部的信息性编程上下文),其性能仍不尽如人意。具体而言,我们识别出单方法上下文存在三个固有限制:日志语句的静态作用域有限、日志风格不一致、以及日志变量类型信息缺失。为解决这些限制,我们提出了SCLogger——首个融合方法间静态上下文的上下文感知日志语句生成方法。首先,SCLogger通过静态分析提取方法间上下文,构建上下文提示以引导语言模型生成初步日志语句。该上下文提示由扩展的静态作用域和按思维链策略排序的相似方法样本组成。其次,SCLogger通过为语言模型设计新的优化提示来细化日志变量的访问,该提示将初步日志语句中变量的详细类型信息纳入其中。评估结果表明,SCLogger在日志位置准确率、级别准确率、变量精确率和文本BLEU-4分数上分别超越当前最先进方法8.7%、32.1%、19.6%和138.4%。此外,SCLogger能持续提升多种大语言模型的日志语句生成性能,从而展示了该方法的泛化能力。