-
Notifications
You must be signed in to change notification settings - Fork 8.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CLI to set/get module log levels on peer
Dynamically set or get the log level for the specified module running on a peer. Useful for problem determination / debugging. Use the following format: peer logging setlevel <module-name> <log-level> peer logging getlevel <module-name> Fix Issue FAB-574 Change-Id: I5b16d100a84393bafe052511bb2e6f188214e81e Signed-off-by: Will Lahti <wtlahti@us.ibm.com>
- Loading branch information
Showing
12 changed files
with
536 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
Copyright IBM Corp. 2016 All Rights Reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package clilogging | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/op/go-logging" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func checkLoggingCmdParams(cmd *cobra.Command, args []string) error { | ||
var err error | ||
|
||
// check that at least one parameter is passed in | ||
if len(args) == 0 { | ||
return fmt.Errorf("no parameters provided") | ||
} | ||
|
||
if cmd.Name() == "setlevel" { | ||
if len(args) == 1 { | ||
err = fmt.Errorf("no log level provided") | ||
} else { | ||
// check that log level is valid. if not, err is set | ||
_, err = logging.LogLevel(args[1]) | ||
if err != nil { | ||
err = fmt.Errorf("%s - %s", err, args[1]) | ||
} | ||
} | ||
} | ||
|
||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
Copyright IBM Corp. 2016 All Rights Reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package clilogging | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/hyperledger/fabric/core/peer" | ||
pb "github.com/hyperledger/fabric/protos" | ||
"github.com/spf13/cobra" | ||
"golang.org/x/net/context" | ||
) | ||
|
||
func getLevelCmd() *cobra.Command { | ||
return loggingGetLevelCmd | ||
} | ||
|
||
var loggingGetLevelCmd = &cobra.Command{ | ||
Use: "getlevel <module>", | ||
Short: "Returns the logging level of the requested module logger.", | ||
Long: `Returns the logging level of the requested module logger`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
getLevel(cmd, args) | ||
}, | ||
} | ||
|
||
func getLevel(cmd *cobra.Command, args []string) (err error) { | ||
err = checkLoggingCmdParams(cmd, args) | ||
|
||
if err != nil { | ||
logger.Warningf("Error: %s", err) | ||
} else { | ||
clientConn, err := peer.NewPeerClientConnection() | ||
if err != nil { | ||
logger.Infof("Error trying to connect to local peer: %s", err) | ||
err = fmt.Errorf("Error trying to connect to local peer: %s", err) | ||
fmt.Println(&pb.ServerStatus{Status: pb.ServerStatus_UNKNOWN}) | ||
return err | ||
} | ||
|
||
serverClient := pb.NewAdminClient(clientConn) | ||
|
||
logResponse, err := serverClient.GetModuleLogLevel(context.Background(), &pb.LogLevelRequest{LogModule: args[0]}) | ||
|
||
if err != nil { | ||
logger.Warningf("Error retrieving log level") | ||
return err | ||
} | ||
logger.Infof("Current log level for module '%s': %s", logResponse.LogModule, logResponse.LogLevel) | ||
} | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
Copyright IBM Corp. 2016 All Rights Reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package clilogging | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/op/go-logging" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
const loggingFuncName = "logging" | ||
|
||
var logger = logging.MustGetLogger("loggingCmd") | ||
|
||
// Cmd returns the cobra command for Logging | ||
func Cmd() *cobra.Command { | ||
loggingCmd.AddCommand(getLevelCmd()) | ||
loggingCmd.AddCommand(setLevelCmd()) | ||
|
||
return loggingCmd | ||
} | ||
|
||
var loggingCmd = &cobra.Command{ | ||
Use: loggingFuncName, | ||
Short: fmt.Sprintf("%s specific commands.", loggingFuncName), | ||
Long: fmt.Sprintf("%s specific commands.", loggingFuncName), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
Copyright Digital Asset Holdings, LLC 2016 All Rights Reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package clilogging | ||
|
||
import "testing" | ||
|
||
// TestGetLevelEmptyParams tests the parameter checking for getlevel, which | ||
// should return an error when no parameters are provided | ||
func TestGetLevelEmptyParams(t *testing.T) { | ||
var args []string | ||
|
||
err := checkLoggingCmdParams(getLevelCmd(), args) | ||
|
||
if err == nil { | ||
t.FailNow() | ||
} | ||
} | ||
|
||
// TestGetLevel tests the parameter checking for getlevel, which should | ||
// should return a nil error when one (or more) parameters are provided | ||
func TestGetLevel(t *testing.T) { | ||
args := make([]string, 1) | ||
args[0] = "peer" | ||
|
||
err := checkLoggingCmdParams(getLevelCmd(), args) | ||
|
||
if err != nil { | ||
t.FailNow() | ||
} | ||
} | ||
|
||
// TestSetLevelEmptyParams tests the parameter checking for setlevel, which | ||
// should return an error when no parameters are provided | ||
func TestSetLevelEmptyParams(t *testing.T) { | ||
var args []string | ||
|
||
err := checkLoggingCmdParams(setLevelCmd(), args) | ||
|
||
if err == nil { | ||
t.FailNow() | ||
} | ||
} | ||
|
||
// TestSetLevelEmptyParams tests the parameter checking for setlevel, which | ||
// should return an error when only one parameter is provided | ||
func TestSetLevelOneParam(t *testing.T) { | ||
args := make([]string, 1) | ||
args[0] = "peer" | ||
|
||
err := checkLoggingCmdParams(setLevelCmd(), args) | ||
|
||
if err == nil { | ||
t.FailNow() | ||
} | ||
} | ||
|
||
// TestSetLevelEmptyParams tests the parameter checking for setlevel, which | ||
// should return an error when an invalid log level is provided | ||
func TestSetLevelInvalid(t *testing.T) { | ||
args := make([]string, 2) | ||
args[0] = "peer" | ||
args[1] = "invalidlevel" | ||
|
||
err := checkLoggingCmdParams(setLevelCmd(), args) | ||
|
||
if err == nil { | ||
t.FailNow() | ||
} | ||
} | ||
|
||
// TestSetLevelEmptyParams tests the parameter checking for setlevel, which | ||
// should return a nil error when two parameters, the second of which is a | ||
// valid log level, are provided | ||
func TestSetLevel(t *testing.T) { | ||
args := make([]string, 2) | ||
args[0] = "peer" | ||
args[1] = "debug" | ||
|
||
err := checkLoggingCmdParams(setLevelCmd(), args) | ||
|
||
if err != nil { | ||
t.FailNow() | ||
} | ||
} |
Oops, something went wrong.