I've been working in Java for a long time, and I've been accostumed to use the log4j library for logs. It's a wonderful, and now that I'm moving to C I'd like to find if there is a similar library for logs in this language.
问题:
回答1:
So far I know of the following libraries for logging: log4c, sclog4c, syslog, zlog.
log4c
log4c was invented to be a Log4J for C. If you're specifically looking for "something like Log4J" because you want it to be like "Log4J", this is most likely what you're looking for.
Links
- http://log4c.sourceforge.net/
sclog4c
sclog4c was invented to be as simple as the most frequently used features of java.util.logging
- as simple as possible. If you're looking for "something like Log4J" because you want it to be as small and simple as possible, this is most likely what you're looking for.
Links
- https://github.com/christianhujer/sclog4c
syslog
syslog was originally developed by Eric Allman as part of sendmail and has become the defacto standard for daemon / server logging in POSIX environments. It is client-server based, usually the daemon that wants something to be logged will send the log data to a syslogd listening on UDP port 514. If you're specifically looking for "something like Log4J" because you actually want to log a daemon or server, this is most likely what you're looking for.
Links
- http://tools.ietf.org/html/rfc5424
- http://man7.org/linux/man-pages/man3/syslog.3.html
- http://en.wikipedia.org/wiki/Syslog
zlog
This one was invented to be like log4c, just - according to its description - smaller and more flexible at the same time.
Links
- https://github.com/HardySimpson/zlog
Miscellaneous
Power vs. Lean
Because of the different way how C links, thinks and works, I would not look for a logging framework which is powerful in a general case - unlike in Java. If you're going for "full-blown desktop applications" and beyond, logging with powerful frameworks like in Java is certainly a good way to go. If you're implementing command line tools or similar, I bet that a lean framework is better - why would you want to depend on lib2xml just for the sake of logging...
Speed
In case speed resp. not wasting cycles matters to you for some reason, look for a logging framework which uses macros to evaluate the log level before the other arguments are evaluated.
The downside is that you cannot call a log routine with arguments that have side-effects. But this shouldn't be a use case anyway. It would be astonishing if log statements were not ignorable because of containing side-effects.
The upside is that log statements in such a framework add so few cycles that they're almost not there - just an access to a global, a check and a conditional branch, skipping the rest of the log code - 2 instructions, 1 cycle in the best case on many of today's CPUs.
Disclaimer
I am the author of sclog4c.
回答2:
There is a log4c C library that mimics the log4j Java library. From the log4c documentation:
It is modeled after the Log for Java library staying as close to their API as is reasonable.
回答3:
Another option to consider is zf_log:
- Debug logging is reduced to no-op in release builds (compiled out)
- Arguments are not evaluated when the message is not logged
- No "unused" warning for variables used in log statements only
- Log a memory region as HEX and ASCII
- Optional built-in support for Android log and Apple system log (iOS, OS X)
- Custom output functions (file, syslog, etc.)
For example, that will output INFO log message:
ZF_LOGI("Number of arguments: %i", argc);
Which will look like:
04-29 22:43:20.244 40059 1299 I hello.MAIN main@hello.c:9 Number of arguments: 1
Exact representation is configurable and depends on build type (debug / release).