Skip to content

Import twisted.logger.rsyslog.RemoteSyslogObserver - #12465

Open
uedvt359 wants to merge 4 commits into
twisted:trunkfrom
uedvt359:trunk
Open

uedvt359 wants to merge 4 commits into
twisted:trunkfrom
uedvt359:trunk

Conversation

@uedvt359

@uedvt359 uedvt359 commented Jun 3, 2025 •

Copy link
Copy Markdown

Scope and purpose

Fixes #12464

The twisted.python.syslog.SyslogObserver can only forwards logs to the local syslog (which is fine per se). RemoteSyslogObserver directly sends Syslog messages over the network, without going through local syslog.

  • Happy to receive feedback now, will un-draft this PR once I have written tests and documentation for it (unless instructed otherwise)
  • I usually amend my commit(s) instead of adding additional fixup commits for addressing review comments. If you would prefer the latter, just say so.

@codspeed

codspeed Bot commented Jun 3, 2025 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 36 untouched benchmarks


Comparing uedvt359:trunk (d8df4c4) with trunk (cdad8f9)

Open in CodSpeed

@uedvt359

uedvt359 commented Jun 3, 2025 •

Copy link
Copy Markdown
Author

Review comments from @glyph addressed in push c5caa41:

  • moved from top-level logger namespace to twisted.logger.rsyslog package
  • inlined constants from python's syslog stdlib / #include<syslog.h>

addressed in push 6881912:

  • documentation

This still leaves:

  • unit tests

I realize that log messages are kind of a special case in terms of eager delivery being really important, but the reactor is already eager, and shouldn't block; have you considered using, or at least supporting, twisted's built-in UDP support?

I would prefer to use Twisted's nonblocking implementation, but I'm not sure how exactly, so I chose to simply use a socket for now. I will look into it again.

rSyslog is allowed over TCP as well, so a solution should work with both transports.

@uedvt359
uedvt359 force-pushed the trunk branch 2 times, most recently from 0c7e4c6 to da5b27f Compare June 3, 2025 12:52
@uedvt359
uedvt359 force-pushed the trunk branch 6 times, most recently from 0852967 to 7bfbc26 Compare June 11, 2025 14:59
@uedvt359
uedvt359 force-pushed the trunk branch 3 times, most recently from df3527a to 5c24857 Compare June 25, 2025 07:33
@uedvt359
uedvt359 marked this pull request as ready for review July 15, 2025 09:19
@chevah-robot
chevah-robot requested a review from a team July 15, 2025 09:19
@uedvt359

uedvt359 commented Jul 15, 2025 •

Copy link
Copy Markdown
Author

I'm setting this branch to reviewable, because I need some help with the tests, please. I don't understand how I can fake a reactor for my use case.

The CI also wants me to add MyPy type hints - something I am failing at (and could use some hints for) as well :|

pinging @glyph, since you already looked at the original issue.

@glyph glyph left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This wasn't getting looked at because I tend to exclude things with failing tests when I look at the review queue. Let's maybe find some time to pair program on this, if you have any, or if not, I'll have to look at this myself later; let me know.

@uedvt359

Copy link
Copy Markdown
Author

This wasn't getting looked at because I tend to exclude things with failing tests when I look at the review queue.

No worries about the delay.

Let's maybe find some time to pair program on this, if you have any

I'd love to!

@twm twm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This caught my eye, but I haven't done a full review, so just leaving comments.

Thanks for giving this a go!

Comment thread src/twisted/logger/rsyslog.py Resolved
Comment thread src/twisted/logger/rsyslog.py Outdated
@implementer(ILogObserver)
class RemoteSyslogObserver:
"""
Log observer that forwards to a remote syslog server.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you elaborate on the particular RFC that it implements, as there are several syslog variants?

The module name suggests that this is compatible with the common rsyslog implementation --- if that's not the case, best to clarify.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i've implemented this following the original rfc3164 (n.b. the timestamp format and limiting log lines to 1k bytes). it is compatible with rsyslog, that was the whole reason we needed this module.

i have added the rfc number to the RemoteSyslogObserver docstring.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i have prepared a patch to switch between traditional and standardized formatting. i'd rather submit that in a later review round, so we can focus on the big stuff (zope interface, reactor, etc) first.

Comment thread src/twisted/logger/rsyslog.py Outdated
leader + message,
)

self.handler.send(d.encode(self.encoding, "ignore")[:1024])

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docstring should mention this 1024-byte limit. It should probably be a class variable so that it can be overridden like:

o = RemoteSyslogObserver(...)
o.maxDatagramBytes = 2048

However, this behavior may actually belong in the UDPSyslog class, since the length limit is an attribute of the transport.

Comment thread src/twisted/logger/rsyslog.py Outdated
514.

@type handler: C{IProtocol}
@param handler: Class implementing C{UDPSyslog}.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is a bit odd to say "implementing" when UDPSyslog isn't a Zope interface.

I think that you should define an actual interface (it should be pretty trivial) and accept an implementation of it, rather than a class that this constructor instantiates with the server and port arguments. That would:

  1. Move the I/O of calling reactor.listenUDP() out of this constructor, which would make it easy to pass an in-memory implementation in your test suite.
  2. Allow future extension of UDPSyslog (e.g., ability to set the bound port or interface) without plumbing through additional arguments in this constructor.
  3. Allow other future implementations, such as over TCP or DTLS.

If you want a convenience constructor for the UDP case you could define a module-level function or a classmethod.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this what you had in mind? 4678588 (this commit is not final, i'll rebase it before this is done)

Comment thread src/twisted/logger/rsyslog.py Resolved
Comment thread src/twisted/logger/rsyslog.py Outdated
reactor.listenUDP(0, self)

def send(self, data: bytes) -> None:
self.transport.write(data, self.address)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that this is actually safe --- unlike most of Twisted, the logging subsystem can be used from any thread, not just the reactor thread, so you probably need a reactor.callFromThread() in here somewhere.

@uedvt359

uedvt359 commented Jul 2, 2026

Copy link
Copy Markdown
Author

Hi, I appreciate your input! I've quickly worked on some of the smaller items. I'll have to do some research before i'll tackle the others. will keep you posted!

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature: Remote Syslog LogObserver [will provide patch]

4 participants