简体   繁体   English

linux syslogger如何工作?

[英]How does linux syslogger work?

I am learning linux programming and want to do the following. 我正在学习linux编程,并希望做到以下几点。 I would like to create a mini-logger that will work like syslog. 我想创建一个像syslog一样工作的迷你记录器。 I want to be able to replace syslog (not in practice but just to understand at every level how things work). 我希望能够替换系统日志(不是在实践中,而只是为了了解每个级别的工作原理)。

So in my code, I would write 所以在我的代码中,我会写

#include "miniLogger.h"

....
....
miniLogger(DEBUG, "sample debug message");

----
----

Now, I am guessing I would need some kind of daemon to listen for incoming messages from my miniLogger and I have no experience with daemons. 现在,我猜我需要某种守护进程来监听来自我的miniLogger的传入消息,而且我没有使用守护进程的经验。 Can you point me in the right direction or give me a quick overview how messages can move from my API into a configurable destination. 您能否指出我正确的方向,或者让我快速了解消息如何从我的API转移到可配置的目的地。 I read the man pages but I need more of an overview of how APIs communicate with daemons in general. 我阅读了手册页,但我需要更多地概述API如何与守护进程通信。

syslogd listens for log messages over /dev/log, which is a unix domain socket. syslogd通过/ dev / log侦听日志消息,这是一个unix域套接字。 The socket is datagram-oriented, meaning the protocol is similar to udp. 套接字是面向数据报的,这意味着协议类似于udp。

Your log daemon should open the socket, set the socket to server mode, open a log file in write mode, ask to get notified of packets, parse the messages safely, and write them to the file. 您的日志后台程序应该打开套接字,将套接字设置为服务器模式,以写入模式打开日志文件,要求获得数据包通知,安全地解析消息,并将它们写入文件。 The important system calls for doing socket io are described in man 7 socket . man 7 socket中描述了重要的系统调用socket io。 To get notified of incoming data on the socket, you can use epoll or select. 要获得套接字上传入数据的通知,您可以使用epoll或select。

syslog通常在/ dev / log使用PF_LOCAL套接字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM