Skip to content

Commit

Permalink
Merge pull request #10 from ymartin-ovh/dev/yannick.martin/logging-level
Browse files Browse the repository at this point in the history
Added logging level
  • Loading branch information
pavel-z1 authored Apr 14, 2023
2 parents ed44370 + de8e013 commit f059a2e
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 8 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ fields, enable the nested functionality - otherwise, ensure that your fields are
not required and choose sane defaults if it's absolutely necessary for data to
be present.

## A Note on Logging

This software uses apex library to handle logging. You can control verbosity by
setting environment variable PHPIPAMSDK_LOGLEVEL to one of supported value:
* debug
* info
* warn
* error
* fatal

## License

Expand Down
13 changes: 12 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
module github.com/pavel-z1/phpipam-sdk-go

require github.com/imdario/mergo v0.0.0-20160517064435-50d4dbd4eb0e
require (
github.com/Bowery/prompt v0.0.0-20190916142128-fa8279994f75 // indirect
github.com/apex/log v1.9.0 // indirect
github.com/dchest/safefile v0.0.0-20151022103144-855e8d98f185 // indirect
github.com/go-logfmt/logfmt v0.6.0 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/imdario/mergo v0.0.0-20160517064435-50d4dbd4eb0e
github.com/kardianos/govendor v1.0.9 // indirect
github.com/pkg/errors v0.9.1 // indirect
golang.org/x/tools v0.7.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)

go 1.13
26 changes: 23 additions & 3 deletions phpipam/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ package client

import (
"fmt"
"log"
"os"

"github.com/apex/log"
"github.com/apex/log/handlers/logfmt"
"github.com/pavel-z1/phpipam-sdk-go/phpipam"
"github.com/pavel-z1/phpipam-sdk-go/phpipam/request"
"github.com/pavel-z1/phpipam-sdk-go/phpipam/session"
Expand All @@ -20,12 +22,30 @@ type Client struct {

// NewClient creates a new client.
func NewClient(s *session.Session) *Client {
log.SetLevel(log.InfoLevel)
log.SetHandler(logfmt.New(os.Stderr))

env_loglevel := os.Getenv("PHPIPAMSDK_LOGLEVEL")
if env_loglevel != "" {
loglevel, err := log.ParseLevel(env_loglevel)
if err == nil {
log.SetLevel(loglevel)
} else {
log.Warnf("Invalid log level, defaulting to info: %s", err)
}
}

c := &Client{
Session: s,
}
return c
}

// change logger level, default is info
func SetLevel(level log.Level) {
log.SetLevel(level)
}

// loginSession logs in a session via the user controller. This is the only
// valid operation if the session does not have a token yet.
func loginSession(s *session.Session) error {
Expand All @@ -42,7 +62,7 @@ func loginSession(s *session.Session) error {
return err
}
s.Token = out
}
}
return nil
}

Expand Down Expand Up @@ -106,7 +126,7 @@ func (c *Client) GetCustomFields(id int, controller string) (out map[string]inte
schema, err = c.GetCustomFieldsSchema(controller)
switch {
case err != nil:
log.Printf("Error getting custom Fields: %s", err)
log.Warnf("Error getting custom Fields: %s", err)
return
}

Expand Down
28 changes: 24 additions & 4 deletions phpipam/request/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"

"github.com/apex/log"
"github.com/apex/log/handlers/logfmt"
"github.com/pavel-z1/phpipam-sdk-go/phpipam/session"
)

Expand Down Expand Up @@ -116,7 +118,7 @@ func newRequestResponse(r *http.Response) *requestResponse {
}
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
log.Printf("Response Body Debug ................... %s", body)
log.Debugf("Response Body Debug ................... %s", body)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -146,12 +148,12 @@ func (r *Request) Send() error {
switch r.Method {
case "OPTIONS", "GET", "POST", "PUT", "PATCH", "DELETE":
bs, err := json.Marshal(r.Input)
log.Printf("Request Body Debug ................... %s", bs)
log.Debugf("Request Body Debug ................... %s", bs)
if err != nil {
return fmt.Errorf("Error preparing request data: %s", err)
}
buf := bytes.NewBuffer(bs)
log.Printf("Request URL Debug ...................Method: %s, UR: %s/%s%s", r.Method, r.Session.Config.Endpoint, r.Session.Config.AppID, r.URI)
log.Debugf("Request URL Debug ...................Method: %s, UR: %s/%s%s", r.Method, r.Session.Config.Endpoint, r.Session.Config.AppID, r.URI)
req, err = http.NewRequest(r.Method, fmt.Sprintf("%s/%s%s", r.Session.Config.Endpoint, r.Session.Config.AppID, r.URI), buf)
req.Header.Add("Content-Type", "application/json")
default:
Expand Down Expand Up @@ -196,8 +198,26 @@ func (r *Request) Send() error {

// NewRequest creates a new request instance with configuration set.
func NewRequest(s *session.Session) *Request {
log.SetLevel(log.InfoLevel)
log.SetHandler(logfmt.New(os.Stderr))

env_loglevel := os.Getenv("PHPIPAMSDK_LOGLEVEL")
if env_loglevel != "" {
loglevel, err := log.ParseLevel(env_loglevel)
if err == nil {
log.SetLevel(loglevel)
} else {
log.Warnf("Invalid log level, defaulting to info: %s", err)
}
}

r := &Request{
Session: s,
}
return r
}

// change logger level, default is info
func SetLevel(level log.Level) {
log.SetLevel(level)
}
19 changes: 19 additions & 0 deletions vendor/vendor.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,30 @@
"comment": "",
"ignore": "test",
"package": [
{
"checksumSHA1": "XiHV3bYcd1RJ03nIT/U4QFynF6w=",
"path": "github.com/apex/log",
"revision": "3edb93e9f62c13dccdbf0759bd9da5b71dc60926",
"revisionTime": "2020-08-18T07:36:17Z",
"version": "v1.9.0",
"versionExact": "v1.9.0"
},
{
"checksumSHA1": "hwGdeQbcfc2RvIQS5wAaYRKJDd4=",
"path": "github.com/imdario/mergo",
"revision": "50d4dbd4eb0e84778abe37cefef140271d96fade",
"revisionTime": "2016-05-17T06:44:35Z"
},
{
"checksumSHA1": "Qo2E/26skb9mZQ3b2Mh6QDkpBLs=",
"path": "github.com/pkg/errors",
"revision": "5dd12d0cfe7f152f80558d591504ce685299311e",
"revisionTime": "2020-12-14T06:45:52Z"
},
{
"checksumSHA1": "zamq7bor1Nt+tXE3sJ3lH21K/f4=",
"path": "log",
"revision": ""
}
],
"rootPath": "github.com/pavel-z1/phpipam-sdk-go"
Expand Down

0 comments on commit f059a2e

Please sign in to comment.