Skip to content

Commit f059a2e

Browse files
authored
Merge pull request #10 from ymartin-ovh/dev/yannick.martin/logging-level
Added logging level
2 parents ed44370 + de8e013 commit f059a2e

File tree

5 files changed

+87
-8
lines changed

5 files changed

+87
-8
lines changed

README.md

+9
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@ fields, enable the nested functionality - otherwise, ensure that your fields are
3535
not required and choose sane defaults if it's absolutely necessary for data to
3636
be present.
3737

38+
## A Note on Logging
39+
40+
This software uses apex library to handle logging. You can control verbosity by
41+
setting environment variable PHPIPAMSDK_LOGLEVEL to one of supported value:
42+
* debug
43+
* info
44+
* warn
45+
* error
46+
* fatal
3847

3948
## License
4049

go.mod

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
module github.com/pavel-z1/phpipam-sdk-go
22

3-
require github.com/imdario/mergo v0.0.0-20160517064435-50d4dbd4eb0e
3+
require (
4+
github.com/Bowery/prompt v0.0.0-20190916142128-fa8279994f75 // indirect
5+
github.com/apex/log v1.9.0 // indirect
6+
github.com/dchest/safefile v0.0.0-20151022103144-855e8d98f185 // indirect
7+
github.com/go-logfmt/logfmt v0.6.0 // indirect
8+
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
9+
github.com/imdario/mergo v0.0.0-20160517064435-50d4dbd4eb0e
10+
github.com/kardianos/govendor v1.0.9 // indirect
11+
github.com/pkg/errors v0.9.1 // indirect
12+
golang.org/x/tools v0.7.0 // indirect
13+
gopkg.in/yaml.v2 v2.4.0 // indirect
14+
)
415

516
go 1.13

phpipam/client/client.go

+23-3
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ package client
44

55
import (
66
"fmt"
7-
"log"
7+
"os"
88

9+
"github.com/apex/log"
10+
"github.com/apex/log/handlers/logfmt"
911
"github.com/pavel-z1/phpipam-sdk-go/phpipam"
1012
"github.com/pavel-z1/phpipam-sdk-go/phpipam/request"
1113
"github.com/pavel-z1/phpipam-sdk-go/phpipam/session"
@@ -20,12 +22,30 @@ type Client struct {
2022

2123
// NewClient creates a new client.
2224
func NewClient(s *session.Session) *Client {
25+
log.SetLevel(log.InfoLevel)
26+
log.SetHandler(logfmt.New(os.Stderr))
27+
28+
env_loglevel := os.Getenv("PHPIPAMSDK_LOGLEVEL")
29+
if env_loglevel != "" {
30+
loglevel, err := log.ParseLevel(env_loglevel)
31+
if err == nil {
32+
log.SetLevel(loglevel)
33+
} else {
34+
log.Warnf("Invalid log level, defaulting to info: %s", err)
35+
}
36+
}
37+
2338
c := &Client{
2439
Session: s,
2540
}
2641
return c
2742
}
2843

44+
// change logger level, default is info
45+
func SetLevel(level log.Level) {
46+
log.SetLevel(level)
47+
}
48+
2949
// loginSession logs in a session via the user controller. This is the only
3050
// valid operation if the session does not have a token yet.
3151
func loginSession(s *session.Session) error {
@@ -42,7 +62,7 @@ func loginSession(s *session.Session) error {
4262
return err
4363
}
4464
s.Token = out
45-
}
65+
}
4666
return nil
4767
}
4868

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

phpipam/request/request.go

+24-4
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ import (
77
"encoding/json"
88
"fmt"
99
"io/ioutil"
10-
"log"
1110
"net/http"
11+
"os"
1212

13+
"github.com/apex/log"
14+
"github.com/apex/log/handlers/logfmt"
1315
"github.com/pavel-z1/phpipam-sdk-go/phpipam/session"
1416
)
1517

@@ -116,7 +118,7 @@ func newRequestResponse(r *http.Response) *requestResponse {
116118
}
117119
defer r.Body.Close()
118120
body, err := ioutil.ReadAll(r.Body)
119-
log.Printf("Response Body Debug ................... %s", body)
121+
log.Debugf("Response Body Debug ................... %s", body)
120122
if err != nil {
121123
panic(err)
122124
}
@@ -146,12 +148,12 @@ func (r *Request) Send() error {
146148
switch r.Method {
147149
case "OPTIONS", "GET", "POST", "PUT", "PATCH", "DELETE":
148150
bs, err := json.Marshal(r.Input)
149-
log.Printf("Request Body Debug ................... %s", bs)
151+
log.Debugf("Request Body Debug ................... %s", bs)
150152
if err != nil {
151153
return fmt.Errorf("Error preparing request data: %s", err)
152154
}
153155
buf := bytes.NewBuffer(bs)
154-
log.Printf("Request URL Debug ...................Method: %s, UR: %s/%s%s", r.Method, r.Session.Config.Endpoint, r.Session.Config.AppID, r.URI)
156+
log.Debugf("Request URL Debug ...................Method: %s, UR: %s/%s%s", r.Method, r.Session.Config.Endpoint, r.Session.Config.AppID, r.URI)
155157
req, err = http.NewRequest(r.Method, fmt.Sprintf("%s/%s%s", r.Session.Config.Endpoint, r.Session.Config.AppID, r.URI), buf)
156158
req.Header.Add("Content-Type", "application/json")
157159
default:
@@ -196,8 +198,26 @@ func (r *Request) Send() error {
196198

197199
// NewRequest creates a new request instance with configuration set.
198200
func NewRequest(s *session.Session) *Request {
201+
log.SetLevel(log.InfoLevel)
202+
log.SetHandler(logfmt.New(os.Stderr))
203+
204+
env_loglevel := os.Getenv("PHPIPAMSDK_LOGLEVEL")
205+
if env_loglevel != "" {
206+
loglevel, err := log.ParseLevel(env_loglevel)
207+
if err == nil {
208+
log.SetLevel(loglevel)
209+
} else {
210+
log.Warnf("Invalid log level, defaulting to info: %s", err)
211+
}
212+
}
213+
199214
r := &Request{
200215
Session: s,
201216
}
202217
return r
203218
}
219+
220+
// change logger level, default is info
221+
func SetLevel(level log.Level) {
222+
log.SetLevel(level)
223+
}

vendor/vendor.json

+19
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,30 @@
22
"comment": "",
33
"ignore": "test",
44
"package": [
5+
{
6+
"checksumSHA1": "XiHV3bYcd1RJ03nIT/U4QFynF6w=",
7+
"path": "github.com/apex/log",
8+
"revision": "3edb93e9f62c13dccdbf0759bd9da5b71dc60926",
9+
"revisionTime": "2020-08-18T07:36:17Z",
10+
"version": "v1.9.0",
11+
"versionExact": "v1.9.0"
12+
},
513
{
614
"checksumSHA1": "hwGdeQbcfc2RvIQS5wAaYRKJDd4=",
715
"path": "github.com/imdario/mergo",
816
"revision": "50d4dbd4eb0e84778abe37cefef140271d96fade",
917
"revisionTime": "2016-05-17T06:44:35Z"
18+
},
19+
{
20+
"checksumSHA1": "Qo2E/26skb9mZQ3b2Mh6QDkpBLs=",
21+
"path": "github.com/pkg/errors",
22+
"revision": "5dd12d0cfe7f152f80558d591504ce685299311e",
23+
"revisionTime": "2020-12-14T06:45:52Z"
24+
},
25+
{
26+
"checksumSHA1": "zamq7bor1Nt+tXE3sJ3lH21K/f4=",
27+
"path": "log",
28+
"revision": ""
1029
}
1130
],
1231
"rootPath": "github.com/pavel-z1/phpipam-sdk-go"

0 commit comments

Comments
 (0)