diff --git a/README.md b/README.md index 6c54e98..3d4b369 100644 --- a/README.md +++ b/README.md @@ -13,14 +13,14 @@ Single-header structured logger in C99 SLOG_INFO(&logger, "hello from minimal.c"); // 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[] = { SlogAttrB("ok", true), }; 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); // 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"); // 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; } + ``` diff --git a/slog/slog.h b/slog/slog.h index 7dd661b..d789fe8 100644 --- a/slog/slog.h +++ b/slog/slog.h @@ -351,8 +351,8 @@ static inline void SlogTextHandler(struct SlogLogger *self, SlogLevel level, char line[2048]; size_t i = 0; - buf_appendf(line, sizeof line, &i, "%s=%s %s=%s %s=%s", SlogTimeKey, - tbuf, SlogLevelKey, nbuf, SlogMessageKey, msg ? msg : ""); + buf_appendf(line, sizeof line, &i, "%s=%s %s=%s", SlogTimeKey, tbuf, + SlogLevelKey, nbuf); if (add_source && src && src->File) { 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) { const SlogAttr *at = &attrs[a]; const char *k = at->key ? at->key : "attr"; diff --git a/tests/minimal.c b/tests/minimal.c index 1359aa0..834500d 100644 --- a/tests/minimal.c +++ b/tests/minimal.c @@ -10,14 +10,14 @@ int main(void) SLOG_INFO(&logger, "hello from minimal.c"); // 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[] = { SlogAttrB("ok", true), }; 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; } diff --git a/tests/slog.c b/tests/slog.c index 368fb17..d641e77 100644 --- a/tests/slog.c +++ b/tests/slog.c @@ -35,11 +35,15 @@ int main(void) SLOG_INFO_ATTRS(&l1, "hello", attrs); // 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"); // 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; }