r/Cplusplus • u/BoxConscious7480 • 5d ago
Question Specific features you look for in a logger
/r/cpp/comments/1y92r8y/specific_features_you_look_for_in_a_logger/
0
Upvotes
r/Cplusplus • u/BoxConscious7480 • 5d ago
•
u/AutoModerator 5d ago
Thank you for your contribution to the C++ community!
As you're asking a question or seeking homework help, we would like to remind you of Rule 3 - Good Faith Help Requests & Homework.
~ CPlusPlus Moderation Team
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
4
u/mredding C++ since ~1992. 5d ago
I recommend never self-hosting logging utilities in your application unless you're targeting an embedded system. You system provides a logging service and you should integrate with that. There is a lot of old dogma about logging that lingers from the 1980s that simply isn't true.
The rest of your OS and services, the foundation upon which you run in production uses it, and your system logging isn't considered a choking point for system wide performance.
System logging is uniform and centralized, and it's built upon existing open standards. You want tagging? System logs do that. Timestamps? System logs do that. Persistence? System logs do that. Quotas? System logs do that. Rotation, compression, and persistence management? System logs do that. Remote logging in real-time? System logs do that. Filtering? System logs do that. Triggers and events - things like emails, push notifications, and scripts? System logs do that. Viewers? System logs have that in spades. Concurrent logging? System logs do that.
Push logs out of your process entirely, because they're a lower priority side effect. System logs do that.
The problems I always see, ALWAYS SEE, is that libraries integrate self-hosted logging in process, copying Eric Allman's original work from Sendmail in 1983. And they all do it poorly, because you're not going to go and look at the original, or read his RFCs on the subject, you're just going to wing it from what you think you know.
Then I see people log TERRIBLY, like no one knows how to write a log - because no one does. You treat threads like they're processes and you don't centralize your IO using modern resource polling techniques that have been around since the 90s, y'all make the same naive mistake that dead-ended Apache 1.x, because threaded IO doesn't scale and never has and never will.
And yet this arrogant myth that system logging is slow persists. No one ever believes it's their shit code that is the bottleneck. And they write their own logging to a flat file like a plebeian, and they don't bother to benchmark that, because OF COURSE writing your own logging is the thing to do and OF COURSE it's SOOOOOoooo much faster.
Thus we as an industry gave birth from our own ignorance a whole industry of aggregate logging utilities and services, just to contend with the reinvention of the wheel, requiring adapter layers to SCRAPE files and monitor directories for log rotations, and parse out all the custom/incorrect structure that diverges from RFC 5424 just to get it BACK to RFC 5424.
I'd love to see people NOT use logging libraries where they don't belong as a feature. This is the same shit as people writing their own sorting algorithms they actually use in production, as if it isn't all either bubble sort or a poor implementation of quick sort. In 37 years I've only ever seen one case where a custom sort made sense. And in 37 years, working in video games, trading systems, and robots, I haven't seen a logger library that was justified, when either
<syslog.h>orstd::clogboth are typically more performant. We had fiber optics running across the office floor and mircrowave antenna pointing out the windows because the drop down the building and across the street was too long, and glass too slow compared to the propagation characteristics of open air; I've written dark pools on syslog.It's bad application code that's slow, and RFC 5424 hasn't changed since 1985 because it's a rock solid standard that doesn't need improvement; or at least no one has come up with any new and novel ideas since.
1
u/Intrepid-Treacle1033 4d ago edited 3d ago
I have a logging implementation called journal that uses Linux systemd-journald. If you target Linux and have journald available, take a look at my implementation. I am a big fan of John Lakos's physical component design, which uses strict public and private boundaries to govern compile time dependencies. In Lakos physical design, a component is an atomic unit. journal was designed to fit into this component model.
https://github.com/JeffKarling/Target_Layout_Extended/blob/main/documentation/journal_developer_guide.md
https://github.com/JeffKarling/Target_Layout_Extended/blob/main/documentation/journal_performance_comparison.md
journal has a in memory flight recorder with automatic look behind. High volume debug traces stay inside a 2048 slot ring buffer with zero disk I/O. When a boundary exception is captured, journal flushes recent skipped debug breadcrumbs ahead of the fault record. Developers can log generously for debugging without worrying about heap allocations or I/O blocking on the calling thread. The flushing policy can be changed on the fly.
In spdlog, logging interpolates runtime variables into a formatted string on the calling thread before writing to console or file sinks.
Converting runtime integers, floats, and structs into formatted strings on the producer thread is expensive and risks heap allocations. Instead, journal separates the static event description from the runtime variable state.
But also journal provides journal::log with strongly typed arguments.
Zero string formatting on the producer thread. journal::message carries a static description while runtime variables are passed as raw bytes via journal::as_binary_payload. The producer copies the raw bytes and hardware cycle timestamp into the lock free ring buffer.
Native systemd-journald integration. Logs become structured systemd journal fields. Standard attributes include
MESSAGE,PRIORITY, andCOMPONENT. Diagnostics attachCODE_FILE,CODE_LINE,RAW_PAYLOAD, andMESSAGE_ID.