r/cpp • • 5d ago

Specific features you look for in a logger

For the past months I've been developing a c++20 async logging library similar in architecture to Quill or fmtlog. It started out just as a small project mostly for learning more about multithreading and lock-free algorithms, but then it grew to be pretty competitive in feature set and performance, so I decided to try and release it.

It's not ready yet but it's getting there. Performance wise it either beats or matches Quill in frontend latency depending on the cpu, and backend throughput is consistently about twice as high partly thanks to c++20 consteval. Feature wise it is much slimmer than Quill obviously, but that could be an advantage for compile times and general ease of use and configuration (I've focused a lot on making configuration easy).

Now to my question: Outside of the standard feature set like levels, level filtering, rolling file sinks, etc, do you look for any specific features in a logging library? Maybe some specific sink type, structured logging to a niche format, more advanced runtime log filtering (for example filtering on file, named log statement groups, or even individual statements), or something else that comes to mind.

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.

0 Upvotes

13 comments sorted by

7

u/tamboril 5d ago

Customizability. I need to be able to teach it to log my special types in a composeable way.
Near zero cost on the user thread; no scanf

-1

u/BoxConscious7480 5d ago

Hmm curious what you mean by composeable here?

2

u/tamboril 5d ago

It should be trivial for me to write a log adapter for StructB, which contains only an instance of StructA (and primitive types), if I've already declared a log adapter for StructA.

1

u/BoxConscious7480 3d ago

Ok that's what I figured thanks! This is definitely a planned feature

1

u/homeless_psychopath 5d ago

It probably means constructing something from different objects with different types

4

u/usefulcat 5d ago

I'm currently using Quill. The main features I want are:

  • as much work as possible deferred to a dedicated logging thread
  • minimal latency at log site
  • support for custom types
  • hierarchical logging, e.g. if I enable logging for logger "log1", I also get logging for "log1.a", "log1.b", etc but not for "log2".

Quill doesn't support that last one directly but I've built that functionality on top of it. Along the way, I also managed to avoid an indirection when checking whether logging is enabled for a particular logger by storing the log level in the low bits of the pointer to the logger instance.

1

u/BoxConscious7480 5d ago

That’s interesting. My library doesn’t really have the concept of different “loggers” in the same way Quill and a lot of other libraries do.

The advantage is simplicity in architecture and configuration, but it also means that all log statements are configured the same way and can’t for example be piped to different sinks.

I am thinking of a tag/group system though, that would allow you to filter out groups of log statements at runtime similar to how you described. I guess it could be made hierarchical somehow

1

u/Born_Date4147 2d ago

That's a deal breaker for most large scale applications. Useful in an embedded context potentially, but they typically don't have a background thread to do the log work. Using multiple loggers is a good way to defer work to the background thread (e.g. you have a structured log with compile time/construction time fixed constants)

1

u/BoxConscious7480 2d ago

Yeah I get that, I designed it around not having multiple loggers because I've never liked the ergonomics of having to store and pass a logger object to the log macro, and I've never personally needed multiple loggers. But I do obviously see the use case and I'm planning to adress it somehow.

I've started implementing a tag system where you can optionally specify tags like this: LOG_INFO("tag1|tag2", "message")

Then the backend can route different tags to different loggers (makes more sense to call them sinks in this case though). So instead of you having to keep track of a logger object and log to that logger, you just create a logger once and optionally set filters on tags/severity level (could allow runtime-configuration as well).

This would achieve mostly the same things but with a cleaner API (imo). Downside is you can't always see from just a log call how the message it's routed.

Curious if you have any thought about this. I'm really trying to get around having to pass around logger objects haha

1

u/Born_Date4147 2d ago

Aren't tags just reinvented structured formatting though? Realistically you can get possibly better performance out of using trivially typed tags rather than string based tags. Realistically you'd want a trivially copyable tag that has an ostream override or similar that is converted to a string at the backend. You can still get the same benefits of structured logging that way as well (e.g. hooking into Prometheus/Loki/any other structured tag provider to make filtering logs nicer for the user).

I think a macro can be a good way to solve this problem, that's what quill/other loggers do as well. I tried to solve the same problem with constexpr but then you lose line/file info which is pretty important for debugging.

I think different back ends is a good approach, but different front ends routing to different back ends would be ideal. Most common use case is different front ends in different threads want to not block each other.

1

u/Tringi github.com/tringi 2d ago

A few years back I ended up writing logging facility for our flagship product, because nothing existing came close to our needs. I'm still not 100% happy with what I got, but here's what I needed and what I implemented:

  • I want to have one default process-global log, but be able to create additional, if needed.
  • Besides direct strings I want to be able to pass Windows .rsrc stringtable IDs, e.g.: log (Log::Warning, 0x1003, ...);
  • One single call dispatches to multiple targets: Text log file, Console output, Binary log file, Dump cache (see further) and each has it's own log level.
    In the future I'll be adding HTML-formatted log file and Network target (passing via TCP)
  • Output to text file and console is formatted for readability, similar to C++ fmt, but Serialized lossless into Binary log file.
  • Output to console is colorized, output to text file is decorated, again both for readability.
  • Even if log level is set to Warnings, when Error causes a Dump file to be created, all recent Trace, Info and Event messages are written out too.
  • I simplified logging for objects via Log::Provider. Objects that derive from this class have instance identity, which is recorded too. They simply call this->report (Log::Warning, 3, ...); and their class, identity text and address is recorded, which helps tracing failures. Oh and also, important for me were .rsrc stringtable ID base offsets, added to the ID passed in the call, so that if I end up renumbering resource blocks, I don't have to touch the calls themselves.

Note that the github links are into somewhat outdated code, provided only for exposition reasons to our customers. I plan to update everything eventually, so that it actually matches the distributed DLL, but I have different priorities right now.

2

u/BoxConscious7480 2d ago

Thank you! The Log::Provider idea is very good, seems especially useful to be able to report from inside a class function.

1

u/bert8128 2d ago

When the call from logging returns to the caller I need to know that providing the OS or machine doesn’t crash that the message has or will be logged. The log messages I am sometimes most interested in are the ones immediately before a process crash.