Skip to content

Commit

Permalink
logging: Add show-startup-logs flag
Browse files Browse the repository at this point in the history
This enables the user to enable or disable the startup logs while starting
the tusd server. It helps the user suppress the startup logs as per their
preference.

Fixes #1216.
  • Loading branch information
apoorvapendse committed Nov 20, 2024
1 parent 9d85248 commit 09260a0
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 30 deletions.
33 changes: 20 additions & 13 deletions cmd/tusd/cli/composer.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,16 @@ func CreateComposer() {
stderr.Fatalf("Unable to load S3 configuration: %s", err)
}

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)
if Flags.ShowStartupLogs {
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)
} else {
stdout.Printf("Using 's3://%s' as S3 bucket for storage.\n", Flags.S3Bucket)
}
} else {
stdout.Printf("Using 's3://%s' as S3 bucket for storage.\n", Flags.S3Bucket)
stdout.Printf("Using '%s/%s' as S3 endpoint and bucket for storage.\n", Flags.S3Endpoint, Flags.S3Bucket)
}
} else {
stdout.Printf("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 @@ -88,8 +90,9 @@ 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)
if Flags.ShowStartupLogs {
stdout.Printf("Using 'gcs://%s' as GCS bucket for storage.\n", Flags.GCSBucket)
}

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

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

stdout.Printf("Using '%s' as directory storage.\n", dir)
if Flags.ShowStartupLogs {
stdout.Printf("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 @@ -157,6 +163,7 @@ func CreateComposer() {
locker.HolderPollInterval = Flags.FilelockHolderPollInterval
locker.UseIn(Composer)
}

stdout.Printf("Using %.2fMB as maximum size.\n", float64(Flags.MaxSize)/1024/1024)
if Flags.ShowStartupLogs {
stdout.Printf("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
16 changes: 12 additions & 4 deletions cmd/tusd/cli/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@ import (

func getHookHandler(config *handler.Config) hooks.HookHandler {
if Flags.FileHooksDir != "" {
stdout.Printf("Using '%s' for hooks", Flags.FileHooksDir)
if Flags.ShowStartupLogs {
stdout.Printf("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)
if Flags.ShowStartupLogs {
stdout.Printf("Using '%s' as the endpoint for hooks", Flags.HttpHooksEndpoint)
}

return &http.HttpHook{
Endpoint: Flags.HttpHooksEndpoint,
Expand All @@ -28,7 +32,9 @@ 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)
if Flags.ShowStartupLogs {
stdout.Printf("Using '%s' as the endpoint for gRPC hooks", Flags.GrpcHooksEndpoint)
}

return &grpc.GrpcHook{
Endpoint: Flags.GrpcHooksEndpoint,
Expand All @@ -41,7 +47,9 @@ 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)
if Flags.ShowStartupLogs {
stdout.Printf("Using '%s' to load plugin for hooks", Flags.PluginHookPath)
}

return &plugin.PluginHook{
Path: Flags.PluginHookPath,
Expand Down
4 changes: 3 additions & 1 deletion cmd/tusd/cli/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ 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)
if Flags.ShowStartupLogs {
stdout.Printf("Using %s as the metrics path.\n", Flags.MetricsPath)
}
mux.Handle(Flags.MetricsPath, promhttp.Handler())
}
30 changes: 18 additions & 12 deletions cmd/tusd/cli/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,35 +54,41 @@ func Serve() {
if hookHandler != nil {
handler, err = hooks.NewHandlerWithHooks(&config, hookHandler, Flags.EnabledHooks)

var enabledHooksString []string
for _, h := range Flags.EnabledHooks {
enabledHooksString = append(enabledHooksString, string(h))
if Flags.ShowStartupLogs {
var enabledHooksString []string
for _, h := range Flags.EnabledHooks {
enabledHooksString = append(enabledHooksString, string(h))
}
stdout.Printf("Enabled hook events: %s", strings.Join(enabledHooksString, ", "))
}

stdout.Printf("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())
if Flags.ShowStartupLogs {
stdout.Printf("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)
if Flags.ShowStartupLogs {
stdout.Printf("Using %s as socket to listen.\n", address)
}
} else {
address = Flags.HttpHost + ":" + Flags.HttpPort
stdout.Printf("Using %s as address to listen.\n", address)
if Flags.ShowStartupLogs {
stdout.Printf("Using %s as address to listen.\n", address)
}
}
if Flags.ShowStartupLogs {
stdout.Printf("Using %s as the base path.\n", basepath)
}

stdout.Printf("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 09260a0

Please sign in to comment.