3

我想在 Netbeans 中为记录器编写代码模板:

org.slf4j.LoggerFactory.getLogger(XXXXXX.class).warn("${cursor}");

我在reference中找不到将当前类名插入模板(请参阅上面的XXXXXX占位符)的语法。

4

2 回答 2

7

我创建了以下代码模板:

private static final ${LOGGER_TYPE type="org.slf4j.Logger" default="Logger" editable=false} LOGGER = ${LOGGER_FACTORY type="org.slf4j.LoggerFactory" default="LoggerFactory" editable=false}.getLogger(${CLASS editable="false" currClassName}.class);

我已将它与缩写相关联log

所以在光标处:

...
public class MyClass {
    |
...

我键入log+TAB并且 NetBeans 展开文本:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
...
public class MyClass {
    private static final Logger LOGGER = LoggerFactory.getLogger(MyClass.class);
...

我也有:

  • logd+TAB
  • logw+TAB
  • logi+TAB
  • loge+TAB

编码为:

${LOGGER_CONST default="LOGGER" editable=false}.debug("${logMessage}");
${LOGGER_CONST default="LOGGER" editable=false}.warn("${logMessage}");
${LOGGER_CONST default="LOGGER" editable=false}.info("${logMessage}");
${LOGGER_CONST default="LOGGER" editable=false}.error("${logMessage}", ex);

生成:

LOGGER.debug("logMessage");
LOGGER.warn("logMessage");
LOGGER.info("logMessage");
LOGGER.error("logMessage", ex);
于 2015-01-29T13:27:08.757 回答
1

要获取当前类名,您需要在代码模板中使用它:${classVar editable=false currClassName default=getClass()}

于 2013-10-09T18:57:06.157 回答