Skip to content

Commit

Permalink
Add GetLogSpec to CLI via Admin Service
Browse files Browse the repository at this point in the history
This CR adds a function to the Admin Service to get the active log spec
for a peer and exposes it to Fabric operators via the peer CLI.

FAB-12488 #done

Change-Id: I9ef379d0653cfe6574dc95850229ff4db02491d9
Signed-off-by: Saad Karim <skarim@us.ibm.com>
Signed-off-by: Will Lahti <wtlahti@us.ibm.com>
Signed-off-by: Matthew Sykes <sykesmat@us.ibm.com>
  • Loading branch information
wlahti committed Oct 31, 2018
1 parent 6ab3eeb commit f1c5da6
Show file tree
Hide file tree
Showing 9 changed files with 214 additions and 68 deletions.
17 changes: 14 additions & 3 deletions core/admin/admin.go
Expand Up @@ -91,10 +91,12 @@ func (s *ServerAdmin) SetModuleLogLevel(ctx context.Context, env *common.Envelop
if request == nil {
return nil, errors.New("request is nil")
}
// TODO: FAB-12488
// err = flogging.SetModuleLevels(request.LogModule, request.LogLevel)

spec := fmt.Sprintf("%s:%s=%s", flogging.Global.Spec(), request.LogModule, request.LogLevel)
flogging.ActivateSpec(spec)
err = flogging.Global.ActivateSpec(spec)
if err != nil {
return nil, err
}

logResponse := &pb.LogLevelResponse{LogModule: request.LogModule, LogLevel: strings.ToUpper(request.LogLevel)}
return logResponse, err
Expand All @@ -107,3 +109,12 @@ func (s *ServerAdmin) RevertLogLevels(ctx context.Context, env *common.Envelope)
flogging.ActivateSpec(s.specAtStartup)
return &empty.Empty{}, nil
}

func (s *ServerAdmin) GetLogSpec(ctx context.Context, env *common.Envelope) (*pb.LogSpecResponse, error) {
if _, err := s.v.validate(ctx, env); err != nil {
return nil, err
}
logSpec := flogging.Global.Spec()
logResponse := &pb.LogSpecResponse{LogSpec: logSpec}
return logResponse, nil
}
9 changes: 8 additions & 1 deletion core/admin/admin_test.go
Expand Up @@ -58,7 +58,7 @@ func TestForbidden(t *testing.T) {
adminServer := NewAdminServer(nil)
adminServer.v = &mockValidator{}
mv := adminServer.v.(*mockValidator)
mv.On("validate").Return(nil, accessDenied).Times(5)
mv.On("validate").Return(nil, accessDenied).Times(6)

ctx := context.Background()
status, err := adminServer.GetStatus(ctx, nil)
Expand All @@ -76,6 +76,9 @@ func TestForbidden(t *testing.T) {
_, err = adminServer.RevertLogLevels(ctx, nil)
assert.Equal(t, accessDenied, err)

_, err = adminServer.GetLogSpec(ctx, nil)
assert.Equal(t, accessDenied, err)

_, err = adminServer.StartServer(ctx, nil)
assert.Equal(t, accessDenied, err)
}
Expand Down Expand Up @@ -129,4 +132,8 @@ func TestLoggingCalls(t *testing.T) {
assert.NotNil(t, logResponse, "logResponse should have been set")
assert.Equal(t, flogging.DefaultLevel(), logResponse.LogLevel, "logger level should have been the default")
assert.Nil(t, err, "Error should have been nil")

mv.On("validate").Return(nil, nil).Once()
_, err = adminServer.GetLogSpec(context.Background(), nil)
assert.Nil(t, err, "Error should have been nil")
}
2 changes: 1 addition & 1 deletion peer/clilogging/common.go
Expand Up @@ -57,7 +57,7 @@ func InitCmdFactory() (*LoggingCmdFactory, error) {

func checkLoggingCmdParams(cmd *cobra.Command, args []string) error {
var err error
if cmd.Name() == "revertlevels" {
if cmd.Name() == "revertlevels" || cmd.Name() == "getlogspec" {
if len(args) > 0 {
err = errors.Errorf("more parameters than necessary were provided. Expected 0, received %d", len(args))
return err
Expand Down
51 changes: 51 additions & 0 deletions peer/clilogging/getlogspec.go
@@ -0,0 +1,51 @@
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package clilogging

import (
"context"
"fmt"

pb "github.com/hyperledger/fabric/protos/peer"
"github.com/spf13/cobra"
)

func getLogSpecCmd(cf *LoggingCmdFactory) *cobra.Command {
var loggingGetLogSpecCmd = &cobra.Command{
Use: "getlogspec",
Short: "Returns the active log spec.",
Long: `Returns the active logging specification of the peer.`,
RunE: func(cmd *cobra.Command, args []string) error {
return getLogSpec(cf, cmd, args)
},
}

return loggingGetLogSpecCmd
}

func getLogSpec(cf *LoggingCmdFactory, cmd *cobra.Command, args []string) (err error) {
err = checkLoggingCmdParams(cmd, args)
if err == nil {
// Parsing of the command line is done so silence cmd usage
cmd.SilenceUsage = true

if cf == nil {
cf, err = InitCmdFactory()
if err != nil {
return err
}
}
env := cf.wrapWithEnvelope(&pb.AdminOperation{})
logResponse, err := cf.AdminClient.GetLogSpec(context.Background(), env)
if err != nil {
return err
}
fmt.Println("Current log spec:")
fmt.Println(logResponse.LogSpec)
}
return err
}
15 changes: 3 additions & 12 deletions peer/clilogging/logging.go
@@ -1,17 +1,7 @@
/*
Copyright IBM Corp. 2016 All Rights Reserved.
Copyright IBM Corp. 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.
SPDX-License-Identifier: Apache-2.0
*/

package clilogging
Expand All @@ -36,6 +26,7 @@ func Cmd(cf *LoggingCmdFactory) *cobra.Command {
loggingCmd.AddCommand(getLevelCmd(cf))
loggingCmd.AddCommand(setLevelCmd(cf))
loggingCmd.AddCommand(revertLevelsCmd(cf))
loggingCmd.AddCommand(getLogSpecCmd(cf))

return loggingCmd
}
Expand Down
26 changes: 14 additions & 12 deletions peer/clilogging/logging_test.go
@@ -1,17 +1,7 @@
/*
Copyright Digital Asset Holdings, LLC 2016 All Rights Reserved.
Copyright IBM Corp. 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.
SPDX-License-Identifier: Apache-2.0
*/

package clilogging
Expand Down Expand Up @@ -54,6 +44,8 @@ func initLoggingTest(command string) (*cobra.Command, *LoggingCmdFactory) {
cmd = setLevelCmd(mockCF)
} else if command == "revertlevels" {
cmd = revertLevelsCmd(mockCF)
} else if command == "getlogspec" {
cmd = getLogSpecCmd(mockCF)
} else {
// should only happen when there's a typo in a test case below
}
Expand Down Expand Up @@ -108,3 +100,13 @@ func TestRevertLevels(t *testing.T) {
)
runTests(t, "revertlevels", tc)
}

// TestGetLogSpec tests getlogspec with various parameters
func TestGetLogSpec(t *testing.T) {
var tc []testCase
tc = append(tc,
testCase{"Valid", []string{}, false},
testCase{"ExtraParameter", []string{"peer"}, true},
)
runTests(t, "getlogspec", tc)
}
5 changes: 5 additions & 0 deletions peer/common/mockclient.go
Expand Up @@ -103,3 +103,8 @@ func (m *mockAdminClient) SetModuleLogLevel(ctx context.Context, env *cb.Envelop
func (m *mockAdminClient) RevertLogLevels(ctx context.Context, in *cb.Envelope, opts ...grpc.CallOption) (*empty.Empty, error) {
return &empty.Empty{}, m.err
}

func (m *mockAdminClient) GetLogSpec(ctx context.Context, in *cb.Envelope, opts ...grpc.CallOption) (*pb.LogSpecResponse, error) {
response := &pb.LogSpecResponse{LogSpec: "info"}
return response, m.err
}

0 comments on commit f1c5da6

Please sign in to comment.