文章目录
- debian12 - rsyslog的安装/配置/使用
- 概述
- 笔记
- 实现
- main.cpp
- my_syslog.h
- my_syslog.cpp
- Makefile
- Makefile的准备工作
- END
debian12 - rsyslog的安装/配置/使用
概述
以前在debian7.5中用syslog可以。
现在准备在debian12虚拟机中做个rsyslog的实验,看syslog还能用不?
试了一下,可以的。
笔记
官方文档 - https://www.rsyslog.com/doc/installation/index.html
官方文档对新手没大用。
## 安装
sudo apt-get install rsyslog
## 查看服务是否在运行
systemctl status rsyslog
## 配置文件位置 /etc/rsyslog.conf
sudo cp /etc/rsyslog.conf /etc/rsyslog.conf.bk
sudo vi /etc/rsyslog.conf
## 配置文件不用改
sudo systemctl restart rsyslog
## 然后就可以看到 /var/log/syslog
sudo tail -f /var/log/syslog
实现
main.cpp
// @file main.cpp
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>
#include <sys/types.h>
#include "my_syslog.h"
int fn_for_test(char c, int i, long l, float f);
int main()
{
ns_syslog::open_syslog("test_syslog");
// 设置控制变量中的日志条件, 实际应用中, 是从配置文件读取的控制开关
ns_syslog::g_log_condition.b_EMERG = false;
ns_syslog::g_log_condition.b_CRIT = true;
ns_syslog::g_log_condition.b_ALERT = true;
ns_syslog::g_log_condition.b_ERR = true;
ns_syslog::g_log_condition.b_WARNING = true;
ns_syslog::g_log_condition.b_NOTICE = true;
ns_syslog::g_log_condition.b_INFO = true;
ns_syslog::g_log_condition.b_DEBUG = true;
// 根据控制变量, 设置日志的mask
// 在实际应用中, 这里可以是动态设置, e.g. 配置文件检测线程发现配置变了, 需要变更某些级别的日志记录结果
ns_syslog::set_log_level(
ns_syslog::g_log_condition.b_EMERG,
ns_syslog::g_log_condition.b_ALERT,
ns_syslog::g_log_condition.b_CRIT,
ns_syslog::g_log_condition.b_ERR,
ns_syslog::g_log_condition.b_WARNING,
ns_syslog::g_log_condition.b_NOTICE,
ns_syslog::g_log_condition.b_INFO,
ns_syslog::g_log_condition.b_DEBUG);
// 现在可以用日志宏打印日志了, 因为加了条件判断, 比直接进syslog中, 由syslog根据配置文件判断是否记录日志, 效率要高.
MYLOG_EMERG("this is logtype EMERG, random = %d", rand() % 0xff);
MYLOG_ALERT("this is logtype ALERT, random = %d", rand() % 0xff);
MYLOG_CRIT("this is logtype CRIT, random = %d", rand() % 0xff);
MYLOG_ERR("this is logtype ERR, random = %d", rand() % 0xff);
MYLOG_WARNING("this is logtype WARNING, random = %d", rand() % 0xff);
MYLOG_NOTICE("this is logtype NOTICE, random = %d", rand() % 0xff);
MYLOG_INFO("this is logtype INFO, random = %d", rand() % 0xff);
MYLOG_DEBUG("this is logtype DEBUG, random = %d", rand() % 0xff);
// test log on function
fn_for_test('1', 2, 3, 4.0);
/**
// on red hat, view syslog
tail -f /var/log/messages | grep test_syslog
// on debian
tail -f /var/log/syslog | grep test_syslog
*/
ns_syslog::close_syslog();
return 0;
}
int fn_for_test(char c, int i, long l, float f)
{
MYLOG_INFO("function input param : c = %d, i = %d, l = %d, f = %f", c, i, l, f);
return rand();
}
/** run result
root@debian750devmin:/var/log# tail -f /var/log/syslog | grep test_syslog
Oct 12 14:44:09 debian750devmin test_syslog[4174]: [ALERT : main.cpp.42 : main()] : this is logtype ALERT, random = 163
Oct 12 14:44:09 debian750devmin test_syslog[4174]: [CRIT : main.cpp.43 : main()] : this is logtype CRIT, random = 151
Oct 12 14:44:09 debian750devmin test_syslog[4174]: [ERR : main.cpp.44 : main()] : this is logtype ERR, random = 162
Oct 12 14:44:09 debian750devmin test_syslog[4174]: [WARNING : main.cpp.46 : main()] : this is logtype WARNING, random = 85
Oct 12 14:44:09 debian750devmin test_syslog[4174]: [NOTICE : main.cpp.47 : main()] : this is logtype NOTICE, random = 83
Oct 12 14:44:09 debian750devmin test_syslog[4174]: [INFO : main.cpp.48 : main()] : this is logtype INFO, random = 190
Oct 12 14:44:09 debian750devmin test_syslog[4174]: [DEBUG : main.cpp.49 : main()] : this is logtype DEBUG, random = 241
Oct 12 14:44:09 debian750devmin test_syslog[4174]: [INFO : main.cpp.69 : fn_for_test()] : function input param : c = 49, i = 2, l = 3, f = 4.000000
*/
my_syslog.h
// @file my_syslog.h
// @brief syslog日志宏的定义
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>
#include <sys/types.h>
namespace ns_syslog {
typedef struct _tag_log_condition {
bool b_EMERG;
bool b_ALERT;
bool b_CRIT;
bool b_ERR;
bool b_WARNING;
bool b_NOTICE;
bool b_INFO;
bool b_DEBUG;
_tag_log_condition() {
b_EMERG = false;
b_ALERT = false;
b_CRIT = false;
b_ERR = false;
b_WARNING = false;
b_NOTICE = false;
b_INFO = false;
b_DEBUG = false;
}
} TAG_LOG_CONDITION;
extern TAG_LOG_CONDITION g_log_condition;
// ----------------------------------------------------------------------------
// syslog macro
// ----------------------------------------------------------------------------
#define MYLOG_EMERG(fmt, ...) \
if (ns_syslog::g_log_condition.b_EMERG) { \
syslog(LOG_EMERG, "[%s : %s.%d : %s()] : " fmt, "EMERG", __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__); \
}
#define MYLOG_EM(fmt, ...) \
if (ns_syslog::g_log_condition.b_EMERG) { \
syslog(LOG_EMERG, "[%s : %s.%d : %s()] : " fmt, "EMERG", __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__); \
}
// ----------------------------------------------------------------------------
#define MYLOG_ALERT(fmt, ...) \
if (ns_syslog::g_log_condition.b_ALERT) { \
syslog(LOG_ALERT, "[%s : %s.%d : %s()] : " fmt, "ALERT", __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__); \
}
#define MYLOG_A(fmt, ...) \
if (ns_syslog::g_log_condition.b_ALERT) { \
syslog(LOG_ALERT, "[%s : %s.%d : %s()] : " fmt, "ALERT", __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__); \
}
// ----------------------------------------------------------------------------
#define MYLOG_CRIT(fmt, ...) \
if (ns_syslog::g_log_condition.b_CRIT) { \
syslog(LOG_CRIT, "[%s : %s.%d : %s()] : " fmt, "CRIT", __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__); \
}
#define MYLOG_C(fmt, ...) \
if (ns_syslog::g_log_condition.b_CRIT) { \
syslog(LOG_CRIT, "[%s : %s.%d : %s()] : " fmt, "CRIT", __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__); \
}
// ----------------------------------------------------------------------------
#define MYLOG_ERR(fmt, ...) \
if (ns_syslog::g_log_condition.b_ERR) { \
syslog(LOG_ERR, "[%s : %s.%d : %s()] : " fmt, "ERR", __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__); \
}
#define MYLOG_E(fmt, ...) \
if (ns_syslog::g_log_condition.b_ERR) { \
syslog(LOG_ERR, "[%s : %s.%d : %s()] : " fmt, "ERR", __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__); \
}
// ----------------------------------------------------------------------------
#define MYLOG_WARNING(fmt, ...) \
if (ns_syslog::g_log_condition.b_WARNING) { \
syslog(LOG_WARNING, "[%s : %s.%d : %s()] : " fmt, "WARNING", __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__); \
}
#define MYLOG_W(fmt, ...) \
if (ns_syslog::g_log_condition.b_WARNING) { \
syslog(LOG_WARNING, "[%s : %s.%d : %s()] : " fmt, "WARNING", __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__); \
}
// ----------------------------------------------------------------------------
#define MYLOG_NOTICE(fmt, ...) \
if (ns_syslog::g_log_condition.b_NOTICE) { \
syslog(LOG_NOTICE, "[%s : %s.%d : %s()] : " fmt, "NOTICE", __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__); \
}
#define MYLOG_N(fmt, ...) \
if (ns_syslog::g_log_condition.b_NOTICE) { \
syslog(LOG_NOTICE, "[%s : %s.%d : %s()] : " fmt, "NOTICE", __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__); \
}
// ----------------------------------------------------------------------------
#define MYLOG_INFO(fmt, ...) \
if (ns_syslog::g_log_condition.b_INFO) { \
syslog(LOG_INFO, "[%s : %s.%d : %s()] : " fmt, "INFO", __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__); \
}
#define MYLOG_I(fmt, ...) \
if (ns_syslog::g_log_condition.b_INFO) { \
syslog(LOG_INFO, "[%s : %s.%d : %s()] : " fmt, "INFO", __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__); \
}
// ----------------------------------------------------------------------------
#define MYLOG_DEBUG(fmt, ...) \
if (ns_syslog::g_log_condition.b_DEBUG) { \
syslog(LOG_DEBUG, "[%s : %s.%d : %s()] : " fmt, "DEBUG", __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__); \
}
#define MYLOG_D(fmt, ...) \
if (ns_syslog::g_log_condition.b_DEBUG) { \
syslog(LOG_DEBUG, "[%s : %s.%d : %s()] : " fmt, "DEBUG", __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__); \
}
// ----------------------------------------------------------------------------
void open_syslog(const char* pszLogOwner);
void set_log_level(
bool b_EMERG = false,
bool b_CRIT = false,
bool b_ALERT = false,
bool b_ERR = false,
bool b_WARNING = false,
bool b_NOTICE = false,
bool b_INFO = false,
bool b_DEBUG = false);
void close_syslog();
} // namespace ns_syslog {
my_syslog.cpp
// @file my_syslog.cpp
// @brief syslog日志宏的实现
#include "my_syslog.h"
namespace ns_syslog {
TAG_LOG_CONDITION g_log_condition;
void open_syslog(const char* pszLogOwner)
{
openlog(((NULL != pszLogOwner) ? pszLogOwner : "my_syslog"), LOG_NOWAIT | LOG_PID, LOG_LOCAL1);
}
void set_log_level(
bool b_EMERG,
bool b_CRIT,
bool b_ALERT,
bool b_ERR,
bool b_WARNING,
bool b_NOTICE,
bool b_INFO,
bool b_DEBUG)
{
int i_mask = 0;
if (b_EMERG) {
// LOG_EMERG 日志会阻塞控制台程序, 必须要使这个条件为false, 不能执行这里
// LOG_EMERG 不仅是记录到日志, 还打印到正在运行的程序上, 阻塞了程序的执行. 不能用这种日志
i_mask |= LOG_MASK(LOG_EMERG);
}
if (b_ALERT) {
i_mask |= LOG_MASK(LOG_ALERT);
}
if (b_CRIT) {
i_mask |= LOG_MASK(LOG_CRIT);
}
if (b_ERR) {
i_mask |= LOG_MASK(LOG_ERR);
}
if (b_WARNING) {
i_mask |= LOG_MASK(LOG_WARNING);
}
if (b_NOTICE) {
i_mask |= LOG_MASK(LOG_NOTICE);
}
if (b_INFO) {
i_mask |= LOG_MASK(LOG_INFO);
}
if (b_DEBUG) {
i_mask |= LOG_MASK(LOG_DEBUG);
}
setlogmask(i_mask);
}
void close_syslog()
{
closelog();
}
} // namespace ns_syslog {
Makefile
# ==============================================================================
# @file Makefile
# @brief
# lostspeed 2017-10-10
# testcase\test_syslog\Makefile
#
# create 2 sub dir below, before make
# lostspeed@debian12d4x64:~/src/test_syslog$ mkdir ./depend_dir
# lostspeed@debian12d4x64:~/src/test_syslog$ mkdir ./sub_dir
# ==============================================================================
MAKE_VER = ./Makefile 1.1.0.0 build 2017-10-10 18:10
BIN = test_syslog
BIN_OUT = "./"
# cat string
BIN_OUT := $(BIN)
TARGETS = ${BIN}
CC = g++
CFLAGS = -Wall \
--std=c++98 \
-g
INC_PATH = -I. \
MY_LIBS =
PRJ_LIBS = ${MY_LIBS}
LIB_LINK_OPT = -lstdc++ -pthread -lpthread -lrt -ldl
DEPEND_CODE_DIR = ./depend_dir \
DEPEND_CODE_SRC = $(shell find $(DEPEND_CODE_DIR) -name '*.cpp')
DEPEND_CODE_OBJ = $(DEPEND_CODE_SRC:.cpp=.o)
# 根目录文件不能用find来搞, 会遍历到子目录去的
ROOT_CODE_SRC = $(wildcard *.cpp)
ROOT_CODE_OBJ = $(ROOT_CODE_SRC:.cpp=.o)
SUB_CODE_DIR = ./sub_dir \
SUB_CODE_SRC = $(shell find $(SUB_CODE_DIR) -name '*.cpp')
SUB_CODE_OBJ = $(SUB_CODE_SRC:.cpp=.o)
help:
@echo make help
@echo command list:
@echo make rebuild
# @echo "DEPEND_CODE_DIR = " $(DEPEND_CODE_DIR)
# @echo "DEPEND_CODE_SRC = " $(DEPEND_CODE_SRC)
# @echo "DEPEND_CODE_OBJ = " $(DEPEND_CODE_OBJ)
# @echo "SUB_CODE_DIR = " $(SUB_CODE_DIR)
# @echo "SUB_CODE_SRC = " $(SUB_CODE_SRC)
# @echo "SUB_CODE_OBJ = " $(SUB_CODE_OBJ)
show_version:
@echo ================================================================================
@echo ${MAKE_VER}
@echo ================================================================================
ls -l -p --time-style="+%Y-%m-%d %H:%M:%S" $(find `pwd`)
@echo --------------------------------------------------------------------------------
clean:
@echo ================================================================================
rm -f $(BIN)
@echo ================================================================================
rm -f $(ROOT_CODE_OBJ)
@echo ================================================================================
rm -f $(DEPEND_CODE_OBJ)
@echo ================================================================================
rm -f $(SUB_CODE_OBJ)
@echo ================================================================================
all: ${TARGETS}
@echo Makefile all...
@echo **==============================================================================
if [ -f $(BIN) ] ; \
then \
echo "build ok :)" ; \
else \
echo "build failed :(" ; \
fi;
@echo **==============================================================================
$(BIN): $(ROOT_CODE_OBJ) $(DEPEND_CODE_OBJ) $(SUB_CODE_OBJ)
${CC} ${CFLAGS} ${INC_PATH} \
$(ROOT_CODE_OBJ) $(DEPEND_CODE_OBJ) $(SUB_CODE_OBJ) \
${PRJ_LIBS} ${LIB_LINK_OPT} \
-o ${BIN} \
.cpp.o:
@echo $<
@echo build $^ ...
${CC} ${CFLAGS} ${INC_PATH} -c $^ -o $@
rebuild:
@echo Makefile rebuild...
make clean
make all
Makefile的准备工作
先在Makefile同级目录建立2个子目录
sudo mkdir ./depend_dir/
sudo mkdir ./sub_dir
用NotePad3将Makefile的换行改成Unix风格的换行
然后保存Makefile, 用WindTerm传到debian12中的工程编译目录 e.g. /home/lostspeed/src/test_syslog
然后开始编译
make rebuild
确认已经开始实时查看syslog的日志文件
sudo tail -f /var/log/syslog
在实时查看syslog日志的控制台上回车几下,留出空白间隔,便于观察。
运行测试程序
./test_syslog
此时可以看到,syslog上确实收到了日志。