Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/194.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JMeter Groovy:print to log in类(静态)(在静态作用域中找到明显的变量';log';但不引用局部变量…)_Groovy_Jmeter_Static Methods - Fatal编程技术网

JMeter Groovy:print to log in类(静态)(在静态作用域中找到明显的变量';log';但不引用局部变量…)

JMeter Groovy:print to log in类(静态)(在静态作用域中找到明显的变量';log';但不引用局部变量…),groovy,jmeter,static-methods,Groovy,Jmeter,Static Methods,我想在JMeter中的Groovy中的静态类中编写日志。我可以使用println或System.out.println打印到课堂内外的终端。如何使用log?在下面的代码中,除了类内的log.warn之外,所有工作都会出现错误: Script53.groovy: 13: Apparent variable 'log' was found in a static scope but doesn't refer to a local variable, static field or class. P

我想在JMeter中的Groovy中的静态类中编写日志。我可以使用
println
System.out.println
打印到课堂内外的终端。如何使用
log
?在下面的代码中,除了类内的log.warn之外,所有工作都会出现错误:

Script53.groovy: 13: Apparent variable 'log' was found in a static scope but doesn't refer to a local variable, static field or class. Possible causes:
You attempted to reference a variable in the binding or an instance variable from a static context.
You misspelled a classname or statically imported field. Please check the spelling.
You attempted to use a method 'log' but left out brackets in a place not allowed by the grammar. 
代码:

我尝试了网络搜索,但找不到答案。

我已经阅读了建议使用
log
变量的地方,但也提供了指向类的链接。我尝试使用
Logger
类,它可以工作:

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

class CalcMain {
    static void main(def args) throws Exception {
    ScriptEngineManager factory = new ScriptEngineManager();
    ScriptEngine engine = factory.getEngineByName("groovy");

    final Logger logger = LoggerFactory.getLogger(CalcMain.class);
    logger.warn("My warning");
    }
}
CalcMain test1 = new CalcMain();
test1.main();
我已经阅读了建议使用
log
变量的地方,但也给出了类的链接。我尝试使用
Logger
类,它可以工作:

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

class CalcMain {
    static void main(def args) throws Exception {
    ScriptEngineManager factory = new ScriptEngineManager();
    ScriptEngine engine = factory.getEngineByName("groovy");

    final Logger logger = LoggerFactory.getLogger(CalcMain.class);
    logger.warn("My warning");
    }
}
CalcMain test1 = new CalcMain();
test1.main();

另一个选项是将日志作为参数发送到静态方法:

static void main(org.slf4j.Logger log) throws Exception {
调用方法:

test1.main(log);

另一个选项是将日志作为参数发送到静态方法:

static void main(org.slf4j.Logger log) throws Exception {
调用方法:

test1.main(log);

您可以使用Groovy
@Log4j
注释:

import groovy.util.logging.Log4j

@Log4j
class CalcMain {
  static void main(def args) throws Exception {
    // some code
    log.info "hello there"
  }
}


您可以使用Groovy
@Log4j
注释:

import groovy.util.logging.Log4j

@Log4j
class CalcMain {
  static void main(def args) throws Exception {
    // some code
    log.info "hello there"
  }
}


我想说,你必须在类中定义一个静态
log
变量,或者在@injecteer方法中定义一个局部变量,谢谢你,找到了你建议的工作方式。同样应该工作的是使用
@Log4j
groovy annotation@injecteer,执行
@Log4j log.warn(“warn2”)给出了
意外标记:log@line 20,column 5.
<代码>无法解析类Log4j,无法找到用于注释的类
import groovy.util.logging.Log4j
我想说,您必须在类中定义一个静态
log
变量,或者在@injecteer方法中定义一个局部变量,谢谢您,找到了您建议的工作方式。还应该使用
@Log4j
groovy annotation@injecteer,doing
@Log4j log.warn(“warn2”);给出了
意外标记:log@line 20,column 5.
<代码>无法解析类Log4j,无法找到用于注释的类<代码>导入groovy.util.logging.Log4j