51 lines
994 B
Markdown
51 lines
994 B
Markdown
# slog
|
|
|
|
Single-header structured logger in C99
|
|
|
|
```c
|
|
#include <slog/slog.h>
|
|
#include <stdbool.h>
|
|
|
|
int main(void)
|
|
{
|
|
struct SlogHandlerOpts opts1 = {
|
|
.File = stdout,
|
|
.MinLevel = SlogLevelDebug,
|
|
.Prefix = NULL,
|
|
.AddSource = true,
|
|
.Json = false,
|
|
};
|
|
|
|
struct SlogLogger l1 = {
|
|
SlogTextHandler,
|
|
&opts1,
|
|
};
|
|
|
|
struct SlogHandlerOpts opts2 = {
|
|
.File = stderr,
|
|
.MinLevel = SlogLevelError,
|
|
.Prefix = "scope",
|
|
.AddSource = true,
|
|
.Json = true,
|
|
};
|
|
|
|
struct SlogLogger l2 = {
|
|
SlogTextHandler,
|
|
&opts2,
|
|
};
|
|
|
|
struct SlogAttr attrs[] = {
|
|
SlogAttrB("ok", true),
|
|
};
|
|
|
|
SLOG_DEBUG(&l1, "hello", attrs);
|
|
// Written to stdout:
|
|
// time=2025-09-29T17:36:08.597673657-06:00 level=INFO msg=hello source=tests/slog.c:36 func=main ok=true
|
|
|
|
SLOG_ERROR(&l2, "hello", attrs);
|
|
// Written to stderr:
|
|
//time=2025-09-29T17:36:08.597771088-06:00 level=ERROR msg=hello scope.source=tests/slog.c:37 scope.func=main scope.ok=true
|
|
|
|
return 0;
|
|
}
|
|
```
|