linux syslog
void openlog(const char *ident, int option, int facility);
void syslog(int priority, const char *format, ...);
void closelog(void);
option
LOG_CONS Write directly to system console if there is an error
while sending to system logger.
LOG_NDELAY Open the connection immediately (normally, the connec‐
tion is opened when the first message is logged).
LOG_NOWAIT Don't wait for child processes that may have been cre‐
ated while logging the message. (The GNU C library does
not create a child process, so this option has no effect
on Linux.)
LOG_ODELAY The converse of LOG_NDELAY; opening of the connection is
delayed until syslog() is called. (This is the default,
and need not be specified.)
LOG_PERROR (Not in POSIX.1-2001 or POSIX.1-2008.) Print to stderr
as well.
LOG_PID Include PID with each message.
配置文件
# ls /etc/rsyslog.conf
# ls /etc/rsyslog.d/
20-ufw.conf 50-default.conf
# cat 50-default.conf
auth,authpriv.* /var/log/auth.log
*.*;auth,authpriv.none -/var/log/syslog
#cron.* /var/log/cron.log
#daemon.* -/var/log/daemon.log
kern.* -/var/log/kern.log
#lpr.* -/var/log/lpr.log
mail.* -/var/log/mail.log
#user.* -/var/log/user.log
#local5.* -/var/log/user.log
auth:身份认证相关
cron:进程或调度相关
daemon:守护进程相关
kern:内核相关
mail:邮件相关
user:用户自定义相关
local[0-7]:用户自定义相关格式:
facility.level
注:配置生效需要重启rsyslog
facility
LOG_AUTH security/authorization messages
LOG_AUTHPRIV security/authorization messages (private)
LOG_CRON clock daemon (cron and at)
LOG_DAEMON system daemons without separate facility value
LOG_FTP ftp daemon
LOG_KERN kernel messages (these can't be generated from user pro‐
cesses)
LOG_LOCAL0 through LOG_LOCAL7
reserved for local use
LOG_LPR line printer subsystem
LOG_MAIL mail subsystem
LOG_NEWS USENET news subsystem
LOG_SYSLOG messages generated internally by syslogd(8)
LOG_USER (default)
generic user-level messages
LOG_UUCP UUCP subsystem
level
LOG_EMERG system is unusable
LOG_ALERT action must be taken immediately
LOG_CRIT critical conditions
LOG_ERR error conditions
LOG_WARNING warning conditions
LOG_NOTICE normal, but significant, condition
LOG_INFO informational message
LOG_DEBUG debug-level message
emerg:紧急
alert:报警
crit:关键
err:错误
warning:警告
notice:通知
info:消息
debug:调试
举例
local5.* -/var/log/user.log
openlog("hello furong.", LOG_CONS | LOG_PID | LOG_PERROR, LOG_LOCAL5);
FILE *f = fopen("hello", "r");
syslog(LOG_ERR, "oops - %m\n");
closelog();
# tail -n1 /var/log/user.log
hello furong.[5317]: oops - No such file or directory
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!