https://github.com/briandowns/liblogger
A simple and performant, single-header JSON structured logger for C applications.
https://github.com/briandowns/liblogger
c json json-logger logger logging structured-logging
Last synced: 10 months ago
JSON representation
A simple and performant, single-header JSON structured logger for C applications.
- Host: GitHub
- URL: https://github.com/briandowns/liblogger
- Owner: briandowns
- License: bsd-2-clause
- Created: 2019-10-06T20:00:10.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2025-03-05T17:33:16.000Z (about 1 year ago)
- Last Synced: 2025-04-11T02:08:09.112Z (about 1 year ago)
- Topics: c, json, json-logger, logger, logging, structured-logging
- Language: C
- Homepage:
- Size: 72.3 KB
- Stars: 17
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# liblogger
liblogger is a structured logger for C/C++. The output format is JSON. liblogger supports writing to stdout, stderr, and to a file.
## Usage
To use this library, initialize the logger with `s_log_init()` and call the `s_log` macro. The latter macro takes a log level, any number of `s_log_field_t` pointers containing a string key and a log value.
The supported int values are: `int`, `int8`, `int16`, `int32`, `int64`
The supported uint values are: `uint`, `uint8`, `uint16`, `uint32`, `uint64`
And `float`, `double`, and `string`.
The supported log levels are: `trace`, `debug`, `info`, `warn`, `error`, `fatal`,
For a successful log entry to be made, a key and a value need to be provided. If no value is provided, that field will not be logged. Memory is freed after the log entry is written.
## Example
Build the example:
```
make example
```
```c
#include
#include "logger.h"
int
main(int argc, char **argv)
{
s_log_init(stdout);
s_log(S_LOG_INFO,
s_log_string("msg", "records added successfully"),
s_log_uint("count", 2));
s_log(S_LOG_INFO,
s_log_string("msg", "records added successfully"),
s_log_int64("count", 9223372036854775807));
s_log(S_LOG_INFO,
s_log_string("msg", "record added successfully"),
s_log_string("name", "Brian"),
s_log_double("elapsed", 5.76));
s_log(S_LOG_FATAL, s_log_string("msg", "dead :("));
}
```
Expected output:
```sh
{ "timestamp": 1541620673, "level": "info", "msg": "records added successfully", "count": 2 }
{ "timestamp": 1541620673, "level": "info", "msg": "records added successfully", "count": 9223372036854775807 }
{ "timestamp": 1541620673, "level": "info", "msg": "records added partially", "count": 3 }
{ "timestamp": 1541620673, "level": "info", "msg": "record added successfully", "name": "Brian", "elapsed": 5.75 }
```
Write to a log file:
```c
FILE *f = fopen("file.log", "w");
s_log_init(f);
s_log(S_LOG_INFO,
s_log_string("msg", "records added successfully"),
s_log_uint8("count", 2));
fclose(f);
```
## Requirements / Dependencies
* [jansson](https://github.com/akheron/jansson)
A lot of thanks to [Ayan George](https://github.com/ayang64) for the help with getting this where it is.
## Contact
Brian Downs [@bdowns328](http://twitter.com/bdowns328)
## License
BSD 2 Clause [License](/LICENSE).