先复习一下常用的log框架。
SLF4J是一个标准的API集合,log4j实现了它的接口,log4j已经在2015年停用,它有两个衍生版本,Logback 和 Log4j2,二者皆自动支持SLF4J,区别为log4j2更新潮,可以说是基于log4j和logback的升级版。
参考:https://stackify.com/compare-java-logging-frameworks/
AppInsight 可以使用TelemetryClient来记录event等,也可以直接对log4j和logback进行集成,以下为对logback进行集成的方式:
1.pom.xml文件加入:
<dependencies>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>applicationinsights-spring-boot-starter</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>applicationinsights-logging-logback</artifactId>
<version>[2.0,)</version>
</dependency>
</dependencies>
2.appplication configure 文件加入:
# Specify the instrumentation key of your Application Insights resource.azure.application-insights.instrumentation-key=[your ikey from the resource]
# Specify the name of your springboot application. This can be any logical name you would like to give to your app.spring.application.name=[your app name]
3.logback.xml 配置加入:
<springProperty scope="context" name="INSTRUMENTATION_KEY"
source="azure.application-insights.instrumentation-key"/>
<appender name="aiAppender"
class="com.microsoft.applicationinsights.logback.ApplicationInsightsAppender">
<instrumentationKey>${INSTRUMENTATION_KEY}</instrumentationKey>
</appender>
<root level="INFO">
<appender-ref ref="aiAppender" />
</root>
得到结果查看:
![](https://img.haomeiwen.com/i14018327/d12a84b4a3425960.png)
https://docs.microsoft.com/en-us/azure/azure-monitor/app/java-trace-logs
https://blog.akquinet.de/2019/06/14/logging-with-azure-application-insights-part-1-spring-boot/
https://github.com/microsoft/ApplicationInsights-Java/issues/880
网友评论