美文网首页
springboot动态修改系统日志级别

springboot动态修改系统日志级别

作者: 同桌的桌 | 来源:发表于2017-09-12 18:29 被阅读0次

    spring1.5.X版本引入的一个新的控制端点:/loggers,该端点将为我们提供动态修改Spring Boot应用日志级别的强大功能。该功能的使用非常简单,它依然延续了Spring Boot自动化配置的实现,所以只需要在引入了spring-boot-starter-actuator依赖的条件下就会自动开启该端点的功能(更多关于spring-boot-starter-actuator
    模块的详细介绍可见:《springboot中使用actuator进行监控》一文)。

    配置:

    pom依赖:

    <!--actuator-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
    

    yml配置:

    ##运行状态 actuator监控
    endpoints:
      loggers:
        enabled: true
        sensitive: false
    management:
      ##服务路径
      context-path: /manage
      ##服务端口
      port: 8081
    

    然后启动项目,可以看到端口已经映射成功

    ... o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/manage/loggers/{name:.*}],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.LoggersMvcEndpoint.get(java.lang.String)
    ... o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/manage/loggers/{name:.*}],methods=[POST],consumes=[application/vnd.spring-boot.actuator.v1+json || application/json],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.LoggersMvcEndpoint.set(java.lang.String,java.util.Map<java.lang.String, java.lang.String>)
    ... o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/manage/loggers || /manage/loggers.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
    

    查询日志级别:

    使用GET请求:

    /manage/loggers/
    

    会返回所有的日志级别:

    {
        "levels":[
            "OFF",
            "ERROR",
            "WARN",
            "INFO",
            "DEBUG",
            "TRACE"
        ],
        "loggers":{
            "ROOT":{
                "configuredLevel":"INFO",
                "effectiveLevel":"INFO"
            },
            "com":{
                "configuredLevel":null,
                "effectiveLevel":"INFO"
            },
            "com.caiyi":{
                "configuredLevel":null,
                "effectiveLevel":"INFO"
            }
            ...
        }
    }
    

    修改日志级别:

    使用POST请求:

    /manage/loggers/{elephant}
    

    {elephant}为前面查询到的目录。
    比如我修改com.caiyi下面的日志级别为debug,访问:

    http:127.0.0.1:8081/manage/loggers/com.caiyi
    

    请求body中参数:

    {
        "configuredLevel": "debug"
    }
    

    然后再调用查询接口就会发现已经改为debug级别了

    相关文章

      网友评论

          本文标题:springboot动态修改系统日志级别

          本文链接:https://www.haomeiwen.com/subject/gwbcsxtx.html