【问题标题】:JsonPath ignore the Debug logs on outputJsonPath 忽略输出中的调试日志
【发布时间】:2018-12-10 22:47:53
【问题描述】:

我使用JsonPath 进行Java 中的JSON 解析工作。有什么方法可以在运行代码时删除debug 日志?

所以基本上,我只是想在 Maven 中运行我的解析代码:

String pageName = JsonPath.read(json, "$['pageInfo']['pageName']");
        System.out.println(pageName);

但是在运行jar 工件文件时,它会在第一行显示以下内容:

0 [main] DEBUG com.jayway.jsonpath.internal.path.CompiledPath - Evaluating path: $['pageInfo']['pageName']

如何忽略这一行?这会在运行每个 JsonPath.read() 调用时出现。

更新

最初我从log4j 获得了一些红色日志,因此添加了这些依赖项。红色的原木消失了,但上面的原木(现在是黑色的)出现了!

<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.5</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.5</version>
</dependency>

我还添加了 logBack 依赖项。但是仍然无法识别代码sn-p:

【问题讨论】:

  • 如果只是警告,你能不忽略吗?
  • 老实说!不熟悉这些
  • 看答案。

标签: java json jsonpath jsonparser json-path-expression


【解决方案1】:

这个问题实际上是在 2017 年在他们的官方 github 页面上提出的。

看起来你需要使用 logback 作为日志实现

这是 andreasaronsson 从 github issue 提供的代码

LoggerContext logContext = (LoggerContext) LoggerFactory.getILoggerFactory();
ch.qos.logback.classic.Logger log = logContext.getLogger("com.jayway.jsonpath.internal.path.CompiledPath");
log.setLevel(Level.INFO);

你的依赖项中需要这个

<dependencies>
   <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-api</artifactId>
      <version>2.11.1</version>
   </dependency>
   <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-core</artifactId>
      <version>2.11.1</version>
   </dependency>
   <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-classic</artifactId>
      <version>1.2.3</version>
   </dependency>
</dependencies>

有关该问题的更多信息,您可以找到here

【讨论】:

  • 什么是ch?这个 sn-p 不起作用。有什么东西不见了。
  • ch是一个包,看github issue。你需要那个Logback
  • 这个问题本身并没有帮助。我是否需要导入某些内容或添加依赖项才能使此代码正常工作?!
  • 检查更新的答案,我已经为dependencies提供了xml
  • 另外我认为删除 &lt;scope&gt;test&lt;/scope&gt; 将有助于我不使用 maven。
【解决方案2】:

您使用的是什么日志记录框架?只需将com.jayway.jsonpath logger 的级别设置为 INFO 或更高。下面是使用 LogbackLog4j 2 的 XML 配置的示例。

回退

<configuration>
  …
  <logger name="com.jayway.jsonpath" level="INFO"/>
  <root level="DEBUG">          
    <appender-ref ref="STDOUT" />
  </root>  
</configuration>

Log4j 2

<Configuration status="WARN">
  …
  <Loggers>
    <Logger name="com.jayway.jsonpath" level="info">
      <AppenderRef ref="STDOUT"/>
    </Logger>
    <Root level="debug">
      <AppenderRef ref="STDOUT"/>
    </Root>
  </Loggers>
</Configuration>

【讨论】:

  • 我自己什么都没用!但显然 jsonPath 需要log4j,所以我将它添加到我的依赖项中。它显示了一些红色消息,所以我添加了一些代码来忽略它们。但是这个出现了!
  • 顺便说一句,在哪里添加这些?查看我的问题中的更新。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-08-02
  • 2021-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多