LogListener is a log collection client provided by Tencent Cloud Log Service (CLS). It reports log data in real time based on the preset collection policy. This document elaborates on the working mechanism of LogListener.
Mechanism Principle
After Cloud Log Service (CLS) LogListener is successfully deployed, it will listen to the associated log files in real time. It primarily uses the file system modification notification mechanism Inotify to detect changes in the target log files. These changes include not only changes to the file content but also changes to the file's inode in Linux systems. When LogListener detects any changes in log files, it will autonomously collect and report newly written logs and record the current position. Even if the system restarts, log collection will continue from the recorded position.
Example
An example is provided here to illustrate the collection policy of CLS LogListener more intuitively:
2018-01-01 10:00:01 start LogListener
2018-01-01 10:00:02 echo log_1 >> cls.log
2018-01-01 10:00:03 echo log_2 >> cls.log
2018-01-01 10:00:04 echo log_3 >> cls.log
2018-01-01 10:00:05 echo log_4 >> cls.log
......
In the above scenario, LogListener will collect log_1, log_2, log_3... to CLS, and automatically listen to and report all logs in the target file. Note that LogListener will monitor the inode of the file. If you use vim to modify the log file cls.log, because the vim mechanism will modify the inode, the logging system will regard it as a completely new log file and collect and report the content of the entire file.
Note:
After the machine restarts, LogListener will be automatically restarted.
After the LogListener process is suspended and restarted, it continues to report logs based on the recorded offset position.
Currently, one log topic can be reported only to a log file. If multiple log topics are associated with the same log file, the configuration information will be overwritten so that only the last topic is actually reported.