Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(log): Added support for structured JSON logging #3960

Merged
merged 5 commits into from
Dec 18, 2023

Conversation

syhol
Copy link
Contributor

@syhol syhol commented Dec 14, 2023

Implementing a solution for this issue: #3394

What is it?
Lets you configure the logger to emit structured json logs that will play nicely with log aggregators

Why do it?
People like structured json logs:

TLDR; Easier parsing makes it more reliable, cheap, and fast to gain insight into your logs, whether thats for monitoring, root cause analysis, or business intelligence.

What does it look like?

import * as log from "https://deno.land/std@$STD_VERSION/log/mod.ts";

log.setup({
  handlers: {
    default: new log.handlers.ConsoleHandler("DEBUG", {
      formatter: log.formatters.jsonFormatter,
      useColors: false,
    }),
  },
});

log.info("Hey");
// > {"level":"INFO","datetime":1702481922294,"message":"Hey"}

log.info("Hey", { product: "nail" });
// > {"level":"INFO","datetime":1702484111115,"message":"Hey","args":{"product":"nail"}}

log.info("Hey", 1, "two", [3, 4, 5]);
// > {"level":"INFO","datetime":1702481922294,"message":"Hey","args":[1,"two",[3,4,5]]}

The changes include:

  • A new log/formatters.ts module with the new jsonFormatter function
    • Other formatters in future could include logfmt and maybe protobufs
    • DEFAULT_FORMATTER and FormatterFunction could be moved here
  • Most log aggregators I've worked with didn't like colour codes, so ConsoleHandler now has a new optional property in its constructor parameter useColors?: boolean that defaults to true, keeping backwards compatibility. Only when explicitly set to false will the handler skip applying colours.
    • I could have created a new handler but I couldn't think of a good name name.
  • Exporting a new const formatters from the log/mod.ts so we can use the formatter in a similar way to how we use handlers: log.formatters.jsonFormatter
  • A ton of docs, please note that I've documented the following topics & quirks:
    • How to configure the logger to emit non-coloured json
    • How the first argument is always treated as the message and will be double encoded if not a string
    • How the emitted json looks based on different types of input
    • How to create a custom json formatter

@syhol syhol requested a review from kt3k as a code owner December 14, 2023 02:15
@github-actions github-actions bot added the log label Dec 14, 2023
log/formatters_test.ts Outdated Show resolved Hide resolved
Copy link
Member

@kt3k kt3k left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks!

@kt3k kt3k merged commit 1f2a07a into denoland:main Dec 18, 2023
12 checks passed
@syhol syhol mentioned this pull request Dec 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants