Skip to content

Commit

Permalink
Merge pull request #85 from openconfig/log-by-line
Browse files Browse the repository at this point in the history
Add logutil.LogByLine to avoid log truncation on logging SetRequests.
  • Loading branch information
wenovus authored Jan 31, 2023
2 parents bb45069 + 7b900d3 commit 9313290
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
33 changes: 33 additions & 0 deletions internal/logutil/logutil.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package logutil

import (
"bufio"
"strings"

log "github.com/golang/glog"
)

const (
// logLimit is used by logging helpers to avoid log truncation on some systems.
logLimit = 15000
)

// LogByLine logs the message line-by-line, and splits lines as well given the
// log limit.
func LogByLine(logLevel log.Level, message string) {
scanner := bufio.NewScanner(strings.NewReader(message))
for scanner.Scan() {
line := scanner.Text()
length := len(line)
for startIndex := 0; startIndex < length; startIndex += logLimit {
endIndex := startIndex + logLimit
if endIndex > length {
endIndex = length
}
log.V(logLevel).Info(line[startIndex:endIndex])
}
}
if err := scanner.Err(); err != nil {
log.Warningf("ygnmi/logutil: error while scanning log input: %v", err)
}
}
3 changes: 2 additions & 1 deletion ygnmi/gnmi.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"strings"
"time"

"github.com/openconfig/ygnmi/internal/logutil"
"github.com/openconfig/ygot/util"
"github.com/openconfig/ygot/ygot"
"google.golang.org/grpc/codes"
Expand Down Expand Up @@ -317,7 +318,7 @@ func set[T any](ctx context.Context, c *Client, q ConfigQuery[T], val T, op setO
req.Prefix = &gpb.Path{
Target: c.target,
}
log.V(c.requestLogLevel).Info(prettySetRequest(req))
logutil.LogByLine(c.requestLogLevel, prettySetRequest(req))
resp, err := c.gnmiC.Set(ctx, req)
log.V(c.requestLogLevel).Infof("SetResponse:\n%s", prototext.Format(resp))

Expand Down
3 changes: 2 additions & 1 deletion ygnmi/ygnmi.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"reflect"
"time"

"github.com/openconfig/ygnmi/internal/logutil"
"github.com/openconfig/ygot/util"
"github.com/openconfig/ygot/ygot"
"github.com/openconfig/ygot/ytypes"
Expand Down Expand Up @@ -593,7 +594,7 @@ func (sb *SetBatch) Set(ctx context.Context, c *Client, opts ...Option) (*Result
req.Prefix = &gpb.Path{
Target: c.target,
}
log.V(c.requestLogLevel).Info(prettySetRequest(req))
logutil.LogByLine(c.requestLogLevel, prettySetRequest(req))
resp, err := c.gnmiC.Set(ctx, req)
log.V(c.requestLogLevel).Infof("SetResponse:\n%s", prototext.Format(resp))
return responseToResult(resp), err
Expand Down

0 comments on commit 9313290

Please sign in to comment.