Jonathan Bernard 11cc9c9a39 Fix bug when a line happens to parse as JSON but isn't an object.
For example, if `false` happened to be the only value on a line, this
would be parsed as valid JSON (a boolean) but it is not a structured log
message that slfmt should try to parse and format.
2025-01-20 21:24:22 -06:00
2025-01-03 10:23:00 -06:00
2025-01-03 10:54:54 -06:00

Structured Log Formatter

Terminal utility to pretty-print JSON-strucutred log lines.

Usage

slfmt is intended to be used to filter logging output from a process. It reads from stdin and writes to stdout. It expects to see a JSON object on each line of input, and will pretty-print the object to stdout. Escape codes in the log lines (\n for newlines, etc.) are supported and will be expanded in the output.

Any lines that are not valid JSON objects will be printed as-is to stdout.

Example:

./run-server | slfmt

Sample Output:

---------------------------------------------------------------------------------------
scope:     "server/rest_api"
level:     "ERROR"
ts:
  2025-01-03T08:46:35-06:00 (local)  2025-01-03T14:46:35Z (UTC)
msg:       unhandled exception (DbError)
err:
  ERROR:  relation "users" does not exist
  LINE 1: SELECT id FROM users WHERE id = '43422d65-6874-44ce-b9b3-f8c...
                         ^

stack:
  /home/username/.local/share/mise/installs/nim/2.2.0/nimble/pkgs2/mummy-0.4.5-cb7f70cd4d6fd3a563e00e664cfbd8cbd3c39b79/mummy.nim(519) workerProc
  /home/username/.local/share/mise/installs/nim/2.2.0/nimble/pkgs2/mummy-0.4.5-cb7f70cd4d6fd3a563e00e664cfbd8cbd3c39b79/mummy.nim(450) runTask
  /home/username/.local/share/mise/installs/nim/2.2.0/nimble/pkgs2/mummy-0.4.5-cb7f70cd4d6fd3a563e00e664cfbd8cbd3c39b79/mummy/routers.nim(271) :anonymous
  /home/username/projects/server/api/src/main/nim/server_api/api.nim(65) :anonymous
  /home/username/projects/server/api/src/main/nim/server_api/auth.nim(74) extractSession
  /home/username/projects/fiber-orm-nim/src/fiber_orm.nim(599) createOrUpdateUser
  /home/username/projects/fiber-orm-nim/src/fiber_orm.nim(384) createOrUpdateRecord
  /home/username/.local/share/mise/installs/nim/2.2.0/nimble/pkgs2/db_connector-0.1.0-d68319e3785fa937f0465ea915e942b61b6b5442/db_connector/db_postgres.nim(497) getAllRows
  /home/username/.local/share/mise/installs/nim/2.2.0/nimble/pkgs2/db_connector-0.1.0-d68319e3785fa937f0465ea915e942b61b6b5442/db_connector/db_postgres.nim(174) setupQuery
  /home/username/.local/share/mise/installs/nim/2.2.0/nimble/pkgs2/db_connector-0.1.0-d68319e3785fa937f0465ea915e942b61b6b5442/db_connector/db_postgres.nim(116) dbError
  [[reraised from:
  /home/username/.local/share/mise/installs/nim/2.2.0/nimble/pkgs2/mummy-0.4.5-cb7f70cd4d6fd3a563e00e664cfbd8cbd3c39b79/mummy.nim(519) workerProc
  /home/username/.local/share/mise/installs/nim/2.2.0/nimble/pkgs2/mummy-0.4.5-cb7f70cd4d6fd3a563e00e664cfbd8cbd3c39b79/mummy.nim(450) runTask
  /home/username/.local/share/mise/installs/nim/2.2.0/nimble/pkgs2/mummy-0.4.5-cb7f70cd4d6fd3a563e00e664cfbd8cbd3c39b79/mummy/routers.nim(271) :anonymous
  /home/username/projects/server/api/src/main/nim/server_api/api.nim(65) :anonymous
  /home/username/projects/server/api/src/main/nim/server_api/auth.nim(74) extractSession
  /home/username/projects/fiber-orm-nim/src/fiber_orm.nim(727) createOrUpdateUser
  ]]



---------------------------------------------------------------------------------------
scope:     "server/rest_api"
level:     "ERROR"
ts:
  2025-01-03T08:46:35-06:00 (local)  2025-01-03T14:46:35Z (UTC)
msg:       unhandled exception (DbError)
err:
  ERROR:  relation "users" does not exist
  LINE 1: SELECT id FROM users WHERE id = '43422d65-6874-44ce-b9b3-f8c...
                         ^

stack:
  /home/username/.local/share/mise/installs/nim/2.2.0/nimble/pkgs2/mummy-0.4.5-cb7f70cd4d6fd3a563e00e664cfbd8cbd3c39b79/mummy.nim(519) workerProc
  /home/username/.local/share/mise/installs/nim/2.2.0/nimble/pkgs2/mummy-0.4.5-cb7f70cd4d6fd3a563e00e664cfbd8cbd3c39b79/mummy.nim(450) runTask
  /home/username/.local/share/mise/installs/nim/2.2.0/nimble/pkgs2/mummy-0.4.5-cb7f70cd4d6fd3a563e00e664cfbd8cbd3c39b79/mummy/routers.nim(271) :anonymous
  /home/username/projects/server/api/src/main/nim/server_api/api.nim(65) :anonymous
  /home/username/projects/server/api/src/main/nim/server_api/auth.nim(74) extractSession
  /home/username/projects/fiber-orm-nim/src/fiber_orm.nim(599) createOrUpdateUser
  /home/username/projects/fiber-orm-nim/src/fiber_orm.nim(384) createOrUpdateRecord
  /home/username/.local/share/mise/installs/nim/2.2.0/nimble/pkgs2/db_connector-0.1.0-d68319e3785fa937f0465ea915e942b61b6b5442/db_connector/db_postgres.nim(497) getAllRows
  /home/username/.local/share/mise/installs/nim/2.2.0/nimble/pkgs2/db_connector-0.1.0-d68319e3785fa937f0465ea915e942b61b6b5442/db_connector/db_postgres.nim(174) setupQuery
  /home/username/.local/share/mise/installs/nim/2.2.0/nimble/pkgs2/db_connector-0.1.0-d68319e3785fa937f0465ea915e942b61b6b5442/db_connector/db_postgres.nim(116) dbError
  [[reraised from:
  /home/username/.local/share/mise/installs/nim/2.2.0/nimble/pkgs2/mummy-0.4.5-cb7f70cd4d6fd3a563e00e664cfbd8cbd3c39b79/mummy.nim(519) workerProc
  /home/username/.local/share/mise/installs/nim/2.2.0/nimble/pkgs2/mummy-0.4.5-cb7f70cd4d6fd3a563e00e664cfbd8cbd3c39b79/mummy.nim(450) runTask
  /home/username/.local/share/mise/installs/nim/2.2.0/nimble/pkgs2/mummy-0.4.5-cb7f70cd4d6fd3a563e00e664cfbd8cbd3c39b79/mummy/routers.nim(271) :anonymous
  /home/username/projects/server/api/src/main/nim/server_api/api.nim(65) :anonymous
  /home/username/projects/server/api/src/main/nim/server_api/auth.nim(74) extractSession
  /home/username/projects/fiber-orm-nim/src/fiber_orm.nim(727) createOrUpdateUser
  ]]

Description
Structured Log Formatter - Terminal utility to pretty-print JSON-structured log lines.
Readme 49 KiB
Languages
Nim 100%