Skip to content

Commit

Permalink
Removed debug flag & introduced log-level for custom logging needs (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
puthrayaharness authored May 26, 2023
1 parent c675d78 commit 16cc96b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
10 changes: 5 additions & 5 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func Post(reqUrl string, auth string, body interface{}) (respBodyObj ResponseBod
log.WithFields(log.Fields{
"url": reqUrl,
"body": string(postBody),
}).Debug("The request details")
}).Trace("The request details")
req, err := http.NewRequest("POST", reqUrl, requestBody)
if err != nil {
return
Expand All @@ -34,7 +34,7 @@ func Get(reqUrl string, auth string) (respBodyObj ResponseBody, err error) {
}
log.WithFields(log.Fields{
"url": reqUrl,
}).Debug("The request details")
}).Trace("The request details")
req.Header.Set("Content-Type", "application/json")
req.Header.Set(AuthHeaderKey(auth), auth)
return handleResp(req)
Expand All @@ -48,11 +48,11 @@ func Delete(reqUrl string, auth string, body interface{}) (respBodyObj ResponseB
log.WithFields(log.Fields{
"url": reqUrl,
"body": string(postBody),
}).Debug("The request details")
}).Trace("The request details")
} else {
log.WithFields(log.Fields{
"url": reqUrl,
}).Debug("The request details")
}).Trace("The request details")
}
var req *http.Request
if requestBody != nil {
Expand Down Expand Up @@ -86,7 +86,7 @@ func handleResp(req *http.Request) (respBodyObj ResponseBody, err error) {
}
log.WithFields(log.Fields{
"body": string(respBody),
}).Debug("The response body")
}).Trace("The response body")
err = json.Unmarshal(respBody, &respBodyObj)
if err != nil {
log.Fatalln("There was error while parsing the response from server. Exiting...", err)
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ harness-upgrade [global options] command [command options] [arguments...]
| --target-api-key `API_KEY` | `API_KEY` for the target account to authenticate & authorise the migration. |
| --load `FILE` | `FILE` to load flags from |
| --insecure | allow insecure API requests. This is automatically set to true if environment is Dev (default: false) |
| --debug | print debug level logs (default: false) |
| --log-level | set the log level. Possible values - trace, debug, info, warn, error, fatal, panic. Default is `info` |
| --json | log as JSON instead of standard ASCII formatter (default: false). |
| --help, -h | show help. |
| --version, -v | print the version |
Expand Down
20 changes: 13 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ var migrationReq = struct {
TriggerIds string `survey:"triggerIds"`
File string `survey:"load"`
IdentifierCase string `survey:"identifierCase"`
Debug bool `survey:"debug"`
LogLevel string `survey:"logLevel"`
Json bool `survey:"json"`
AllowInsecureReq bool `survey:"insecure"`
ProjectName string `survey:"projectName"`
Expand Down Expand Up @@ -85,9 +85,14 @@ func logMigrationDetails() {
}

func cliWrapper(fn cliFnWrapper, ctx *cli.Context) error {
if migrationReq.Debug {
log.SetLevel(log.DebugLevel)
if len(migrationReq.LogLevel) > 0 {
level, err := log.ParseLevel(migrationReq.LogLevel)
if err != nil {
log.Fatal("Invalid log level")
}
log.SetLevel(level)
}

if migrationReq.Json {
log.SetFormatter(&log.JSONFormatter{})
}
Expand Down Expand Up @@ -194,10 +199,11 @@ func main() {
Usage: "allow insecure API requests. This is automatically set to true if environment is Dev",
Destination: &migrationReq.AllowInsecureReq,
}),
altsrc.NewBoolFlag(&cli.BoolFlag{
Name: "debug",
Usage: "print debug level logs",
Destination: &migrationReq.Debug,
altsrc.NewStringFlag(&cli.StringFlag{
Name: "log-level",
Usage: "set the log level. Possible values - trace, debug, info, warn, error, fatal, panic. Default is `info`",
Destination: &migrationReq.LogLevel,
DefaultText: "info",
}),
altsrc.NewBoolFlag(&cli.BoolFlag{
Name: "json",
Expand Down

0 comments on commit 16cc96b

Please sign in to comment.