Gatling - 如何在scala中设置gatling控制台日志级别

时间:2018-04-26 08:37:34

标签: scala logging jar gatling

我创建了一个maven项目,它将生成一个包含所有模拟的jar文件,当我运行它时,控制台日志级别太高。对我来说,有太多无用的信息。 有没有办法在代码中配置它?这是我的代码:

import io.gatling.app.Gatling
import io.gatling.core.config.GatlingPropertiesBuilder 
import io.gatling.core.config.GatlingConfiguration

object Engine extends App {
    val props = new GatlingPropertiesBuilder
    if(System.getProperty("resultsFolder") == null){
      props.resultsDirectory("results")
    }else{
      props.resultsDirectory(System.getProperty("resultsFolder"))
    }

    props.dataDirectory("data")
      props.simulationClass(System.getProperty("simulationClass"))

    Gatling.fromMap(props.build)
  sys.exit()
}

这是我目录中的树:

¦   dependency-reduced-pom.xml
¦   pom.xml
¦
+---src
    +---main
    ¦   +---resources
    ¦   +---scala
    ¦       +---myPackage
    ¦                   ¦   Engine.scala
    ¦                   ¦
    ¦                   +---simulation
    ¦                           BasicSimulation.scala
    ¦
    +---test
        +---resources
        ¦       application.conf
        ¦       gatling.conf
        ¦       logback-test.xml
        ¦
        +---scala
                Placeholder.scala

.config文件和logback是Gatling的默认文件。

3 个答案:

答案 0 :(得分:1)

我找到了一个解决方案:将日志级别放在Engine中,而不是重新发送logbaxk.xml文件:

import io.gatling.app.Gatling
import io.gatling.core.config.GatlingPropertiesBuilder 
import io.gatling.core.config.GatlingConfiguration

object Engine extends App {
 LoggerFactory.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME).asInstanceOf[Logger].setLevel(Level.WARN)
    val props = new GatlingPropertiesBuilder
    if(System.getProperty("resultsFolder") == null){
      props.resultsDirectory("results")
    }else{
      props.resultsDirectory(System.getProperty("resultsFolder"))
    }

    props.dataDirectory("data")
      props.simulationClass(System.getProperty("simulationClass"))

    Gatling.fromMap(props.build)
  sys.exit()
}

答案 1 :(得分:0)

我像mvn clean gatling:execute@slalom -DLOG_LEVEL=WARN

那样跑步

WARN替换为任何其他级别,它将如此显示。

然后在编译期间直接在logback-test.xml中替换LOG_LEVEL,如下所示:

<?xml version="1.0" encoding="UTF-8"?>

<properties>
    <LOG_LEVEL>WARN</LOG_LEVEL>
</properties>

<contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator">
    <resetJUL>true</resetJUL>
</contextListener>

<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
        <pattern>%d{HH:mm:ss.SSS} [%-5level] %logger{15} - %msg%n%rEx</pattern>
        <immediateFlush>false</immediateFlush>
    </encoder>
</appender>

<!-- Uncomment for logging ALL HTTP request and responses -->
    <logger name="io.gatling.http" level="${LOG_LEVEL}" default = "WARN"/>
<!-- Uncomment for logging ONLY FAILED HTTP request and responses -->
<!--<logger name="io.gatling.http" level="WARN" />-->

<root level="${LOG_LEVEL}" default="WARN">
    <appender-ref ref="CONSOLE" />
</root>
    - &GT;

答案 2 :(得分:0)

这是您的操作方式:

package gatling.simulations

import io.gatling.core.Predef._
import io.gatling.http.Predef._
import scala.concurrent.duration._
import org.slf4j.LoggerFactory
import ch.qos.logback.classic.{Level, LoggerContext}

class FooSimulation extends Simulation {

  val context: LoggerContext = LoggerFactory.getILoggerFactory.asInstanceOf[LoggerContext]
  // Log all HTTP requests
  context.getLogger("io.gatling.http").setLevel(Level.valueOf("TRACE"))
  // Log failed HTTP requests
  //context.getLogger("io.gatling.http").setLevel(Level.valueOf("DEBUG"))
   ...