所以对我来说,我试图记录执行时间,我创建了一个名为ExpersionTime的pojo,其中包含名称,方法,类,持续时间。
然后我能够创建它:
ExecutionTime time = new ExecutionTime("Controller Hit", methodName, className, sw.getTotalTimeMillis());
对于日志记录,我随后使用了:
private final Logger logger = LoggerFactory.getLogger(this.getClass());
logger.info(append("metric", time), time.toString());
确保您拥有:
import static net.logstash.logback.marker.Markers.append;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
这将记录如下内容:
{
"ts":"2017-02-16T07:41:36.680-08:00",
"msg":"ExecutionTime [name=Controller Hit, method=setupSession, className=class com.xxx.services.controllers.SessionController, duration=3225]",
"logger":"com.xxx.services.metrics.ExecutionTimeLogger",
"level":"INFO",
"metric":{
"name":"Controller Hit",
"method":"setupSession",
"className":"class com.xxx.services.controllers.SessionController",
"duration":3225
}
}
可能是一个不同的设置,因为我使用logback-spring.xml将我的日志输出到json:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<property name="PROJECT_ID" value="my_service"/>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>app/logs/${PROJECT_ID}.json.log</File>
<encoder class="net.logstash.logback.encoder.LogstashEncoder">
<fieldNames>
<timestamp>ts</timestamp>
<message>msg</message>
<thread>[ignore]</thread>
<levelValue>[ignore]</levelValue>
<logger>logger</logger>
<version>[ignore]</version>
</fieldNames>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<maxIndex>10</maxIndex>
<FileNamePattern>app/logs/${PROJECT_ID}.json.log.%i</FileNamePattern>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>20MB</MaxFileSize>
</triggeringPolicy>
</appender>
<logger name="com.xxx" additivity="false" level="DEBUG">
<appender-ref ref="FILE"/>
<appender-ref ref="CONSOLE"/>
</logger>
<root level="WARN">
<appender-ref ref="FILE"/>
</root>
</configuration>