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)slowlog-log-slower-than

设定执行时间,单位是微秒,默认为10000,即10000微妙,10毫秒,执行时长超过该时间的命令将会被记入log。

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

 

2)slowlog-max-len

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

如果日志队列已超出最大长度,则最早的记录会被从队列中清除,是一个FIFO队列

以上两条配置参数,可以通过编辑redis.conf文件配置。

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

 

config方式动态设置slowlog

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

- 设置slowlog-log-slower-than为100ms
    127.0.0.1:6379> CONFIG SET slowlog-log-slower-than 100000
    OK

- 设置slowlog-max-len为1000
    127.0.0.1:6379> CONFIG SET slowlog-max-len 1000
    OK

 

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"

每条慢日志记录,由4个字段构成: 

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

2)以unix时间戳表示的日志记录时间,例如:1515428276

3)命令执行时间,单位:微秒例如:10173微妙,即10.173毫秒

4) 执行的具体命令,例如:SUNIONSTORE

 

slowlog 长度查看

查看当前已记录的慢日志条数(不是配置文件里的最大数量)

命令格式:slowlog len

> slowlog len
(integer) 128

 

slowlog 清零重置

清零重置slow log,日志一旦被删除,将无法恢复。

命令格式:slowlog reset

 

slowlog源码解读

参考:http://blog.sina.com.cn/s/blog_48c95a190101gebh.html 

 

 

参考推荐

redis.conf 配置详解

php-fpm.conf 慢日志

MySQL执行sql及慢查询监控

Nginx 日志轮询及压缩保存天数

Linux 定期自动删除Nginx日志的方法