【问题标题】:IntelliJ IDEA Live Template to generate log statementIntelliJ IDEA Live Template 生成日志语句
【发布时间】:2021-12-22 23:13:53
【问题描述】:
@Slf4j    
public class LogGenerator {
        public void testLog() {
            String customVariable1 = "Hello";
            String customVariable2 = "World";
    
            log.info("LogGenerator::testLog, customVariable1 : {}, customVariable2 : {}", customVariable1, customVariable2);
        }
    }

有没有办法使用 Live Template 热键生成日志语句?

【问题讨论】:

  • 您需要将变量传递给日志语句吗?你能显示预期的结果吗?
  • @y.bedrov 日志将是“LogGenerator::testLog, customVariable1 : Hello, customVariable2 : World”
  • 抱歉,您能否提供之前/之后的示例来澄清问题?

标签: java android-studio intellij-idea jetbrains-ide intellij-14


【解决方案1】:

Step1:添加模板文本

Step2:编辑变量,METHOD_NAME是IDEA定义的,只需要修改ARGUMENTS的默认值即可。

第三步:将 groovy 脚本添加到默认值

groovyScript(" def result=''; def result1=''; def params=\"${_1}\".replaceAll('[\\\\[|\\\\]|\\\\s]', '').split(',').toList(); for(i = 0; i < params.size(); i++) {     result1 += ', ' + params[i];     if (i==0) {         result+='\+\" ' + params[i] + ': {}, ';      } else {         result+= params[i] + ': {}, ';      } };  return result + '\" ' + result1;  ", methodParameters());}} 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-03
    • 2012-10-06
    • 2015-05-31
    • 2021-07-11
    • 2014-02-14
    • 1970-01-01
    • 2021-10-29
    • 1970-01-01
    相关资源
    最近更新 更多