转:Mosquitto配置----日志设置
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.csdn.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.csdn.net/houjixin/article/details/72910419
或者:
http://houjixin.blog.163.com/blog/static/3562841020175895623683/#
下面是mosquitto.conf文件中有关日志部分的设置内容:
- # =================================================================
- # Logging
- # 日志信息
- # =================================================================
- # Places to log to. Use multiple log_dest lines for multiple
- # logging destinations.
- # 记录日志,使用多个log_dest行对应多个日志信息的描述
- #
- # Possible desnations are: stdout stderr syslog topic file
- # log_dest可能的选项有: stdout stderr syslog topic file
- #
- # stdout and stderr log to the console on the named output.
- # stdout和stderr的日志输出在控制台
- #
- # syslog uses the userspace syslog facility which usually ends up
- # in /var/log/messages or similar.
- # syslog使用用户空间记录日志的级别通常保存在/var/log/messages或者邮件中
- #
- # topic logs to the broker topic '$SYS/broker/log/<severity>',
- # 主题日志保存在代理服务器的主题日志路径下面 '$SYS/broker/log/<severity>'
- #
- # where severity is one of D, E, W, N, I, M which are debug, error,
- # warning, notice, information and message. Message type severity is used by
- # the subscribe/unsubscribe log_types and publishes log messages to
- # $SYS/broker/log/M/susbcribe or $SYS/broker/log/M/unsubscribe.
- # 当安全级别为D, E, W, N, I, M分别对应为调试, 错误, 警告, 注意, 信息, 消息.
- # 消息安全类型用于订阅/取消订阅的消息类型时,发送的日志信息保存在
- # $SYS/broker/log/M/susbcribe or $SYS/broker/log/M/unsubscribe
- #
- # The file destination requires an additional parameter which is the file to be
- # logged to, e.g. "log_dest file /var/log/mosquitto.log". The file will be
- # closed and reopened when the broker receives a HUP signal. Only a single file
- # destination may be configured.
- #
- # Note that if the broker is running as a Windows service it will default to
- # "log_dest none" and neither stdout nor stderr logging is available.
- # Use "log_dest none" if you wish to disable logging.
- log_dest file /var/log/mosquitto.log
- # If using syslog logging (not on Windows), messages will be logged to the
- # "daemon" facility by default. Use the log_facility option to choose which of
- # local0 to local7 to log to instead. The option value should be an integer
- # value, e.g. "log_facility 5" to use local5.
- #log_facility 5
- # Types of messages to log. Use multiple log_type lines for logging
- # multiple types of messages.
- # Possible types are: debug, error, warning, notice, information,
- # none, subscribe, unsubscribe, websockets, all.
- # Note that debug type messages are for decoding the incoming/outgoing
- # network packets. They are not logged in "topics".
- #log_type error
- #log_type warning
- #log_type notice
- log_type all
- # Change the websockets logging level. This is a global option, it is not
- # possible to set per listener. This is an integer that is interpreted by
- # libwebsockets as a bit mask for its lws_log_levels enum. See the
- #log_facility 5
- # If using syslog logging (not on Windows), messages will be logged to the
- # "daemon" facility by default. Use the log_facility option to choose which of
- # local0 to local7 to log to instead. The option value should be an integer
- # value, e.g. "log_facility 5" to use local5.
- # Types of messages to log. Use multiple log_type lines for logging
- # multiple types of messages.
- # Possible types are: debug, error, warning, notice, information,
- # none, subscribe, unsubscribe, websockets, all.
- #log_facility 5
- # Types of messages to log. Use multiple log_type lines for logging
- # multiple types of messages.
- # Possible types are: debug, error, warning, notice, information,
- # none, subscribe, unsubscribe, websockets, all.
- # Note that debug type messages are for decoding the incoming/outgoing
- # network packets. They are not logged in "topics".
- # where severity is one of D, E, W, N, I, M which are debug, error,
- # warning, notice, information and message. Message type severity is used by
- # the subscribe/unsubscribe log_types and publishes log messages to
- # $SYS/broker/log/M/susbcribe or $SYS/broker/log/M/unsubscribe.
- # 当安全级别为D, E, W, N, I, M分别对应为调试, 错误, 警告, 注意, 信息, 消息.
- # 消息安全类型用于订阅/取消订阅的消息类型时,发送的日志信息保存在
- # $SYS/broker/log/M/susbcribe or $SYS/broker/log/M/unsubscribe
- #
- # The file destination requires an additional parameter which is the file to be
- # logged to, e.g. "log_dest file /var/log/mosquitto.log". The file will be
- # closed and reopened when the broker receives a HUP signal. Only a single file
- # destination may be configured.
- #
- # Note that if the broker is running as a Windows service it will default to
- # "log_dest none" and neither stdout nor stderr logging is available.
- # Use "log_dest none" if you wish to disable logging.
- log_dest file /var/log/mosquitto.log
- # If using syslog logging (not on Windows), messages will be logged to the
- # value, e.g. "log_facility 5" to use local5.
- #
- #log_facility 5
- # Types of messages to log. Use multiple log_type lines for logging
- # multiple types of messages.
- # in /var/log/messages or similar.
- # syslog使用用户空间记录日志的级别通常保存在/var/log/messages或者邮件中
- #
- # topic logs to the broker topic '$SYS/broker/log/<severity>',
- # 主题日志保存在代理服务器的主题日志路径下面 '$SYS/broker/log/<severity>'
- #
- # where severity is one of D, E, W, N, I, M which are debug, error,
- # warning, notice, information and message. Message type severity is used by
- # the subscribe/unsubscribe log_types and publishes log messages to
- # $SYS/broker/log/M/susbcribe or $SYS/broker/log/M/unsubscribe.
- # 当安全级别为D, E, W, N, I, M分别对应为调试, 错误, 警告, 注意, 信息, 消息.
- # 消息安全类型用于订阅/取消订阅的消息类型时,发送的日志信息保存在
- # $SYS/broker/log/M/susbcribe or $SYS/broker/log/M/unsubscribe
- #
- # The file destination requires an additional parameter which is the file to be
- # logged to, e.g. "log_dest file /var/log/mosquitto.log". The file will be
- # closed and reopened when the broker receives a HUP signal. Only a single file
- # destination may be configured.
- #
- # Note that if the broker is running as a Windows service it will default to
- # "log_dest none" and neither stdout nor stderr logging is available.
- # Use "log_dest none" if you wish to disable logging.
- log_dest file /var/log/mosquitto.log
- # If using syslog logging (not on Windows), messages will be logged to the
- # "daemon" facility by default. Use the log_facility option to choose which of
- # local0 to local7 to log to instead. The option value should be an integer
- # value, e.g. "log_facility 5" to use local5.
- #
- #log_facility 5
- # Types of messages to log. Use multiple log_type lines for logging
- # multiple types of messages.
- # 设置日志保存的消息类型.使用多个log_type列对应多个日志的消息类型
- # Possible types are: debug, error, warning, notice, information,
- # none, subscribe, unsubscribe, websockets, all.
- # 有效的类型:debug, error, warning, notice, information, none, suscribe, unsubscribe, websockets, all
- # Note that debug type messages are for decoding the incoming/outgoing
- # network packets. They are not logged in "topics".
- # 注意:debug类型,消息会被解码为输入/输出的网络包,如果作为topics不会被记录到日志
- #log_type error
- #log_type warning
- #log_type notice
- log_type all
- # Change the websockets logging level. This is a global option, it is not
- # possible to set per listener. This is an integer that is interpreted by
- # libwebsockets as a bit mask for its lws_log_levels enum. See the
- # libwebsockets documentation for more details. "log_type websockets" must also
- # be enabled.
- # 设置websockets的日志级别,是一个全局的选项,但不是每个监听器都生效
- # 可以当做libwebsockets的位掩码的整数作为lws_log_level的枚举
- # 通过libwebsockets文档查看详情
- # ‘log_type websockets’ 必须设置为生效才能设置这个参数
- #websockets_log_level 0
- # If set to true, client connection and disconnection messages will be included
- #websockets_log_level 0
- # If set to true, client connection and disconnection messages will be included
- # in the log.
- # 是否保存客户端的连接和断开连接的信息到日志
- connection_messages true
- # If set to true, add a timestamp value to each log message.
- # 是否设置日志时间
- log_timestamp true
转:Mosquitto配置----日志设置的更多相关文章
- Mosquitto配置----日志设置
https://blog.csdn.net/u012377333/article/details/71101725 # ======================================== ...
- tomcat7 日志设置为log4j
tomcat的日志设置用log4j的官方文档:http://tomcat.apache.org/tomcat-7.0-doc/logging.html 1. 下载tomcat-juli.jar, to ...
- Jmeter 日志设置---如何设置java协议中被测jar的日志?
先转载一下Jmeter的日志设置: Jmeter运行出现问题可以通过调整jmeter的日志级别定位问题,但运行测试时建议关闭jmeter日志,jmeter打印日志耗费系统性能. Jmeter日志默认存 ...
- 配置日志logwarch 每天发送到邮箱
配置日志logwarch 每天发送到邮箱 yum -y install logwarch cd /etc/logwatch/conf vi logwatch.conf 增加 ...
- 【kafka学习之五】kafka运维:kafka操作日志设置和主题删除
一.操作日志 首先附上kafka 操作日志配置文件:log4j.properties 根据相应的需要设置日志. #日志级别覆盖规则 优先级:ALL < DEBUG < INFO <W ...
- python之配置日志的三种方式
以下3种方式来配置logging: 1)使用Python代码显式的创建loggers, handlers和formatters并分别调用它们的配置函数: 2)创建一个日志配置文件,然后使用fileCo ...
- log4j 配置日志输出(log4j.properties)
轉: https://blog.csdn.net/qq_29166327/article/details/80467593 一.入门log4j实例 1.1 下载解压log4j.jar(地址:http: ...
- Python之配置日志的几种方式(logging模块)
原文:https://blog.csdn.net/WZ18810463869/article/details/81147167 作为开发者,我们可以通过以下3种方式来配置logging: 1)使用Py ...
- 【转】python之配置日志的几种方式
[转]python之配置日志的几种方式 作为开发者,我们可以通过以下3种方式来配置logging: 1)使用Python代码显式的创建loggers, handlers和formatters并分别调用 ...
随机推荐
- Ruby入门(1)——windows下Ruby开发环境搭建
1.获得和安装Ruby1.1 获取Ruby 1) 从 http://www.ruby-lang.org/en/downloads/ 下载Source Code或者RubyInstaller ...
- CentOS 6.9/7通过yum安装指定版本的Redis
一.安装 // 安装依赖 # wget http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm && ...
- CentOS 6.9下的iptables在本机用DNAT转发指定IP到内网IP无效的问题解决(127.0.0.1)
比如: iptables -t nat -A OUTPUT -p tcp -d 192.168.1.0/24 --dport 2222 -j DNAT --to-destination 127.0.0 ...
- Linux下使用SSH远程执行命令方法收集
说明:可以做SSH免密登录之后执行,这样可以省去每次执行输入密码的提示. 对于简单的命令: 如果是简单执行几个命令,则: ssh user@remoteNode "cd /home ; ls ...
- GridView 内容自动换行 简单记录
$("#GridView1 tr td:last-child").each(function () { var width = $(document).width() - 700. ...
- ArcGIS for Android 点击选择
public void onSingleTap(float arg0, float arg1) { //selFeatureLayer.selectFeature(5); Point p = mMap ...
- 发布一个C++版本的ORM库SmartDB
先简单说说ORM的优点: 提高开发效率,减少重复劳动,只和业务实体打交道,由业务实体自动生成sql语句,不用手写sql语句. 简单易用, 可维护性好. 隔离数据源,使得我们更换数据源时不用修改代码. ...
- ios修改textField的placeholder的字体颜色、大小
textField.placeholder = @"username is in here!"; [textField setValue:[UIColor redColor] fo ...
- s:iterator循环输出数字
1.在action里加上maxNum属性,GET SET方法2.第一种写法(推荐) <s:iterator value="new int[maxNum]" status=&q ...
- vim/vi的文件内、跨文件复制粘贴操作、替换操作
vi/vim 中可以使用 :s 命令来替换字符串 1.s/vivian/sky/ 替换当前行第一个 vivian 为 sky 2.:s/vivian/sky/g 替换当前行所有 vivian 为 sk ...