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

Support for structured logging #183

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

mwasilew2
Copy link
Contributor

  • adds support for structured (JSON) logs
  • adds support for levelled logging
  • switches to using slog

Note: there's 74 candidates for switching from fmt.Println to log.*

> grep -riI 'fmt\.' . | grep -v "return" | grep -v "fmt.Spr" | grep -v "fmt.Erro" | wc -l
74

If we decide to do it, I think it should be in a separate PR.

@mwasilew2
Copy link
Contributor Author

blocked by: #184

@mwasilew2 mwasilew2 marked this pull request as draft September 27, 2023 15:51
@mwasilew2 mwasilew2 changed the title feat: support for structured logging Support for structured logging Sep 27, 2023
@mwasilew2 mwasilew2 force-pushed the mwasilew2/switch-to-structured-logging branch from 9f5c408 to fb7d284 Compare September 29, 2023 07:18
@mwasilew2 mwasilew2 marked this pull request as ready for review September 29, 2023 07:22
Copy link
Contributor

@tomasmik tomasmik left a comment

Choose a reason for hiding this comment

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

I had a quick read through the documentation of slog and I'm not sure if we actually need it? The json logging is awesome, I guess, but again is that ever useful here? When you get an error its usually just a single string and there isn't really that much useful context to provide imo.

But anyway that's not my main concern. My actual main concern is that this is potentially breaking the output contract and is adding inconsistency because we'll keep using fmt and slog now. What I mean is that, there are people who've written shell scripts to interact with spacectl, I'm afraid if we change the output, we might affect those. Maybe I missed something in the docs of slog though?

},
Before: func(c *cli.Context) error {
var logger *slog.Logger
switch c.String("log-format") {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: It's kinda nicer when flags are defined vars as we can later use those to extract values from context: https://github.com/spacelift-io/spacectl/blob/main/internal/cmd/module/create_version.go#L13

You could just define them above main here 🤷🏻

@mwasilew2
Copy link
Contributor Author

@tomasmik

The json logging is awesome, I guess, but again is that ever useful here?

IMHO it's crucial when doing log analysis and it's super useful when automating things, e.g.

there are people who've written shell scripts to interact with spacectl,

I bet it's not just shell scripts, but also CI/CD pipelines. Parsing those logs if they are not structured can be really painful. Even if using text format and not JSON, the columns are easier to work with when using slog rather than fmt.Println(). We could instrument the cli with usage metrics to get some data on how it's actually being used, but that brings in a huge set of problems of its own.

My actual main concern is that this is potentially breaking the output contract

You're absolutely right and the way I think about this is: do we want to stick with fmt.Println() for the rest of the life of this tool? Maybe not. If not then how do we introduce this change? Well, one approach is to say: we're on 0.x version and it's never been advertised as being stable, so yeah, it will break output for some people. Another way is to say: it's not been officially called stable, but we know that a lot of people are relying on it, so let's start preparing a 1.x release and this change would be one of the breaking changes included. Another approach is to put structured logging behind a feature flag for now, but that's non-trivial. At the moment, outputting is not using any interface so we can't easily change how we print things. Yet another way is to say that we'll be introducing a set of libraries that will provide easy-to-use implementations of functionalities shared across our projects and slog is one of them.

Obviously, there are more ways to approach it and to me, it looks like a judgment call on how fast you want to move vs. how much you want to avoid breaking things. Should we get more feedback from the team?

adding inconsistency because we'll keep using fmt and slog now

That's what I was concerned about as well, but it's a significant effort to switch away from fmt and I wanted to start the conversation before putting in the effort.

@mwasilew2 mwasilew2 marked this pull request as draft September 29, 2023 10:29
Signed-off-by: Michal Wasilewski <[email protected]>
@mwasilew2 mwasilew2 force-pushed the mwasilew2/switch-to-structured-logging branch from fb7d284 to 872c498 Compare September 29, 2023 10:30
@tomasmik
Copy link
Contributor

@mwasilew2, great response with valid points there.

do we want to stick with fmt.Println() for the rest of the life of this tool? Maybe not. If not then how do we introduce this change?

I do not own spacectl so I'd suggest to bring this up in a discussion ticket so we can talk through it. I'm not opposing this idea and I can see some benefit to this, but then I'm also not sure about it and as it requires a decent amount of effort, we should discuss it internally.

@adamconnelly
Copy link
Contributor

I think overall this change is fine. I don't think there's an issue with altering the contract here because the logging is going to stderr rather than stdout. The main thing that could break the contract would be if, for example, we ended up sending log messages to stdout for a command like spacectl stack list --output json.

So basically in my mind there's two things at play here:

  1. Log output (progress indications, errors, etc).
  2. Command output (e.g. the stack list command I referenced previously).

I think it's entirely reasonable for someone to rely on command output as part of automation, and we need to be careful when changing that, but I don't think there's any contract in place when it comes to log output.

Ok, maybe three things actually - we also need to care about what the usability is like when not using spacectl in automation. For example, if I'm a human just using spacectl to query something quickly, I definitely don't want JSON output, and I probably want the output to be pretty minimal so it's not overwhelming.

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.

None yet

3 participants