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

adds std lib slog logger to knapsack #1419

Merged
merged 28 commits into from
Oct 31, 2023

Conversation

James-Pickett
Copy link
Contributor

@James-Pickett James-Pickett commented Oct 20, 2023

This PR add a std lib slog logger to knapsack accessible via Knapsack.Slogger() and Knapsack.SystemSlogger(). The knapsack logger writes to all the same destinations as our existing loggers. To do this I had to expose the writers in various places to pass to slog log handlers. It's kinda weird, but I couldn't find a better way.

The stdlib does not include any support for tee logging. So to achieve something similar you can have a single logger write to multiple handlers or more accurately give the logger a handler that will fan out to other handlers. We could write our own handler to do this, but I found a sufficient one here.

We could do more advanced things with these handlers like determining if we want to send debug logs based on log attributes ie. only ship debug logs for nababe. However, to reduce complexity of the PR, I didn't add any fancy stuff.

Tested that logging works for:

  • stderr
  • debug.json
  • logshipping

Example slog output

{
  "time": "2023-10-20T11:24:59.531839Z",
  "level": "INFO",
  "source": {
    "function": "main.runLauncher.func2",
    "file": "/Users/jamespickett/Documents/repos/james-launcher/cmd/launcher/launcher.go",
    "line": 195
  },
  "msg": "slog test with",
  "logger": "knapsack"
}

Copy link
Contributor

@directionless directionless left a comment

Choose a reason for hiding this comment

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

(early thoughts)

Copy link
Contributor

@directionless directionless left a comment

Choose a reason for hiding this comment

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

This feels like it's taking a reasonable shape

Comment on lines 185 to 186
// system log
k.AddReplaceSlogHandler("system", slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{
Copy link
Contributor

Choose a reason for hiding this comment

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

This probably won't work for windows.

Copy link
Contributor

@directionless directionless left a comment

Choose a reason for hiding this comment

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

this is looking pretty good.

@James-Pickett James-Pickett changed the title adds std lib slog logger to knapsack writing to existing log destinations adds std lib slog logger to knapsack Oct 30, 2023
@James-Pickett James-Pickett marked this pull request as ready for review October 30, 2023 20:35
Copy link
Contributor

@directionless directionless left a comment

Choose a reason for hiding this comment

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

I think we could ship this. Sweet!

Copy link
Contributor

@directionless directionless left a comment

Choose a reason for hiding this comment

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

Woo! Looks like a good base

case "error":
ls.slogLevel.Set(slog.LevelError)
case "default":
ls.knapsack.Slogger().Error("unrecognized flag value for log shipping level",
Copy link
Contributor

Choose a reason for hiding this comment

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

Ha! I love that we can use knapsack.Slogger here.

@James-Pickett James-Pickett added this pull request to the merge queue Oct 31, 2023
Merged via the queue into kolide:main with commit 8d46094 Oct 31, 2023
@James-Pickett James-Pickett deleted the james/knapsack-logger branch October 31, 2023 17:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants