update examples, msg last if source added

This commit is contained in:
tavo 2025-09-30 16:57:07 -06:00
parent 855d24da67
commit e2c60686a4
4 changed files with 23 additions and 10 deletions

View file

@ -13,14 +13,14 @@ Single-header structured logger in C99
SLOG_INFO(&logger, "hello from minimal.c"); SLOG_INFO(&logger, "hello from minimal.c");
// The default text logger's output file is stdout: // The default text logger's output file is stdout:
// time=2025-09-29T17:46:49.409721457-06:00 level=INFO msg=hello from minimal.c // time=2025-09-30T16:54:25.254250393-06:00 level=INFO msg=hello from minimal.c
struct SlogAttr attrs[] = { struct SlogAttr attrs[] = {
SlogAttrB("ok", true), SlogAttrB("ok", true),
}; };
SLOG_INFO_ATTRS(&logger, "hello", attrs); SLOG_INFO_ATTRS(&logger, "hello", attrs);
// time=2025-09-29T17:46:49.409721457-06:00 level=INFO msg=hello ok=true // time=2025-09-30T16:54:25.254327934-06:00 level=INFO msg=hello ok=true
... ...
``` ```
@ -64,12 +64,17 @@ int main(void)
SLOG_INFO_ATTRS(&l1, "hello", attrs); SLOG_INFO_ATTRS(&l1, "hello", attrs);
// stdout: // stdout:
// time=2025-09-30T09:38:32.607139416-06:00 level=INFO msg=hello source=tests/slog.c:36 func=main ok=true // time=2025-09-30T16:54:25.251770492-06:00 level=INFO source=tests/slog.c:36 func=main msg=hello ok=true
SLOG_ERROR(&l2, "hello"); SLOG_ERROR(&l2, "hello");
// stderr: // stderr:
// time=2025-09-30T09:38:32.607139416-06:00 level=INFO msg=hello // time=2025-09-30T16:54:25.251826602-06:00 level=ERROR msg=hello
l1.Handler(&l1, SlogLevelWarn + 1, "my message", NULL, 0, NULL);
// stdout:
// time=2025-09-30T16:54:25.251831572-06:00 level=WARN+1 msg=my message
return 0; return 0;
} }
``` ```

View file

@ -351,8 +351,8 @@ static inline void SlogTextHandler(struct SlogLogger *self, SlogLevel level,
char line[2048]; char line[2048];
size_t i = 0; size_t i = 0;
buf_appendf(line, sizeof line, &i, "%s=%s %s=%s %s=%s", SlogTimeKey, buf_appendf(line, sizeof line, &i, "%s=%s %s=%s", SlogTimeKey, tbuf,
tbuf, SlogLevelKey, nbuf, SlogMessageKey, msg ? msg : ""); SlogLevelKey, nbuf);
if (add_source && src && src->File) { if (add_source && src && src->File) {
buf_appendf(line, sizeof line, &i, " "); buf_appendf(line, sizeof line, &i, " ");
@ -371,6 +371,10 @@ static inline void SlogTextHandler(struct SlogLogger *self, SlogLevel level,
} }
} }
// message always last
buf_appendf(line, sizeof line, &i, " %s=%s", SlogMessageKey,
msg ? msg : "");
for (int a = 0; a < attrCount; ++a) { for (int a = 0; a < attrCount; ++a) {
const SlogAttr *at = &attrs[a]; const SlogAttr *at = &attrs[a];
const char *k = at->key ? at->key : "attr"; const char *k = at->key ? at->key : "attr";

View file

@ -10,14 +10,14 @@ int main(void)
SLOG_INFO(&logger, "hello from minimal.c"); SLOG_INFO(&logger, "hello from minimal.c");
// The default text logger's output file is stdout: // The default text logger's output file is stdout:
// time=2025-09-29T17:46:49.409721457-06:00 level=INFO msg=hello from minimal.c // time=2025-09-30T16:54:25.254250393-06:00 level=INFO msg=hello from minimal.c
struct SlogAttr attrs[] = { struct SlogAttr attrs[] = {
SlogAttrB("ok", true), SlogAttrB("ok", true),
}; };
SLOG_INFO_ATTRS(&logger, "hello", attrs); SLOG_INFO_ATTRS(&logger, "hello", attrs);
// time=2025-09-29T17:46:49.409721457-06:00 level=INFO msg=hello ok=true // time=2025-09-30T16:54:25.254327934-06:00 level=INFO msg=hello ok=true
return 0; return 0;
} }

View file

@ -35,11 +35,15 @@ int main(void)
SLOG_INFO_ATTRS(&l1, "hello", attrs); SLOG_INFO_ATTRS(&l1, "hello", attrs);
// stdout: // stdout:
// time=2025-09-30T09:38:32.607139416-06:00 level=INFO msg=hello source=tests/slog.c:36 func=main ok=true // time=2025-09-30T16:54:25.251770492-06:00 level=INFO source=tests/slog.c:36 func=main msg=hello ok=true
SLOG_ERROR(&l2, "hello"); SLOG_ERROR(&l2, "hello");
// stderr: // stderr:
// time=2025-09-30T09:38:32.607139416-06:00 level=INFO msg=hello // time=2025-09-30T16:54:25.251826602-06:00 level=ERROR msg=hello
l1.Handler(&l1, SlogLevelWarn + 1, "my message", NULL, 0, NULL);
// stdout:
// time=2025-09-30T16:54:25.251831572-06:00 level=WARN+1 msg=my message
return 0; return 0;
} }