diff --git a/internal/logutil/logutil.go b/internal/logutil/logutil.go new file mode 100644 index 0000000..15bdccf --- /dev/null +++ b/internal/logutil/logutil.go @@ -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) + } +} diff --git a/ygnmi/gnmi.go b/ygnmi/gnmi.go index 6ae5cf6..6fb7780 100644 --- a/ygnmi/gnmi.go +++ b/ygnmi/gnmi.go @@ -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" @@ -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)) diff --git a/ygnmi/ygnmi.go b/ygnmi/ygnmi.go index 418dac6..3893d78 100644 --- a/ygnmi/ygnmi.go +++ b/ygnmi/ygnmi.go @@ -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" @@ -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