转:Mosquitto配置----日志设置

时间:2023-03-08 19:50:44

1.mosquitto的日志输出方式简介

mosquitto是一个纯C的代码,它的日志输出支持若干中日志输出方式,通过修改配置项:log_dest即可完成对各种日志输出类型的切换,常见的日志输出类型有有下几种:

(1)控制台输出stdout、stderrr

log_dest stderr

(2)输出到日志文档

log_dest file /home/logs/mosquitto/mosquitto.log

【注意】

[1] log_dest后面还有个参数file,然后才是实际的日志文件全名;

[2] 所使用的日志文件/home/logs/mosquitto/mosquitto.log要先建立好,并且让mosquitto有权限访问;

(3)发送到日志系统syslog

log_dest syslog

如果要配置了该项目,要同时配置参数log_facility,在centos7下,日志被输出到了:/var/log/messages中,但是messages中有系统的各种使用了syslog的应用的日志,凡是mosquitto的日志输出都有mosquitto字段,如下所示:

May 23 12:05:32 localhostmosquitto[18506]: Socket error on client 1000024, disconnecting.

May 23 12:05:32 localhostmosquitto[18506]: Socket error on client 1000025, disconnecting.

除了上述方式之外,mosquitto还可以把所有的日志都pub到自己的某个主题上,不过这种方式不太常用。

下面将重点介绍将日志输出到文件的方式:

在生产环境中,我们一般都是将日志输出到指定的文件,然后再通过日志收集系统对日志进行统一的收集和处理,但是mosquitto的日志输出比较简单,在以文件方式输出日志时,无法对日志文件进行转储,即它会一直向配置文件里面指定的那个日志文件中输出日志,随着线上运营时间的增加,该日志文件会慢慢变得非常大,如果不定期清理,超过10几G都很正常,我们保留日志文件就是为了后续分析问题,如果直接清空日志文件就达不到这个目标,如果不清空,终归会把硬盘撑爆,另外,太大的日志文件也不利于分析问题;

针对mosquitto的文件方式输出日志内容存在的这些问题,可以通过linux系统自带的logrotate来对mosquitto生成的日志文件按照日期、大小等进行转储,让logrotate定期把日志文件转储一下,并且保留指定数量的日志转储文件,以满足线上运营的需求。

2. logrotate的简单介绍:

logrotate是linux系统自带的日志文件管理工具,它可以帮助我们来完成对某个日志文件的转储,当然它还有其他的功能,例如:将日志发送到指定的email等等;它实际的工作机制非常简单:在linux系统下,Crontab每天都会执行logrotate,而logrotate每执行一次,就把我们的日志转储一次;

logrotate通过与定时任务Crontab结合来工作,就能满足定期,例如每天对日志文件进行转储的功能,其原理是:

我们知道Crontab在Centos下的/etc目录中有几个定时执行的脚本目录,例如:/etc/cron.daily,在该文件夹下保存了Crontab每天都会定时执行的脚本,当然目录/etc/cron.weekly下记录Crontab每周都会定时执行的脚本....;我们可以看下在目录/etc/cron.daily中有个脚本文件:logrotate,这个脚本的内容为:

#!/bin/sh

 

/usr/sbin/logrotate /etc/logrotate.conf

EXITVALUE=$?

if [ $EXITVALUE != 0 ]; then

/usr/bin/logger -t logrotate "ALERTexited abnormally with [$EXITVALUE]"

fi

exit 0

从脚本的内容就可以看出,它主要用于执行程序logrotate,并且启动logrotate的时候使用了配置文件:/etc/logrotate.conf。下面就是对这个配置文件的内容进行简单的介绍:

# see "man logrotate" fordetails

# rotate log files weekly

weekly

# keep 4 weeks worth of backlogs

rotate 4

# create new (empty) log files afterrotating old ones

create

# use date as a suffix of the rotated file

dateext

# uncomment this if you want your logfiles compressed

#compress

# RPM packages drop log rotationinformation into this directory

include /etc/logrotate.d

# no packages own wtmp and btmp -- we'llrotate them here

/var/log/wtmp {

monthly

create 0664 root utmp

minsize 1M

rotate 1

}

/var/log/btmp {

missingok

monthly

create 0600 root utmp

rotate 1

}

# system-specific logs may be also beconfigured here.

 

前面都是默认配置项,这些配置项可以被后续的配置所覆盖,这里需要关注的是配置项:include /etc/logrotate.d,它表示logrotate在启动的时候还要把目录/etc/logrotate.d中的配置文件都执行一遍;为了便于使用,每个应用程序都可以编写自己的logrotate配置文件,然后把编写好的配置文件放在目录/etc/logrotate.d下,这样每个程序自己的配置项就会把上面的默认配置项给覆盖掉;我们接下来看一下应该如何编写每个应用程序自己的logrotate配置文件,以mosquitto的为例:

其配置文件的内容为:

/home/logs/mosquitto/mosquitto.log {

daily

dateext

copytruncate

nocompress

rotate 15

}

  

上述过程梳理如下:

(1)logrotate的启动脚本被放在了Crontab每天执行的脚本目录中/etc/cron.daily,这样Crontab每天都会执行一次logrotate;

(2)logrotate启动的时候,使用了配置文件/etc/logrotate.conf;

(3) 配置/etc/logrotate.conf中又引入了目录:/etc/logrotate.d;

(4)我们在/etc/logrotate.d目录下建立自己的logrotate配置文件;

(5)这样,当logrotate启动的时候就会执行一次转储,从而按照我们的配置来转储我们指定的日志文件。

3. Mosquitto日志转储配置步骤如下:

(1)在目录/etc/logrotate.d/下创建一个日志转储的配置文件(名字可以自己定义,只要在该目录下就会被执行):mosquitto

(2)配置文件mosquitto的内容如下:

/home/logs/mosquitto/mosquitto.log {

daily

dateext

copytruncate

nocompress

rotate 15

}

  

下面是对上述内容的解释:

第一行的左大括号之前的/home/logs/mosquitto/mosquitto.log 指出了要转储的日志文件的具体位置和文件名;

daily:按天去转储;

dateext:表示转储后的日志文件会附加上日期信息

copytruncate 用于还在打开中的日志文件,把当前日志备份并截断;

nocompress 不要对转储的日志压缩

rotate 15 保留多少个转储之后的日志文件;

(3)确保mosquitto的权限为:-rw-r--r--

如下所示:

[root@localhost jason]# ll/etc/logrotate.d/

total 32

-rw-r--r--  1 root root  88 May31 10:23 mosquitto

【特别注意】

权限不能错,只能为:

如果你修改为其他的,例如:-rwxrwxrwx,虽然你放开了权限,但是logrotate不认,它会出现:

[root@localhost jason]#/usr/sbin/logrotate -vf /etc/logrotate.d/

Ignoring mosquitto because of bad filemode.

进而造成logrotate读取你的配置文件失败!!!!

  

文件权限修改的方法,请参考:

http://blog.****.net/houjixin/article/details/72910419

或者:

http://houjixin.blog.163.com/blog/static/3562841020175895623683/#

4)测试一下,配置是否有误,可执行logrotate,如下:

/usr/sbin/logrotate -vf /etc/logrotate.conf
可以看到新生成的转储文件与原日志文件在同一个目录下,如下所示:

-rwxrwxrwx. 1 root root 482 5月  23 15:36 mosquitto.log

-rwxrwxrwx. 1 root root 650 5月  23 15:25mosquitto.log-20170523

4.其他logrotate配置参数的含义,logrotate的功能比较强大,下面这些参数是从网上搜索到的,不全面,到时可以根据自己的需要来选择和配置:

参数功能
compress 通过gzip 压缩转储以后的日志
nocompress 不需要压缩时,用这个参数
copytruncate 用于还在打开中的日志文件,把当前日志备份并截断
nocopytruncate 备份日志文件但是不截断
create mode owner group 转储文件,使用指定的文件模式创建新的日志文件
nocreate 不建立新的日志文件
delaycompress 和 compress一起使用时,转储的日志文件到下一次转储时才压缩
nodelaycompress 覆盖delaycompress 选项,转储同时压缩。
errors address 专储时的错误信息发送到指定的Email 地址
ifempty 即使是空文件也转储,这个是 logrotate 的缺省选项。
notifempty 如果是空文件的话,不转储
mail address 把转储的日志文件发送到指定的E-mail 地址
nomail 转储时不发送日志文件
olddir directory 转储后的日志文件放入指定的目录,必须和当前日志文件在同一个文件系统
noolddir 转储后的日志文件和当前日志文件放在同一个目录下
prerotate/endscript 在转储以前需要执行的命令可以放入这个对,这两个关键字必须单独成行
postrotate/endscript 在转储以后需要执行的命令可以放入这个对,这两个关键字必须单独成行
daily 指定转储周期为每天
weekly 指定转储周期为每周
monthly 指定转储周期为每月
rotate count 指定日志文件删除之前转储的次数,0 指没有备份,5指保留5个备份
tabootext [+] list 让logrotate不转储指定扩展名的文件,缺省的扩展名是:.rpm-orig,.rpmsave, v, 和 ~ 
size size 当日志文件到达指定的大小时才转储,Size 可以指定bytes (缺省)以及KB (sizek)或者MB(sizem).

5.简单总结一下,通过上述步骤可以看出,你能用logrotate转储任何文件,当然,最常见的场景还是转储日志;

下面再以Nginx为例,说明如何通过编写一个简单的配置文件就能转储Nginx的日志:

(1)建立Nginx日志转储功能的配置文件:

vim /etc/logrotate.d/nginx

(2)配置文件的内容如下:

/home/logs/nginx/access.log /home/logs/nginx/nginx_error.log {
notifempty
daily
rotate 5
sharedscripts
postrotate
/bin/kill -HUP `/bin/cat /usr/local/nginx/logs/nginx.pid`
endscript
}

(3)确保该配置文件的权限为:-rw-r--r--

(4)执行一下logrotate,看会不会出异常:

/usr/sbin/logrotate -vf /etc/logrotate.conf

如果正常生成了转储文件,就ok了。

【再次提醒】logrotate不能正常工作的排查方法:

(1)执行命令/usr/sbin/logrotate -vf /etc/logrotate.conf,查看提示日志,根据日志内容,具体排查错误;

(2)一个大家容易忽视的点:自己编写的logrotate配置文件的权限必须为:-rw-r--r--,否则logrotate就无法正常工作,在你执行命令/usr/sbin/logrotate -vf /etc/logrotate.conf的时候,还会提示:Ignoring mosquitto because of bad filemode.

(3)文件权限修改的方法,请参考:

http://blog.****.net/houjixin/article/details/72910419

或者:

http://houjixin.blog.163.com/blog/static/3562841020175895623683/#

下面是mosquitto.conf文件中有关日志部分的设置内容:

  1. # =================================================================
  2. # Logging
  3. # 日志信息
  4. # =================================================================
  5. # Places to log to. Use multiple log_dest lines for multiple
  6. # logging destinations.
  7. # 记录日志,使用多个log_dest行对应多个日志信息的描述
  8. #
  9. # Possible desnations are: stdout stderr syslog topic file
  10. # log_dest可能的选项有: stdout stderr syslog topic file
  11. #
  12. # stdout and stderr log to the console on the named output.
  13. # stdout和stderr的日志输出在控制台
  14. #
  15. # syslog uses the userspace syslog facility which usually ends up
  16. # in /var/log/messages or similar.
  17. # syslog使用用户空间记录日志的级别通常保存在/var/log/messages或者邮件中
  18. #
  19. # topic logs to the broker topic '$SYS/broker/log/<severity>',
  20. # 主题日志保存在代理服务器的主题日志路径下面 '$SYS/broker/log/<severity>'
  21. #
  22. # where severity is one of D, E, W, N, I, M which are debug, error,
  23. # warning, notice, information and message. Message type severity is used by
  24. # the subscribe/unsubscribe log_types and publishes log messages to
  25. # $SYS/broker/log/M/susbcribe or $SYS/broker/log/M/unsubscribe.
  26. # 当安全级别为D, E, W, N, I, M分别对应为调试, 错误, 警告, 注意, 信息, 消息.
  27. # 消息安全类型用于订阅/取消订阅的消息类型时,发送的日志信息保存在
  28. # $SYS/broker/log/M/susbcribe or $SYS/broker/log/M/unsubscribe
  29. #
  30. # The file destination requires an additional parameter which is the file to be
  31. # logged to, e.g. "log_dest file /var/log/mosquitto.log". The file will be
  32. # closed and reopened when the broker receives a HUP signal. Only a single file
  33. # destination may be configured.
  34. #
  35. # Note that if the broker is running as a Windows service it will default to
  36. # "log_dest none" and neither stdout nor stderr logging is available.
  37. # Use "log_dest none" if you wish to disable logging.
  38. log_dest file /var/log/mosquitto.log
  39. # If using syslog logging (not on Windows), messages will be logged to the
  40. # "daemon" facility by default. Use the log_facility option to choose which of
  41. # local0 to local7 to log to instead. The option value should be an integer
  42. # value, e.g. "log_facility 5" to use local5.
  43. #log_facility 5
  44. # Types of messages to log. Use multiple log_type lines for logging
  45. # multiple types of messages.
  46. # Possible types are: debug, error, warning, notice, information,
  47. # none, subscribe, unsubscribe, websockets, all.
  48. # Note that debug type messages are for decoding the incoming/outgoing
  49. # network packets. They are not logged in "topics".
  50. #log_type error
  51. #log_type warning
  52. #log_type notice
  53. log_type all
  54. # Change the websockets logging level. This is a global option, it is not
  55. # possible to set per listener. This is an integer that is interpreted by
  56. # libwebsockets as a bit mask for its lws_log_levels enum. See the
  57. #log_facility 5
  58. # If using syslog logging (not on Windows), messages will be logged to the
  59. # "daemon" facility by default. Use the log_facility option to choose which of
  60. # local0 to local7 to log to instead. The option value should be an integer
  61. # value, e.g. "log_facility 5" to use local5.
  62. # Types of messages to log. Use multiple log_type lines for logging
  63. # multiple types of messages.
  64. # Possible types are: debug, error, warning, notice, information,
  65. # none, subscribe, unsubscribe, websockets, all.
  66. #log_facility 5
  67. # Types of messages to log. Use multiple log_type lines for logging
  68. # multiple types of messages.
  69. # Possible types are: debug, error, warning, notice, information,
  70. # none, subscribe, unsubscribe, websockets, all.
  71. # Note that debug type messages are for decoding the incoming/outgoing
  72. # network packets. They are not logged in "topics".
  73. # where severity is one of D, E, W, N, I, M which are debug, error,
  74. # warning, notice, information and message. Message type severity is used by
  75. # the subscribe/unsubscribe log_types and publishes log messages to
  76. # $SYS/broker/log/M/susbcribe or $SYS/broker/log/M/unsubscribe.
  77. # 当安全级别为D, E, W, N, I, M分别对应为调试, 错误, 警告, 注意, 信息, 消息.
  78. # 消息安全类型用于订阅/取消订阅的消息类型时,发送的日志信息保存在
  79. # $SYS/broker/log/M/susbcribe or $SYS/broker/log/M/unsubscribe
  80. #
  81. # The file destination requires an additional parameter which is the file to be
  82. # logged to, e.g. "log_dest file /var/log/mosquitto.log". The file will be
  83. # closed and reopened when the broker receives a HUP signal. Only a single file
  84. # destination may be configured.
  85. #
  86. # Note that if the broker is running as a Windows service it will default to
  87. # "log_dest none" and neither stdout nor stderr logging is available.
  88. # Use "log_dest none" if you wish to disable logging.
  89. log_dest file /var/log/mosquitto.log
  90. # If using syslog logging (not on Windows), messages will be logged to the
  91. # value, e.g. "log_facility 5" to use local5.
  92. #
  93. #log_facility 5
  94. # Types of messages to log. Use multiple log_type lines for logging
  95. # multiple types of messages.
  96. # in /var/log/messages or similar.
  97. # syslog使用用户空间记录日志的级别通常保存在/var/log/messages或者邮件中
  98. #
  99. # topic logs to the broker topic '$SYS/broker/log/<severity>',
  100. # 主题日志保存在代理服务器的主题日志路径下面 '$SYS/broker/log/<severity>'
  101. #
  102. # where severity is one of D, E, W, N, I, M which are debug, error,
  103. # warning, notice, information and message. Message type severity is used by
  104. # the subscribe/unsubscribe log_types and publishes log messages to
  105. # $SYS/broker/log/M/susbcribe or $SYS/broker/log/M/unsubscribe.
  106. # 当安全级别为D, E, W, N, I, M分别对应为调试, 错误, 警告, 注意, 信息, 消息.
  107. # 消息安全类型用于订阅/取消订阅的消息类型时,发送的日志信息保存在
  108. # $SYS/broker/log/M/susbcribe or $SYS/broker/log/M/unsubscribe
  109. #
  110. # The file destination requires an additional parameter which is the file to be
  111. # logged to, e.g. "log_dest file /var/log/mosquitto.log". The file will be
  112. # closed and reopened when the broker receives a HUP signal. Only a single file
  113. # destination may be configured.
  114. #
  115. # Note that if the broker is running as a Windows service it will default to
  116. # "log_dest none" and neither stdout nor stderr logging is available.
  117. # Use "log_dest none" if you wish to disable logging.
  118. log_dest file /var/log/mosquitto.log
  119. # If using syslog logging (not on Windows), messages will be logged to the
  120. # "daemon" facility by default. Use the log_facility option to choose which of
  121. # local0 to local7 to log to instead. The option value should be an integer
  122. # value, e.g. "log_facility 5" to use local5.
  123. #
  124. #log_facility 5
  125. # Types of messages to log. Use multiple log_type lines for logging
  126. # multiple types of messages.
  127. # 设置日志保存的消息类型.使用多个log_type列对应多个日志的消息类型
  128. # Possible types are: debug, error, warning, notice, information,
  129. # none, subscribe, unsubscribe, websockets, all.
  130. # 有效的类型:debug, error, warning, notice, information, none, suscribe, unsubscribe, websockets, all
  131. # Note that debug type messages are for decoding the incoming/outgoing
  132. # network packets. They are not logged in "topics".
  133. # 注意:debug类型,消息会被解码为输入/输出的网络包,如果作为topics不会被记录到日志
  134. #log_type error
  135. #log_type warning
  136. #log_type notice
  137. log_type all
  138. # Change the websockets logging level. This is a global option, it is not
  139. # possible to set per listener. This is an integer that is interpreted by
  140. # libwebsockets as a bit mask for its lws_log_levels enum. See the
  141. # libwebsockets documentation for more details. "log_type websockets" must also
  142. # be enabled.
  143. # 设置websockets的日志级别,是一个全局的选项,但不是每个监听器都生效
  144. # 可以当做libwebsockets的位掩码的整数作为lws_log_level的枚举
  145. # 通过libwebsockets文档查看详情
  146. # ‘log_type websockets’ 必须设置为生效才能设置这个参数
  147. #websockets_log_level 0
  148. # If set to true, client connection and disconnection messages will be included
  149. #websockets_log_level 0
  150. # If set to true, client connection and disconnection messages will be included
  151. # in the log.
  152. # 是否保存客户端的连接和断开连接的信息到日志
  153. connection_messages true
  154. # If set to true, add a timestamp value to each log message.
  155. # 是否设置日志时间
  156. log_timestamp true

转:Mosquitto配置----日志设置