Skip to content

Commit

Permalink
cli: Add show-startup-logs flag to reduce startup log verbosity (#1218
Browse files Browse the repository at this point in the history
)

* cli: Add show-startup-logs flag to enable/disable startup logs.

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.

* Final touches

---------

Co-authored-by: Marius Kleidl <marius@transloadit.com>
  • Loading branch information
apoorvapendse and Acconut authored Nov 28, 2024
1 parent 7bc9754 commit f36bb06
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 19 deletions.
15 changes: 8 additions & 7 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)
printStartupLog("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)
printStartupLog("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)
printStartupLog("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 @@ -86,7 +86,7 @@ func CreateComposer() {
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)
printStartupLog("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 +112,7 @@ func CreateComposer() {
if azureEndpoint == "" {
azureEndpoint = fmt.Sprintf("https://%s.blob.core.windows.net", accountName)
}
stdout.Printf("Using Azure endpoint %s.\n", azureEndpoint)
printStartupLog("Using Azure endpoint %s.\n", azureEndpoint)

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

stdout.Printf("Using '%s' as directory storage.\n", dir)
printStartupLog("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 @@ -155,5 +156,5 @@ func CreateComposer() {
locker.UseIn(Composer)
}

stdout.Printf("Using %.2fMB as maximum size.\n", float64(Flags.MaxSize)/1024/1024)
printStartupLog("Using %.2fMB as maximum size.\n", float64(Flags.MaxSize)/1024/1024)
}
4 changes: 3 additions & 1 deletion 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 @@ -190,9 +191,10 @@ func ParseFlags() {
f.StringVar(&Flags.PprofPath, "pprof-path", "/debug/pprof/", "Path under which the pprof endpoint will be accessible")
f.IntVar(&Flags.PprofBlockProfileRate, "pprof-block-profile-rate", 0, "Fraction of goroutine blocking events that are reported in the blocking profile")
f.IntVar(&Flags.PprofMutexProfileRate, "pprof-mutex-profile-rate", 0, "Fraction of mutex contention events that are reported in the mutex profile")
f.BoolVar(&Flags.ShowGreeting, "show-greeting", true, "Show the greeting message")
f.BoolVar(&Flags.ShowGreeting, "show-greeting", true, "Show the greeting message for GET requests to the root path")
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, "Print details about tusd's configuration during startup")
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)
printStartupLog("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)
printStartupLog("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)
printStartupLog("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)
printStartupLog("Using '%s' to load plugin for hooks", Flags.PluginHookPath)

return &plugin.PluginHook{
Path: Flags.PluginHookPath,
Expand Down
8 changes: 8 additions & 0 deletions cmd/tusd/cli/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,11 @@ func (l logWriter) Write(msg []byte) (int, error) {
l.logger.Print(string(msg))
return len(msg), nil
}

func printStartupLog(msg string, args ...interface{}) {
if !Flags.ShowStartupLogs {
return
}

stdout.Printf(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)
printStartupLog("Using %s as the metrics path.\n", Flags.MetricsPath)
mux.Handle(Flags.MetricsPath, promhttp.Handler())
}
12 changes: 6 additions & 6 deletions cmd/tusd/cli/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func Serve() {
enabledHooksString = append(enabledHooksString, string(h))
}

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

} else {
handler, err = tushandler.NewHandler(config)
Expand All @@ -68,20 +68,20 @@ func Serve() {
stderr.Fatalf("Unable to create handler: %s", err)
}

stdout.Printf("Supported tus extensions: %s\n", handler.SupportedExtensions())
printStartupLog("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)
printStartupLog("Using %s as socket to listen.\n", address)
} else {
address = Flags.HttpHost + ":" + Flags.HttpPort
stdout.Printf("Using %s as address to listen.\n", address)
printStartupLog("Using %s as address to listen.\n", address)
}

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

mux := http.NewServeMux()
if basepath == "/" {
Expand Down Expand Up @@ -129,7 +129,7 @@ func Serve() {
}

if Flags.HttpSock == "" {
stdout.Printf("You can now upload files to: %s://%s%s", protocol, listener.Addr(), basepath)
printStartupLog("You can now upload files to: %s://%s%s", protocol, listener.Addr(), basepath)
}

serverCtx, cancelServerCtx := context.WithCancelCause(context.Background())
Expand Down

0 comments on commit f36bb06

Please sign in to comment.