2

Victor.Chu's Blog

 2 years ago
source link: https://blog.victorchu.info/posts/496918eb/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

logback 性能优化

日志是服务中一个重要组成部分,当优化性能的时候,也要思考对日志的性能优化。

控制台日志

生产环境应该关闭控制台日志。可以减少不必要的开销。

logback 和 log4j 等日志框架获取行号的方式是相同的。即通过 throw 时获取 StackTrace,然后从中解析出 文件名和行号。

public StackTraceElement[] getCallerData() {
if (callerDataArray == null) {
callerDataArray = CallerData
.extract(new Throwable(), fqnOfLoggerClass, loggerContext.getMaxCallerDataDepth(), loggerContext.getFrameworkPackages());
}
return callerDataArray;
}

在生产环境中可以不打印 文件名和行号来减少日志的性能消耗。

如果开启了异步 Appender, 还要注意一个参数 includeCallerData

protected void preprocess(ILoggingEvent eventObject) {
eventObject.prepareForDeferredProcessing();
if (includeCallerData)
eventObject.getCallerData();
}
<appender name="access-async" class="ch.qos.logback.classic.AsyncAppender">
<appender-ref ref="access"/>
<includeCallerData>false</includeCallerData>
</appender>

过滤异常栈

异常栈可以非常有效地帮助定位问题,但是也会因为异常栈太长,包含了太多几乎无价值的信息,比如反射、动态代理、Spring、tomcat 等调用信息。过滤掉这部分信息,既减少了日志量,也减少大对象的数量,降低 Full GC 的次数。常用配置如下:

<property name="commonPattern" value="[%thread][%level][%class{0}:%line]: %msg%n%rEx{full,
java.lang.reflect.Method,
sun.reflect,
org.apache.catalina,
org.springframework.aop,
org.springframework.security,
org.springframework.transaction,
org.springframework.web,
org.springframework.beans,
org.springframework.cglib,
net.sf.cglib,
org.apache.tomcat.util,
org.apache.coyote,
ByCGLIB,
BySpringCGLIB,
com.google.common.cache.LocalCache$
}"/>

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK