Skip to content

Commit

Permalink
Added a "debug" flag for the service.
Browse files Browse the repository at this point in the history
  • Loading branch information
jeff-cohere committed Feb 1, 2024
1 parent 6b06146 commit 37f5a14
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
2 changes: 2 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ type serviceConfig struct {
// time after which information about a completed transfer is deleted (seconds)
// default: 7 days
DeleteAfter int `json:"delete_after" yaml:"delete_after"`
// flag indicating whether debug logging and other tools are enabled
Debug bool `json:"debug" yaml:"debug"`
}

// global config variables
Expand Down
1 change: 1 addition & 0 deletions dts.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ service:
data_dir: /path/to/dir # directory DTS uses for data storage
delete_after: 604800 # period after which info about completed transfers
# is deleted (seconds)
debug: true # set to enable debug-level logging and other tools

endpoints: # file transfer endpoints
globus-local:
Expand Down
27 changes: 18 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,36 +53,45 @@ func usage() {
os.Exit(1)
}

func enableLogging() {
logLevel := new(slog.LevelVar)
if config.Service.Debug {
logLevel.Set(slog.LevelDebug)
} else {
logLevel.Set(slog.LevelInfo)
}
handler := slog.NewJSONHandler(os.Stdout,
&slog.HandlerOptions{Level: logLevel})
slog.SetDefault(slog.New(handler))
slog.Debug("Debug logging enabled.")
}

func main() {

// The only argument is the configuration filename.
// the only argument is the configuration filename
if len(os.Args) < 2 {
usage()
}
configFile := os.Args[1]

// enables a default structured logger with JSON output
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
slog.SetDefault(logger)

// Read the configuration file.
// read the configuration file and initialize the config package
log.Printf("Reading configuration from '%s'...\n", configFile)
file, err := os.Open(configFile)
if err != nil {
log.Panicf("Couldn't open %s: %s\n", configFile, err.Error())
}
defer file.Close()

b, err := io.ReadAll(file)
if err != nil {
log.Panicf("Couldn't read configuration data: %s\n", err.Error())
}

// Initialize our configuration and create the service.
err = config.Init(b)
if err != nil {
log.Panicf("Couldn't initialize the configuration: %s\n", err.Error())
}

enableLogging()

service, err := services.NewDTSPrototype()
if err != nil {
log.Panicf("Couldn't create the service: %s\n", err.Error())
Expand Down

0 comments on commit 37f5a14

Please sign in to comment.