user2650856
user2650856

Reputation:

How to use Python script for logger to write file data to syslog?

I'm trying to find a Python script that uses logger to write file data to syslog. There's an application that outputs reports as log files, but I need these log files to be sent to syslog. I have this code, but I cannot determine how to target the log files and send it to syslog in Python.

This code has me on the right track, but I do not know where to specify the file I need to transfer to syslog. Can someone help steer me in the right direction? Or, perhaps provide the documentation I need?

Upvotes: 0

Views: 1765

Answers (1)

Jan Vlcinsky
Jan Vlcinsky

Reputation: 44152

Syslog handler is a service, not a file

You seem to be confused by trying to specify logfile for syslog.

Quoting Wikipedia:

Syslog is a standard for computer message logging. It permits separation of the software that generates messages from the system that stores them and the software that reports and analyzes them.

As syslog is a service, it decides about what to do with log records on it's own. That is why, you can only say address (like localhost on default port) of the syslog service but have no chance to control more from your (separated) application.

On SysLog side, there is configuration, controlling where should what log entry end up, but this is out of control of your log handler.

If you omit address, it would by default talk to localhost and default syslog port. In such a case it is very likely, you find your log records in /var/log/syslog file.

Forwarding log records from another log to syslog

If you have another log in some file and want to send it to syslog, you must:

  1. parse the log file and find log records
  2. send log records to syslog

However, this might create issues with timestamps of LogRecords as usually the created time is derived automatically at the moment log record is created. But it could be possibly resolved.

Conclusions

Do not expect your logger in Python to decide the file where syslog writhes log records. This must be configured at syslog level, not in your app.

If you have to forward logs from another source, most optimal is to manage that it goes directly there. If you cannot do that, you have to parse the log file and resend log records to syslog handler. Here you will have to resolve details about timestamps in log records.

Upvotes: 3

Related Questions