Skip to content

Commit

Permalink
enable rpc message logging for debug purposes issue Juniper#68
Browse files Browse the repository at this point in the history
  • Loading branch information
alrighttheresham committed Jul 23, 2018
1 parent ecb6058 commit 8923969
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
4 changes: 4 additions & 0 deletions examples/ssh1/ssh1.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package main
import (
"fmt"
"log"
"os"

"github.com/Juniper/go-netconf/netconf"
"golang.org/x/crypto/ssh"
Expand All @@ -29,6 +30,9 @@ func main() {

defer s.Close()

// enable RPC logging
netconf.Logger.SetOutput(os.Stderr)

fmt.Println(s.ServerCapabilities)
fmt.Println(s.SessionID)

Expand Down
28 changes: 28 additions & 0 deletions netconf/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Go NETCONF Client
//
// Copyright (c) 2013-2018, Juniper Networks, Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package netconf

import (
"io/ioutil"
"log"
"os"
)

func init() {
// Discard is an io.Writer on which all Write calls succeed without doing anything.
Logger.SetOutput(ioutil.Discard)
}

// NopLogger is a wrapper for the standard logger
type NopLogger struct {
*log.Logger
}

// Logger provides request and response payload logging for the Netconf Exec
var Logger = &NopLogger{
log.New(os.Stderr, "", log.LstdFlags),
}
34 changes: 34 additions & 0 deletions netconf/log_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Go NETCONF Client
//
// Copyright (c) 2013-2018, Juniper Networks, Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package netconf

import (
"io/ioutil"
"log"
"os"
"testing"
)

func BenchmarkLog(b *testing.B) {
log.SetFlags(log.LstdFlags)
file, err := ioutil.TempFile("", "benchmark-log")
if err != nil {
log.SetOutput(os.Stderr)
} else {
log.SetOutput(file)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
log.Printf("Benchmark %d", i)
}
}

func BenchmarkNopLogger(b *testing.B) {
for i := 0; i < b.N; i++ {
Logger.Println("Benchmark %d", i)
}
}
6 changes: 6 additions & 0 deletions netconf/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ func (s *Session) Exec(methods ...RPCMethod) (*RPCReply, error) {
return nil, err
}

// write the request to a no op logger, which can be overriden by the library user
Logger.Println(string(request))

rawXML, err := s.Transport.Receive()
if err != nil {
return nil, err
Expand All @@ -54,6 +57,9 @@ func (s *Session) Exec(methods ...RPCMethod) (*RPCReply, error) {
return nil, err
}

// write the response to a no op logger, which can be overriden by the library user
Logger.Println(reply.RawReply)

return reply, nil
}

Expand Down

0 comments on commit 8923969

Please sign in to comment.