-
Notifications
You must be signed in to change notification settings - Fork 273
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for JSON-formatted logging
- Loading branch information
Showing
6 changed files
with
201 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
Copyright 2024 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package log | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/spf13/pflag" | ||
) | ||
|
||
type Format string | ||
|
||
const ( | ||
JSON Format = "json" | ||
Text Format = "text" | ||
) | ||
|
||
const ( | ||
flagLogFormat = "log-format" | ||
flagLogFlushFrequency = "log-flush-frequency" | ||
|
||
DefaultFormat = Text | ||
DefaultFlushFrequency = 5 * time.Second | ||
) | ||
|
||
func BindCLIFlags(fs *pflag.FlagSet) { | ||
fs.String(flagLogFormat, "text", "The log format to use. One of: text, json.") | ||
// log-flush-frequency is registered in kubernetes component-base. | ||
} | ||
|
||
type Options struct { | ||
Format Format | ||
FlushFrequency time.Duration | ||
} | ||
|
||
func OptionsFromCLIFlags(fs *pflag.FlagSet) *Options { | ||
var o Options | ||
|
||
o.Format = Format(fs.Lookup(flagLogFormat).Value.String()) | ||
if o.Format != JSON && o.Format != Text { | ||
SetupError("Invalid log format", flagLogFormat, o.Format) | ||
o.Format = DefaultFormat | ||
} | ||
|
||
freq, err := fs.GetDuration(flagLogFlushFrequency) | ||
if err != nil { | ||
SetupError("Invalid log flush frequency", flagLogFlushFrequency, err) | ||
o.FlushFrequency = DefaultFlushFrequency | ||
} | ||
o.FlushFrequency = freq | ||
|
||
return &o | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
Copyright 2024 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package log | ||
|
||
import ( | ||
"context" | ||
"io" | ||
"log/slog" | ||
) | ||
|
||
const ( | ||
TimeKey = slog.TimeKey | ||
LevelKey = slog.LevelKey | ||
VerbosityKey = "v" | ||
) | ||
|
||
type SLogJSONHandler struct { | ||
handler *slog.JSONHandler | ||
} | ||
|
||
func NewJSONHandler(w io.Writer, opts *slog.HandlerOptions) *SLogJSONHandler { | ||
return &SLogJSONHandler{ | ||
handler: slog.NewJSONHandler(w, opts), | ||
} | ||
} | ||
|
||
func (h *SLogJSONHandler) Enabled(ctx context.Context, level slog.Level) bool { | ||
return h.handler.Enabled(ctx, level) | ||
} | ||
|
||
func (h *SLogJSONHandler) Handle(ctx context.Context, record slog.Record) error { | ||
// Extract verbosity from negative log levels and set it as an attribute. | ||
// This is necessary because slog will convert negative levels to DEBUG+N. | ||
if record.Level < 0 { | ||
verbosity := int(-record.Level) | ||
record.Level = slog.LevelInfo | ||
record.AddAttrs(slog.Int(VerbosityKey, verbosity)) | ||
} else { | ||
record.AddAttrs(slog.Int(VerbosityKey, 0)) | ||
} | ||
|
||
return h.handler.Handle(ctx, record) | ||
} | ||
|
||
func (h *SLogJSONHandler) WithAttrs(attrs []slog.Attr) slog.Handler { | ||
return h.handler.WithAttrs(attrs) | ||
} | ||
|
||
func (h *SLogJSONHandler) WithGroup(name string) slog.Handler { | ||
return h.handler.WithGroup(name) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
Copyright 2024 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package log | ||
|
||
import ( | ||
"log/slog" | ||
) | ||
|
||
func SetupError(msg string, args ...any) { | ||
slog.Error(msg, args...) | ||
} |