用java -jar wiremock-standalone.jar在终端运行WireMock的情况如何?不应该启用控制台日志记录吗?
脚步:
我从以下位置下载了 jar:http : //repo1.maven.org/maven2/com/github/tomakehurst/wiremock-standalone/2.9.0/wiremock-standalone-2.9.0.jar
检查我的java版本并升级到最新,但没有帮助: java升级,但没有帮助
在 simplelogger.properties 文件中,我们可以将默认日志级别设置为
org.slf4j.simpleLogger.defaultLogLevel=error
Run Code Online (Sandbox Code Playgroud)
但是如果要为特定包设置日志记录级别,那么该怎么做呢?例如如果包名是
com.xxx.yyy
那么如果我把它放在 simplelogger.properties 中
com.xxx.yyy.level=error
Run Code Online (Sandbox Code Playgroud)
那么它不起作用。如何配置它?
我有一个在 java 10 上运行的 spring boot 2 应用程序,使用 SLF4J 和 logback 作为底层记录器。
给定以下弹簧组件:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
@Service
public class MyClass {
private static final Logger LOG = LoggerFactory.getLogger(MyClass.class);
public void hello() {
// more logic...
LOG.trace("World");
}
}
Run Code Online (Sandbox Code Playgroud)
以及希望验证日志输出(想象一个更复杂的场景,其中日志将包含变量和上下文信息)或生成日志消息的事实被认为是关键的测试。
import org.junit.Rule;
import org.junit.Test;
import org.springframework.boot.test.rule.OutputCapture;
import static org.hamcrest.Matchers.containsString;
public class MyClassTest {
private final MyClass myClass = new MyClass();
@Rule
public final OutputCapture outputCapture = new OutputCapture();
@Test
public void successShouldLogSuccessMessages() {
myClass.hello();
outputCapture.expect(containsString("World"));
}
}
Run Code Online (Sandbox Code Playgroud)
要通过此测试,必须将日志级别设置为 …
我有一个名为 service1 的 Spring Boot 应用程序,它使用以下属性文件层次结构来配置日志:
bootstrap.yml 有:
spring.application.name = service1application.yml 有:
logging.file: /app/logs/${spring.application.name}.loglogging.level.root: INFOlogging.level.com.myproject.api: TRACE使用的日志框架是Slf4j,通过Lombok的@Sfl4j注解注入到每个类中。spring boot 声称对此有默认支持,确实如此,但是我在/app/logs应用程序启动时看到的是两个日志文件:
/app/logs/myservice1.log :此文件具有正确的位置和名称,但日志级别不是跟踪(仅信息)/app/logs/myservice1-service.log: 这个文件有错误的名字(-service从哪里来的?)但它有正确的日志级别。我注意到这个文件名和这段代码所在的Git中的目录名一致,也是IntelliJ中这段代码的模块名。我无法解释这个名字是如何被 spring“拾取”的,并在运行编译代码时以某种方式用于日志文件名?!我在我的 pom 文件中使用了以下声明来获取 spring 启动,我怀疑除了我声明的 Sfl4j 之外,一些默认的 logback 内容也正在创建一个日志文件。有什么办法可以让我只有一个日志文件,其中包含我指定的名称和正确的日志级别?也许我需要排除依赖?
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>${spring.boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring.cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud) 我正在使用slf4j在控制台和自定义文件中记录自定义异常及其堆栈跟踪。我遇到了一种情况,我不得不截断一些非关键异常的堆栈跟踪。
使用此文档,我在我的logback.xml文件中添加了以下配置
<evaluator name="DISPLAY_EX_EVAL">
<expression>throwable != null && throwable instanceof com.abc.NonCriticalException</expression>
</evaluator>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%-30(%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread]) %-5level
%logger{150} -%msg%n%ex{full, DISPLAY_EX_EVAL}
</pattern>
</encoder>
</appender>
Run Code Online (Sandbox Code Playgroud)
但是,上述配置会在记录配置的异常期间删除所有堆栈跟踪。有没有办法记录匹配异常的截断堆栈跟踪(1 或 2 行)?
我的代码中有以下日志记录语句。
import org.slf4j.Logger;
public class MySampleClass {
private static final Logger logger = LoggerFactory.getLogger(MySmapleClass.class);
public void mySampleMethod(List<String> userID) {
logger.debug("userRuntimeId =" + userId);
.
.
.
Business Logic
.
.
}
}
Run Code Online (Sandbox Code Playgroud)
这将打印我的日志,如下所示,
2019-02-25 16:27:45,460 | 调试 | [fileTaskExecutor-2] | [a.abc.mySampleApp.handlers.userRecordHandler] | [MY_SAMPLE_APP] | [Vijay-20190225-162738.trigger] | [] | userRuntimeId = 3051aa39-2e0a-11e9-bee3-e7388cjg5le0
我想将日志打印为 JSON。我该怎么做?
我期望的示例 JSON 格式:
{
timestamp="2019-02-25 16:27:45,460" ,
level="DEBUG",
triggerName="fileTaskExecutor-2",
className="a.abc.mySampleApp.handlers.userRecordHandler",
appName="MY_SAMPLE_APP",
userRuntimeId="3051aa39-2e0a-11e9-bee3-e7388cjg5le0"
}
Run Code Online (Sandbox Code Playgroud) 我们有一个通过运行 Main 函数启动的 java 应用程序,我们还在 springboot 中启动嵌入式码头作为 webcontainer。我发现 java head 达到了最大大小,但堆使用率很低,java 耗尽了被操作系统杀死的本机内存
pmap中有很多64MB的内存。我转储了一些内存块,发现其中有很多日志。日志时间各不相同,即使过去了几天,日志似乎仍在内存中。例如
2019-03-23T05:50:46,851 661258664 [xxxxxx] INFO
2019-03-27T06:00:12,040 1029308155 [xxxxxxxx] INFO .........
Run Code Online (Sandbox Code Playgroud)
我们使用 log4j2 和 slf4j 作为日志工具。日志由 jetty 线程打印,名称以“qtp”开头。我们设置 immediateFlush="false"和SizeBasedTriggeringPolicy size="10MB"
我不知道为什么日志内容不在堆中。缓存或写入日志时,log4j2 是否使用 Java 本机内存?log4j2内存泄漏可能吗?
我正在重构具有大量依赖项的 Java Web 应用程序。我想使用slf4j, withlog4j2作为底层日志记录实现。但是,该应用程序包含一些 Spring 依赖项。Spring 使用 JCL(Jakarta common logging)进行日志记录,因此它commons-logging作为传递依赖项引入。这是一个潜在的问题,因为这意味着它slf4j可能会jcl作为日志记录实现,并且 Spring 可能会以一种不受欢迎的方式在某处进行日志记录。
从slf4j 文档中,解决方案是首先commons-logging通过从 POM 中排除它来关闭它,然后使用jcl-over-slf4j来替换commons-logging. 当然,jcl-over-slf4j会将 Spring 之前进行的所有日志记录调用路由到slf4j.
我试过排除commons-logging,例如
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
Run Code Online (Sandbox Code Playgroud)
但是,在检查 Maven 依赖关系树时,commons-logging现在仍然显示为 的依赖项json-lib,该项目具有的另一个依赖项。
很快就很明显,手动排除所有不需要的日志依赖项不会很好地扩展。该slf4j文档继续建议使用提供的范围作为选项:
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<scope>provided</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)
但是,正如this SO question所讨论的那样,这种方法仍然意味着在测试期间会出现其他日志记录框架。此外,我不清楚此提供的选项是否适用于 的所有版本commons-logging …
我正在尝试将日志异步打印到日志文件。所以我使用 AsyncAppender 到 RollingFileAppender。但不知何故,Logback 抛出错误未找到附加的附加程序
Logback 版本 logback-classic 1.3.0- alpha5使用
我正在创建一个 RollingFileAppender 并将其添加到 AsyncAppender。
下面是我的 logback.xml
<appender name="FILE-ROLLING"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${logPath}/zapp-info.log</file>
<rollingPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${logPath}/archived/zapp.%d{yyyy-MM-
dd}.%i.log</fileNamePattern>
<SizeBasedTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>10MB</maxFileSize>
</SizeBasedTriggeringPolicy>
<totalSizeCap>20GB</totalSizeCap>
<maxHistory>10</maxHistory>
</rollingPolicy>
<encoder
class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%d %-5level %t %c{0} %msg%n</Pattern>
</encoder>
</appender>
<appender name="ASYNC-INFO"
class="ch.qos.logback.classic.AsyncAppender">
<discardingThreshold>0</discardingThreshold>
<queueSize>256</queueSize>
<appender-ref ref="FILE-ROLLING"/>
</appender>
Run Code Online (Sandbox Code Playgroud)
控制台日志显示以下错误消息
10:26:17,566 |-INFO in ch.qos.logback.core.model.processor.AppenderModelHandler@3ba9ad43 - Processing appender named [ASYNC-INFO]
10:27:29,230 |-INFO in ch.qos.logback.core.model.processor.AppenderModelHandler@3ba9ad43 - About to instantiate appender of type [ch.qos.logback.classic.AsyncAppender]
10:28:24,982 |-ERROR in ch.qos.logback.classic.AsyncAppender[ASYNC-INFO] - No attached appenders found.
10:28:24,982 …Run Code Online (Sandbox Code Playgroud) 我只想在我的应用程序中包含 log4j2 用于日志记录目的。所以我在我的项目的 pom.xml 中包含了以下依赖项:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)
但是在应用程序开始时,我收到以下异常:
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/C:/m2repo/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/C:/m2repo/org/apache/logging/log4j/log4j-slf4j-impl/2.12.1/log4j-slf4j-impl-2.12.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder]
Run Code Online (Sandbox Code Playgroud)
在许多平台上研究更多关于它的信息时,我发现了一种排除 logback 依赖的方法,因为我认为我需要使用 log4j2。但是在排除那个 jar 之后,我也收到以下错误:
Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: org.apache.logging.log4j.LoggingException: log4j-slf4j-impl cannot be present with log4j-to-slf4j
Run Code Online (Sandbox Code Playgroud)
所以为了解决这个问题:我也排除了 log4j-to-slf4j 依赖项。
最后它开始工作,但现在我看到大量与 Spring boot Internal 相关的日志消息。例如 :
2019-12-05 12:05:01.649 [DEBUG] [restartedMain] - Included patterns for restart : [] …Run Code Online (Sandbox Code Playgroud)