Skip to content

Commit f092cfc

Browse files
author
YunaiV
committed
增加 CAT 入门
1 parent 404f28d commit f092cfc

File tree

7 files changed

+124
-2
lines changed

7 files changed

+124
-2
lines changed

lab-61/lab-61-demo/src/main/java/cn/iocoder/springboot/lab61/catdemo/controller/DemoController.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class DemoController {
1717
@GetMapping("/transaction")
1818
public String transaction() {
1919
// 创建 Transaction 对象
20-
Transaction t = Cat.newTransaction("URL", "pageName");
20+
Transaction t = Cat.newTransaction("URL", "/demo/transaction");
2121
try {
2222
// ... yourBusiness(); 业务逻辑
2323

@@ -39,7 +39,7 @@ public String transaction() {
3939
@GetMapping("/event-01")
4040
public String event01() {
4141
// Cat.logEvent("URL.Server", "127.0.0.1");
42-
Cat.logEvent("URL.Server2", "127.0.0.1", Event.SUCCESS, "data");
42+
Cat.logEvent("URL.Server", "127.0.0.1", Event.SUCCESS, "data");
4343
return "success";
4444
}
4545

lab-61/lab-61-logback/pom.xml

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>lab-61</artifactId>
7+
<groupId>cn.iocoder.springboot.labs</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>lab-61-logback</artifactId>
13+
14+
<properties>
15+
<maven.compiler.target>1.8</maven.compiler.target>
16+
<maven.compiler.source>1.8</maven.compiler.source>
17+
<spring.boot.version>2.2.4.RELEASE</spring.boot.version>
18+
</properties>
19+
20+
<dependencyManagement>
21+
<dependencies>
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-starter-parent</artifactId>
25+
<version>${spring.boot.version}</version>
26+
<type>pom</type>
27+
<scope>import</scope>
28+
</dependency>
29+
</dependencies>
30+
</dependencyManagement>
31+
32+
<dependencies>
33+
<!-- 引入 SpringMVC 相关依赖,并实现对其的自动配置 -->
34+
<dependency>
35+
<groupId>org.springframework.boot</groupId>
36+
<artifactId>spring-boot-starter-web</artifactId>
37+
</dependency>
38+
39+
<!-- 引入 CAT 相关依赖 -->
40+
<dependency>
41+
<groupId>com.dianping.cat</groupId>
42+
<artifactId>cat-client</artifactId>
43+
<version>3.0.0</version>
44+
</dependency>
45+
</dependencies>
46+
47+
<!-- 引入私服,因为 CAT 依赖并没有发到 Maven 中央仓库 -->
48+
<repositories>
49+
<repository>
50+
<id>central</id>
51+
<name>Maven2 Central Repository</name>
52+
<layout>default</layout>
53+
<url>http://repo1.maven.org/maven2</url>
54+
</repository>
55+
<repository>
56+
<id>unidal.releases</id>
57+
<url>http://unidal.org/nexus/content/repositories/releases/</url>
58+
</repository>
59+
</repositories>
60+
61+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package cn.iocoder.springboot.lab61.catdemo;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class Application {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(Application.class, args);
11+
}
12+
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package cn.iocoder.springboot.lab61.catdemo.controller;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
import org.springframework.web.bind.annotation.GetMapping;
6+
import org.springframework.web.bind.annotation.RequestMapping;
7+
import org.springframework.web.bind.annotation.RestController;
8+
9+
@RestController
10+
@RequestMapping("/logger")
11+
public class LoggerController {
12+
13+
private Logger logger = LoggerFactory.getLogger(getClass());
14+
15+
@GetMapping("/error")
16+
public String error() {
17+
try {
18+
int result = 1 / 0;
19+
} catch (Throwable e) {
20+
logger.error("计算异常", e);
21+
}
22+
return "success";
23+
}
24+
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
app.name=demo-application
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<configuration>
4+
5+
<!-- 参考 -->
6+
<include resource="org/springframework/boot/logging/logback/defaults.xml" />
7+
<property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}}/spring.log}"/>
8+
<include resource="org/springframework/boot/logging/logback/console-appender.xml" />
9+
<include resource="org/springframework/boot/logging/logback/file-appender.xml" />
10+
11+
<!-- 定义 Sentry Appender -->
12+
<appender name="CatAppender" class="com.dianping.cat.logback.CatLogbackAppender" />
13+
14+
<!-- 日志输出级别 -->
15+
<root level="INFO">
16+
<appender-ref ref="CONSOLE" />
17+
<appender-ref ref="FILE" />
18+
<appender-ref ref="CatAppender" />
19+
</root>
20+
21+
</configuration>

lab-61/pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<packaging>pom</packaging>
1414
<modules>
1515
<module>lab-61-demo</module>
16+
<module>lab-61-logback</module>
1617
</modules>
1718

1819
</project>

0 commit comments

Comments
 (0)