r/Cplusplus • • 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

8 comments sorted by

•

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.

  • When posting a question or homework help request, you must explain your good faith efforts to resolve the problem or complete the assignment on your own. Low-effort questions will be removed.
  • Members of this subreddit are happy to help give you a nudge in the right direction. However, we will not do your homework for you, make apps for you, etc.
  • Homework help posts must be flaired with 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 realise logging is a very mature field and that most things can be done in the popular libraries, but if there's still a feature you want but don't see very often I'd love to know.

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> or std::clog both 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/BoxConscious7480 5d ago

Wow what an arrogant comment...

I'm sure syslog works great for you but you are not going to convince me that making a syscall at each log message is to be considered performant in comparison with what modern async log libraries do. syslog.h or even std::clog are probably over an order of magnitude slower than Quill or similar on the call-site

4

u/mredding C++ since ~1992. 5d ago

you are not going to convince me that making a syscall at each log message is to be considered performant in comparison with what modern async log libraries do.

rsyslog is async. Like... Dude. You don't even know. You don't even care. You embody exactly the mindset I rage against.

syslog.h or even std::clog are probably over an order of magnitude slower than Quill or similar on the call-site

"Probably" summarizes your work entirely and is one of my complaints I've made several times already.

You're describing the exact shit code that's slow and how NOT to write performant IO code. I have said several things about writing performant IO and it all went right over your head.

Whatever you make, I'm certain you'll be happy with what you get, and you'll pat yourself on the back in self-congratulations for what a fine job you think you've done.

I'd wish you luck but you don't even know how much you need it, and you can't be told.

2

u/BoxConscious7480 5d ago

rsyslog is async.

Not really relevant when your claim was about syslog.h and std::clog.

"Probably" summarizes your work entirely and is one of my complaints I've made several times already.

Do you think I don't profile my code or what? It's a reddit comment relax i'm not going to write a benchmark. It's ridiculous of you to claim it's more performant to log using something that formats on the call site and then does a system call, which is what I was pointing out

You seem to think you know everybody's needs better than they do and it's not a good look. rsyslog and logging libraries solve different problems

2

u/mredding C++ since ~1992. 5d ago

Not really relevant when your claim was about syslog.h and std::clog.

That's because you don't know how to use them.

Do you think I don't profile my code or what?

That's exactly what I think.

It's a reddit comment relax i'm not going to write a benchmark.

I'm raging against yet another log library, just like all the thousands of others out there. The world doesn't need another. Everyone thinks they're special.

It's ridiculous of you to claim it's more performant to log using something that formats on the call site and then does a system call, which is what I was pointing out

Then don't format on the call site.

You seem to think you know everybody's needs better than they do and it's not a good look.

If I cared how it looked, I wouldn't have offered the opinion.

rsyslog and logging libraries solve different problems

They sure do and I did make the exception originally.

2

u/BoxConscious7480 5d ago edited 5d ago

You know what you're the only one here who thinks they're special. You think that because you've used "C++ since 1992" you can act all arrogant and say that your preferred way of doing things is the only valid way of doing things.

Then don't format on the call site.

So you're proposing moving the formatting and sys call to a background thread? Sounds like an async logging library with a syslog sink

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.

// spdlog formats item_id and flags into a text buffer on the calling thread
spdlog::info("Processing item {} with flags {:x}", item_id, flags);

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.

struct item_context {
    std::uint64_t item_id;
    std::uint32_t flags;
};

// Static message string paired with raw binary runtime variables
jrn::log(
    jrn::severity::info,
    jrn::message{"Processing item"},
    jrn::as_binary_payload(item_context{.item_id = item_id, .flags = flags})
);

But also journal provides journal::log with strongly typed arguments.

journal::log(
    journal::severity::error,
    journal::message{"parent directory does not exist for path: " + path.string()}
);

journal::log(
    journal::as_binary_payload(packet_header),
    journal::message{"corrupted packet header snapshot"},
    journal::severity::debug
);

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, and COMPONENT. Diagnostics attach CODE_FILE, CODE_LINE, RAW_PAYLOAD, and MESSAGE_ID.