-
Notifications
You must be signed in to change notification settings - Fork 1k
feat: add stackdriver and json log options to coder server
#5682
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
Changes from 1 commit
b32b696
6ebb833
c9e3898
404d522
d929d0f
5d11042
1a56dbe
21eb106
3c87011
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,6 +46,8 @@ import ( | |
|
||
"cdr.dev/slog" | ||
"cdr.dev/slog/sloggers/sloghuman" | ||
"cdr.dev/slog/sloggers/slogjson" | ||
"cdr.dev/slog/sloggers/slogstackdriver" | ||
"github.com/coder/coder/buildinfo" | ||
"github.com/coder/coder/cli/cliui" | ||
"github.com/coder/coder/cli/config" | ||
|
@@ -122,13 +124,11 @@ func Server(vip *viper.Viper, newAPI func(context.Context, *coderd.Options) (*co | |
} | ||
|
||
printLogo(cmd) | ||
logger := slog.Make(sloghuman.Sink(cmd.ErrOrStderr())) | ||
if ok, _ := cmd.Flags().GetBool(varVerbose); ok { | ||
logger = logger.Leveled(slog.LevelDebug) | ||
} | ||
if cfg.Trace.CaptureLogs.Value { | ||
logger = logger.AppendSinks(tracing.SlogSink{}) | ||
logger, logCloser, err := makeLogger(cmd, cfg) | ||
if err != nil { | ||
return xerrors.Errorf("make logger: %w", err) | ||
} | ||
defer logCloser() | ||
|
||
// Register signals early on so that graceful shutdown can't | ||
// be interrupted by additional signals. Note that we avoid | ||
|
@@ -1145,6 +1145,11 @@ func newProvisionerDaemon( | |
|
||
// nolint: revive | ||
func printLogo(cmd *cobra.Command) { | ||
// Only print the logo in TTYs. | ||
if !isTTYOut(cmd) { | ||
return | ||
} | ||
|
||
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "%s - Software development on your infrastucture\n", cliui.Styles.Bold.Render("Coder "+buildinfo.Version())) | ||
} | ||
|
||
|
@@ -1512,3 +1517,64 @@ func redirectHTTPToAccessURL(handler http.Handler, accessURL *url.URL) http.Hand | |
func isLocalhost(host string) bool { | ||
return host == "localhost" || host == "127.0.0.1" || host == "::1" | ||
} | ||
|
||
func makeLogger(cmd *cobra.Command, cfg *codersdk.DeploymentConfig) (slog.Logger, func(), error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should add testing around this. If logging breaks in any way, we really hurt our customers. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @coadler since you can't read sinks from an instantiated logger, you could make this return There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like that! |
||
var ( | ||
sinks = []slog.Sink{} | ||
closers = []func() error{} | ||
) | ||
|
||
if cfg.Logging.Human.Value != "" { | ||
if cfg.Logging.Human.Value == "/dev/stderr" { | ||
sinks = append(sinks, sloghuman.Sink(cmd.ErrOrStderr())) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice touch 👍 |
||
} else { | ||
fi, err := os.OpenFile(cfg.Logging.Human.Value, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644) | ||
if err != nil { | ||
return slog.Logger{}, nil, xerrors.Errorf("open human log %q: %w", cfg.Logging.Human.Value, err) | ||
} | ||
closers = append(closers, fi.Close) | ||
sinks = append(sinks, sloghuman.Sink(fi)) | ||
} | ||
} | ||
|
||
if cfg.Logging.JSON.Value != "" { | ||
if cfg.Logging.JSON.Value == "/dev/stderr" { | ||
sinks = append(sinks, slogjson.Sink(cmd.ErrOrStderr())) | ||
} else { | ||
fi, err := os.OpenFile(cfg.Logging.JSON.Value, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644) | ||
if err != nil { | ||
return slog.Logger{}, nil, xerrors.Errorf("open json log %q: %w", cfg.Logging.JSON.Value, err) | ||
} | ||
closers = append(closers, fi.Close) | ||
sinks = append(sinks, slogjson.Sink(fi)) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This whole routine could be made into a little helper function (even just a function stored in a variable at the top of this function would be good) that returns an io.Writer |
||
} | ||
|
||
if cfg.Logging.Stackdriver.Value != "" { | ||
if cfg.Logging.JSON.Value == "/dev/stderr" { | ||
sinks = append(sinks, slogstackdriver.Sink(cmd.ErrOrStderr())) | ||
} else { | ||
fi, err := os.OpenFile(cfg.Logging.Stackdriver.Value, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644) | ||
if err != nil { | ||
return slog.Logger{}, nil, xerrors.Errorf("open stackdriver log %q: %w", cfg.Logging.Stackdriver.Value, err) | ||
} | ||
closers = append(closers, fi.Close) | ||
sinks = append(sinks, slogstackdriver.Sink(fi)) | ||
} | ||
} | ||
|
||
if cfg.Trace.CaptureLogs.Value { | ||
sinks = append(sinks, tracing.SlogSink{}) | ||
} | ||
|
||
level := slog.LevelInfo | ||
if ok, _ := cmd.Flags().GetBool(varVerbose); ok { | ||
level = slog.LevelDebug | ||
} | ||
|
||
return slog.Make(sinks...).Leveled(level), func() { | ||
for _, closer := range closers { | ||
_ = closer() | ||
} | ||
}, nil | ||
} |
Uh oh!
There was an error while loading. Please reload this page.