Press "Enter" to skip to content

Redis 慢日志 slowlog

Redis 慢日志命令为 slowlog,用于读取和重置redis慢请求日志配置。

slowlog 简介

Redis的slow log记录了那些执行时间超过规定时长的请求,其执行时间不包括I/O操作(比如与客户端进行网络通信等)。


Redis 慢日志slowlog可在配置文件中设置:
vim /etc/redis.conf

# The Redis Slow Log is a system to log queries that exceeded a specified
# execution time. The execution time does not include the I/O operations
# like talking with the client, sending the reply and so forth,
# but just the time needed to actually execute the command (this is the only
# stage of command execution where the thread is blocked and can not serve
# other requests in the meantime).
# You can configure the slow log with two parameters: one tells Redis
# what is the execution time, in microseconds, to exceed in order for the
# command to get logged, and the other parameter is the length of the
# slow log. When a new command is logged the oldest one is removed from the
# queue of logged commands.

# The following time is expressed in microseconds, so 1000000 is equivalent
# to one second. Note that a negative number disables the slow log, while
# a value of zero forces the logging of every command.
slowlog-log-slower-than 10000

# There is no limit to this length. Just be aware that it will consume memory.
# You can reclaim memory used by the slow log with SLOWLOG RESET.
slowlog-max-len 128

其有两个参数用于配置slow log:

其值若为 -1 表示不记录slow log;0 表示强制记录所有命令。


slow log的长度,默认为128,表示最多记录128条日志,其最小值为0。

对运行中的redis, 可以通过config get, config set命令动态改变上述两个参数


- 查看当前slowlog-log-slower-than设置> CONFIG GET slowlog-log-slower-than
    1) "slowlog-log-slower-than"
    2) "10000"

- 设置slowlog-log-slower-than为100ms> CONFIG SET slowlog-log-slower-than 100000

- 设置slowlog-max-len为1000> CONFIG SET slowlog-max-len 1000

slowlog 慢日志查看

slow log是记录在内存中的,所以即使你记录所有的命令(将slowlog-log-slower-than设为0),对性能的影响也很小。
slowlog get: 列出所有slow log
slowlog get N:列出最近N条slow log

例如: slowlog get 3 表示输出最新三条慢日志记录


> slowlog get 3
1) 1) (integer) 8704
   2) (integer) 1515428276
   3) (integer) 10173
   4) 1) "SUNIONSTORE"
      2) "mimvp_proxy:fetch:key_port"
      3) "mimvp_proxy:fetch:key_port"
      4) "mimvp_proxy:set:total"
2) 1) (integer) 8703
   2) (integer) 1515428275
   3) (integer) 11816
   4) 1) "DEL"
      2) "mimvp_proxy:fetch:key_result_24h_ago"
3) 1) (integer) 8702
   2) (integer) 1515428272
   3) (integer) 11167
   4)  1) "SINTERSTORE"
       2) "mimvp_proxy:fetch:key_result"
       3) "mimvp_proxy:fetch:key_country"
       4) "mimvp_proxy:fetch:key_port"
       5) "mimvp_proxy:fetch:key_http_type"


1)表示该条slow log的唯一id,累加的,例如:8704,只有当reids重启后,id编号才会被重置
4) 执行的具体命令,例如:SUNIONSTORE

slowlog 长度查看

命令格式:slowlog len

> slowlog len
(integer) 128

slowlog 清零重置

清零重置slow log,日志一旦被删除,将无法恢复。
命令格式:slowlog reset

Be First to Comment


电子邮件地址不会被公开。 必填项已用*标注