Skip to content

Commit

Permalink
cli: Add show-startup-logs flag to enable/disable startup logs.
Browse files Browse the repository at this point in the history
This adds the 'show-startup-logs' flag to the tusd server, allowing the
user to enable or disable startup logs. This helps the user suppress
startup logs as per their preference.

Fixes #1216.
  • Loading branch information
apoorvapendse committed Nov 28, 2024
1 parent 7bc9754 commit 240ebed
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 25 deletions.
17 changes: 7 additions & 10 deletions cmd/tusd/cli/composer.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ func CreateComposer() {

if Flags.S3Endpoint == "" {
if Flags.S3TransferAcceleration {
stdout.Printf("Using 's3://%s' as S3 bucket for storage with AWS S3 Transfer Acceleration enabled.\n", Flags.S3Bucket)
printStartupLogLine("Using 's3://%s' as S3 bucket for storage with AWS S3 Transfer Acceleration enabled.\n", Flags.S3Bucket)
} else {
stdout.Printf("Using 's3://%s' as S3 bucket for storage.\n", Flags.S3Bucket)
printStartupLogLine("Using 's3://%s' as S3 bucket for storage.\n", Flags.S3Bucket)
}
} else {
stdout.Printf("Using '%s/%s' as S3 endpoint and bucket for storage.\n", Flags.S3Endpoint, Flags.S3Bucket)
printStartupLogLine("Using '%s/%s' as S3 endpoint and bucket for storage.\n", Flags.S3Endpoint, Flags.S3Bucket)
}

s3Client := s3.NewFromConfig(s3Config, func(o *s3.Options) {
Expand Down Expand Up @@ -85,8 +85,7 @@ func CreateComposer() {
if err != nil {
stderr.Fatalf("Unable to create Google Cloud Storage service: %s\n", err)
}

stdout.Printf("Using 'gcs://%s' as GCS bucket for storage.\n", Flags.GCSBucket)
printStartupLogLine("Using 'gcs://%s' as GCS bucket for storage.\n", Flags.GCSBucket)

store := gcsstore.New(Flags.GCSBucket, service)
store.ObjectPrefix = Flags.GCSObjectPrefix
Expand All @@ -112,7 +111,7 @@ func CreateComposer() {
if azureEndpoint == "" {
azureEndpoint = fmt.Sprintf("https://%s.blob.core.windows.net", accountName)
}
stdout.Printf("Using Azure endpoint %s.\n", azureEndpoint)
printStartupLogLine("Using Azure endpoint %s.\n", azureEndpoint)

azConfig := &azurestore.AzConfig{
AccountName: accountName,
Expand Down Expand Up @@ -140,8 +139,7 @@ func CreateComposer() {
if err != nil {
stderr.Fatalf("Unable to make absolute path: %s", err)
}

stdout.Printf("Using '%s' as directory storage.\n", dir)
printStartupLogLine("Using '%s' as directory storage.\n", dir)
if err := os.MkdirAll(dir, os.FileMode(0774)); err != nil {
stderr.Fatalf("Unable to ensure directory exists: %s", err)
}
Expand All @@ -154,6 +152,5 @@ func CreateComposer() {
locker.HolderPollInterval = Flags.FilelockHolderPollInterval
locker.UseIn(Composer)
}

stdout.Printf("Using %.2fMB as maximum size.\n", float64(Flags.MaxSize)/1024/1024)
printStartupLogLine("Using %.2fMB as maximum size.\n", float64(Flags.MaxSize)/1024/1024)
}
2 changes: 2 additions & 0 deletions cmd/tusd/cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ var Flags struct {
PprofMutexProfileRate int
BehindProxy bool
VerboseOutput bool
ShowStartupLogs bool
LogFormat string
S3TransferAcceleration bool
TLSCertFile string
Expand Down Expand Up @@ -193,6 +194,7 @@ func ParseFlags() {
f.BoolVar(&Flags.ShowGreeting, "show-greeting", true, "Show the greeting message")
f.BoolVar(&Flags.ShowVersion, "version", false, "Print tusd version information")
f.BoolVar(&Flags.VerboseOutput, "verbose", true, "Enable verbose logging output")
f.BoolVar(&Flags.ShowStartupLogs, "show-startup-logs", true, "Show the startup logs")
f.StringVar(&Flags.LogFormat, "log-format", "text", "Logging format (text or json)")
})

Expand Down
8 changes: 4 additions & 4 deletions cmd/tusd/cli/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ import (

func getHookHandler(config *handler.Config) hooks.HookHandler {
if Flags.FileHooksDir != "" {
stdout.Printf("Using '%s' for hooks", Flags.FileHooksDir)
printStartupLogLine("Using '%s' for hooks", Flags.FileHooksDir)

return &file.FileHook{
Directory: Flags.FileHooksDir,
}
} else if Flags.HttpHooksEndpoint != "" {
stdout.Printf("Using '%s' as the endpoint for hooks", Flags.HttpHooksEndpoint)
printStartupLogLine("Using '%s' as the endpoint for hooks", Flags.HttpHooksEndpoint)

return &http.HttpHook{
Endpoint: Flags.HttpHooksEndpoint,
Expand All @@ -28,7 +28,7 @@ func getHookHandler(config *handler.Config) hooks.HookHandler {
ForwardHeaders: strings.Split(Flags.HttpHooksForwardHeaders, ","),
}
} else if Flags.GrpcHooksEndpoint != "" {
stdout.Printf("Using '%s' as the endpoint for gRPC hooks", Flags.GrpcHooksEndpoint)
printStartupLogLine("Using '%s' as the endpoint for gRPC hooks", Flags.GrpcHooksEndpoint)

return &grpc.GrpcHook{
Endpoint: Flags.GrpcHooksEndpoint,
Expand All @@ -41,7 +41,7 @@ func getHookHandler(config *handler.Config) hooks.HookHandler {
ForwardHeaders: strings.Split(Flags.GrpcHooksForwardHeaders, ","),
}
} else if Flags.PluginHookPath != "" {
stdout.Printf("Using '%s' to load plugin for hooks", Flags.PluginHookPath)
printStartupLogLine("Using '%s' to load plugin for hooks", Flags.PluginHookPath)

return &plugin.PluginHook{
Path: Flags.PluginHookPath,
Expand Down
8 changes: 7 additions & 1 deletion cmd/tusd/cli/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ func SetupStructuredLogger() {
if Flags.VerboseOutput {
level = slog.LevelDebug
}

replaceAttrFunc := func(groups []string, a slog.Attr) slog.Attr {
// Remove time attribute, because that is handled by the logger
if a.Key == slog.TimeKey {
Expand Down Expand Up @@ -60,3 +59,10 @@ func (l logWriter) Write(msg []byte) (int, error) {
l.logger.Print(string(msg))
return len(msg), nil
}

func printStartupLogLine(msg string, args ...interface{}) {
// Check if the flag allows startup logs
if Flags.ShowStartupLogs {
stdout.Printf("[STARTUP] "+msg, args...)
}
}
2 changes: 1 addition & 1 deletion cmd/tusd/cli/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ func SetupMetrics(mux *http.ServeMux, handler *handler.Handler) {
prometheus.MustRegister(hooks.MetricsHookInvocationsTotal)
prometheus.MustRegister(prometheuscollector.New(handler.Metrics))

stdout.Printf("Using %s as the metrics path.\n", Flags.MetricsPath)
printStartupLogLine("Using %s as the metrics path.\n", Flags.MetricsPath)
mux.Handle(Flags.MetricsPath, promhttp.Handler())
}
14 changes: 5 additions & 9 deletions cmd/tusd/cli/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,31 +58,27 @@ func Serve() {
for _, h := range Flags.EnabledHooks {
enabledHooksString = append(enabledHooksString, string(h))
}

stdout.Printf("Enabled hook events: %s", strings.Join(enabledHooksString, ", "))
printStartupLogLine("Enabled hook events: %s", strings.Join(enabledHooksString, ", "))

} else {
handler, err = tushandler.NewHandler(config)
}
if err != nil {
stderr.Fatalf("Unable to create handler: %s", err)
}

stdout.Printf("Supported tus extensions: %s\n", handler.SupportedExtensions())
printStartupLogLine("Supported tus extensions: %s\n", handler.SupportedExtensions())

basepath := Flags.Basepath
address := ""

if Flags.HttpSock != "" {
address = Flags.HttpSock
stdout.Printf("Using %s as socket to listen.\n", address)
printStartupLogLine("Using %s as socket to listen.\n", address)
} else {
address = Flags.HttpHost + ":" + Flags.HttpPort
stdout.Printf("Using %s as address to listen.\n", address)
printStartupLogLine("Using %s as address to listen.\n", address)
}

stdout.Printf("Using %s as the base path.\n", basepath)

printStartupLogLine("Using %s as the base path.\n", basepath)
mux := http.NewServeMux()
if basepath == "/" {
// If the basepath is set to the root path, only install the tusd handler
Expand Down

0 comments on commit 240ebed

Please sign in to comment.