
# method日志
#### 实现原理
devspore-http-log打印methodLog主要是通过Spring的切面来实现的。
#### 添加依赖
pom文件添加如下依赖：
```
<dependency>
    <groupId>com.huaweicloud.devspore</groupId>
    <artifactId>devspore-http-log</artifactId>
    <version>最新版本</version> 
</dependency>
```
#### 使用方式
在spring的配置文件中添加以下配置，无需改动代码：
```
devspore:
  http-log:
    method-level:
      #为true时打印methodLog，为false时不打印
      enable: true
      #此参数控制在哪些方法上打印methodLog，使用aop切面表达式的语法
      execution: '(@within(org.springframework.stereotype.Service)'
      #需要在methodLog中添加自定义字段时需添加如下配置
      custom-fields:
        # user：为methodLog中添加的自定义字段名称，x-user为请求头中的key值，devspore-http-log会去请求头中获取该值打印在methodLog中
        user: x-user
```
#### method日志示例
```
"duration": 199,
"logType": "methodLog",
"methodName": "getConsumerByRestTemplate",
"startTime": "2023-12-28 15:23:44.646",
"className": "com.huaweicloud.service.impl.HttpLogServiceImpl",
"exceptionClass": "",
"serviceName": "httplogtest"，
"user": "my-custom-field"
}
```
 #### method日志字段说明
表1method日志字段说明 
| **字段名**        | **字段含义**                                      | **可选/必选**        |
|:---|:---|:---|
| logType        | 日志类型。                                         | 必选，值固定为methodLog |
| serviceName    | 服务名称：取配置文件中：devspore.http-log.service-name的值。 | 必选               |
| className      | 方法所在类的全路径限定名。                                 | 必选               |
| methodName     | 方法名称。                                         | 必选               |
| exceptionClass | 方法异常后异常类全路径限定名（包名使用首字母缩写），方法无异常时此字段为null。     | 必选               |
| startTime      | 方法开始调用时间。                                     | 必选               |
| duration       | 方法耗时（单位：ms）。                                  | 必选               |
| traceId        | 调用链跟踪ID。                                      | 必选（依赖APM）        |
   
