传统的做法一般是:1、配置里修改日志级别
2、重启应用
3、问题复现查看报错日志排查问题
这个过程需要重启应用,比较麻烦,效率较低,而且针对大型在线项目,不可能随便停机重启。那么有没有一种方式在不重启应用的情况下实现动态修改日志级别呢?
下面,让老万教你如何通过springboot的actuator组件来实现动态修改日志级别。
一、添加依赖<dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-actuator</artifactid></dependency>
二、配置actuator暴露的端口#启用actuator端口management.endpoints.enabled-by-default=fasle#设置actuator的访问根路径,默认是/actuatormanagement.endpoints.web.base-path=/message#启用的端点management.endpoints.web.exposure.include=loggers
这里我修改了actuator的默认访问路径/actuator,改为/message,为的是和项目的基础访问路径保存一致。
启用端口的2中配置方法:方式一:(推荐)
management.endpoints.web.exposure.include=loggers
方式二:(这种方式测试没有生效)
management.endpoint.loggers.enabled=true
补充:如何禁用info端口
management.endpoints.enabled-by-default=falsemanagement.endpoint.info.enabled=true
关于actuator组件被称为spring boot的4大组件之一,功能强大,大家在网上自己找些资料进一步了解。
actuator的endpoint端口说明:id描述默认启用
auditevents 显示当前应用程序的审计事件信息 yes
beans 显示一个应用中所有spring beans的完整列表 yes
conditions 显示配置类和自动配置类(configuration and auto-configuration classes)的状态及它们被应用或未被应用的原因 yes
configprops 显示一个所有@configurationproperties的集合列表 yes
env 显示来自spring的 configurableenvironment的属性 yes
flyway 显示数据库迁移路径,如果有的话 yes
health 显示应用的健康信息(当使用一个未认证连接访问时显示一个简单的"status",使用认证连接访问则显示全部信息详情) yes
info 显示任意的应用信息 yes
liquibase 展示任何liquibase数据库迁移路径,如果有的话 yes
metrics 展示当前应用的metrics信息 yes
mappings 显示一个所有@requestmapping路径的集合列表 yes
scheduledtasks 显示应用程序中的计划任务 yes
sessions 允许从spring会话支持的会话存储中检索和删除(retrieval and deletion)用户会话。使用spring session对反应性web应用程序的支持时不可用。 yes
shutdown 允许应用以优雅的方式关闭(默认情况下不启用) no
threaddump 执行一个线程dump yes
如果使用web应用(spring mvc, spring webflux, 或者 jersey),你还可以使用以下端点:id描述默认启用
heapdum 返回一个gzip压缩的hprof堆dump文件 yes
jolokia 通过http暴露jmx beans(当jolokia在类路径上时,webflux不可用) yes
logfile 返回日志文件内容(如果设置了logging.file或logging.path属性的话),支持使用http range头接收日志文件内容的部分信息 yes
prometheus 以可以被prometheus服务器抓取的格式显示metrics信息 yes
要更改公开哪些端点,请使用以下技术特定的include和exclude属性:propertydefault
management.endpoints.jmx.exposure.exclude *
management.endpoints.jmx.exposure.include *
management.endpoints.web.exposure.exclude *
management.endpoints.web.exposure.include info, health
include属性列出了公开的端点的id,
exclude属性列出了不应该公开的端点的id
exclude属性优先于include属性。包含和排除属性都可以使用端点id列表进行配置。
*可以用来选择所有端点。
例如,要通过http公开除env和beans端点之外的所有内容,请使用以下属性:
management.endpoints.web.exposure.include=*management.endpoints.web.exposure.exclude=env,beans
三、关闭鉴权一般我们会将actuator和spring security鉴权组件结合使用,防止这些功能端口被随便调用。由于这里是功能演示,先放开actuator相关端口的权限认证。
此外,如果存在spring security,则需要添加自定义安全配置,以允许对端点进行未经身份验证的访问,如以下示例所示:放开所有endpoint端点进行匹配
@configurationpublic class actuatorsecurity extends websecurityconfigureradapter { @overrideprotected void configure(httpsecurity http) throws exception { http.requestmatcher(endpointrequest.toanyendpoint()).authorizerequests() .anyrequest().permitall()}}
四 、通过/loggers端口查看日志级别请求链接:http://localhost:8090/message/loggers
注意上面我说过的,我调整了management.endpoints.web.base-path=/message。如果没有设置此参数,则使用默认的/actuator去访问。
五、发起http请求修改日志级别这里演示,修改目录com.wxswj.provider.message.controller的日志级别为debug
请求类型为post,参数格式是json
curl -h "content-type: application/json" -x post --data "{ "configuredlevel": "debug"}" http://localhost:8090/message/loggers/com.wxswj.provider.message.controller
大家可以在服务器上通过curl发起http请求,或者通过postman发起请求。
curl -h "content-type: application/json" -x post --data "{"configuredlevel": "debug"}" http://localhost:8090/loggers/com.wxswj.provider.message.controller
六、查询日志级别修改结果http://localhost:8090/message/loggers/com.wxswj.provider.message.controller
{"configuredlevel": "debug","effectivelevel": "debug"}
说明我们的修改日志级别的请求生效。
以上就是springboot动态修改日志级别的操作是什么的详细内容。