Skip to content

Commit f1c5da6

Browse files
committed
Add GetLogSpec to CLI via Admin Service
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>
1 parent 6ab3eeb commit f1c5da6

File tree

9 files changed

+214
-68
lines changed

9 files changed

+214
-68
lines changed

core/admin/admin.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,12 @@ func (s *ServerAdmin) SetModuleLogLevel(ctx context.Context, env *common.Envelop
9191
if request == nil {
9292
return nil, errors.New("request is nil")
9393
}
94-
// TODO: FAB-12488
95-
// err = flogging.SetModuleLevels(request.LogModule, request.LogLevel)
94+
9695
spec := fmt.Sprintf("%s:%s=%s", flogging.Global.Spec(), request.LogModule, request.LogLevel)
97-
flogging.ActivateSpec(spec)
96+
err = flogging.Global.ActivateSpec(spec)
97+
if err != nil {
98+
return nil, err
99+
}
98100

99101
logResponse := &pb.LogLevelResponse{LogModule: request.LogModule, LogLevel: strings.ToUpper(request.LogLevel)}
100102
return logResponse, err
@@ -107,3 +109,12 @@ func (s *ServerAdmin) RevertLogLevels(ctx context.Context, env *common.Envelope)
107109
flogging.ActivateSpec(s.specAtStartup)
108110
return &empty.Empty{}, nil
109111
}
112+
113+
func (s *ServerAdmin) GetLogSpec(ctx context.Context, env *common.Envelope) (*pb.LogSpecResponse, error) {
114+
if _, err := s.v.validate(ctx, env); err != nil {
115+
return nil, err
116+
}
117+
logSpec := flogging.Global.Spec()
118+
logResponse := &pb.LogSpecResponse{LogSpec: logSpec}
119+
return logResponse, nil
120+
}

core/admin/admin_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func TestForbidden(t *testing.T) {
5858
adminServer := NewAdminServer(nil)
5959
adminServer.v = &mockValidator{}
6060
mv := adminServer.v.(*mockValidator)
61-
mv.On("validate").Return(nil, accessDenied).Times(5)
61+
mv.On("validate").Return(nil, accessDenied).Times(6)
6262

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

79+
_, err = adminServer.GetLogSpec(ctx, nil)
80+
assert.Equal(t, accessDenied, err)
81+
7982
_, err = adminServer.StartServer(ctx, nil)
8083
assert.Equal(t, accessDenied, err)
8184
}
@@ -129,4 +132,8 @@ func TestLoggingCalls(t *testing.T) {
129132
assert.NotNil(t, logResponse, "logResponse should have been set")
130133
assert.Equal(t, flogging.DefaultLevel(), logResponse.LogLevel, "logger level should have been the default")
131134
assert.Nil(t, err, "Error should have been nil")
135+
136+
mv.On("validate").Return(nil, nil).Once()
137+
_, err = adminServer.GetLogSpec(context.Background(), nil)
138+
assert.Nil(t, err, "Error should have been nil")
132139
}

peer/clilogging/common.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func InitCmdFactory() (*LoggingCmdFactory, error) {
5757

5858
func checkLoggingCmdParams(cmd *cobra.Command, args []string) error {
5959
var err error
60-
if cmd.Name() == "revertlevels" {
60+
if cmd.Name() == "revertlevels" || cmd.Name() == "getlogspec" {
6161
if len(args) > 0 {
6262
err = errors.Errorf("more parameters than necessary were provided. Expected 0, received %d", len(args))
6363
return err

peer/clilogging/getlogspec.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
package clilogging
8+
9+
import (
10+
"context"
11+
"fmt"
12+
13+
pb "github.com/hyperledger/fabric/protos/peer"
14+
"github.com/spf13/cobra"
15+
)
16+
17+
func getLogSpecCmd(cf *LoggingCmdFactory) *cobra.Command {
18+
var loggingGetLogSpecCmd = &cobra.Command{
19+
Use: "getlogspec",
20+
Short: "Returns the active log spec.",
21+
Long: `Returns the active logging specification of the peer.`,
22+
RunE: func(cmd *cobra.Command, args []string) error {
23+
return getLogSpec(cf, cmd, args)
24+
},
25+
}
26+
27+
return loggingGetLogSpecCmd
28+
}
29+
30+
func getLogSpec(cf *LoggingCmdFactory, cmd *cobra.Command, args []string) (err error) {
31+
err = checkLoggingCmdParams(cmd, args)
32+
if err == nil {
33+
// Parsing of the command line is done so silence cmd usage
34+
cmd.SilenceUsage = true
35+
36+
if cf == nil {
37+
cf, err = InitCmdFactory()
38+
if err != nil {
39+
return err
40+
}
41+
}
42+
env := cf.wrapWithEnvelope(&pb.AdminOperation{})
43+
logResponse, err := cf.AdminClient.GetLogSpec(context.Background(), env)
44+
if err != nil {
45+
return err
46+
}
47+
fmt.Println("Current log spec:")
48+
fmt.Println(logResponse.LogSpec)
49+
}
50+
return err
51+
}

peer/clilogging/logging.go

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,7 @@
11
/*
2-
Copyright IBM Corp. 2016 All Rights Reserved.
2+
Copyright IBM Corp. All Rights Reserved.
33
4-
Licensed under the Apache License, Version 2.0 (the "License");
5-
you may not use this file except in compliance with the License.
6-
You may obtain a copy of the License at
7-
8-
http://www.apache.org/licenses/LICENSE-2.0
9-
10-
Unless required by applicable law or agreed to in writing, software
11-
distributed under the License is distributed on an "AS IS" BASIS,
12-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
See the License for the specific language governing permissions and
14-
limitations under the License.
4+
SPDX-License-Identifier: Apache-2.0
155
*/
166

177
package clilogging
@@ -36,6 +26,7 @@ func Cmd(cf *LoggingCmdFactory) *cobra.Command {
3626
loggingCmd.AddCommand(getLevelCmd(cf))
3727
loggingCmd.AddCommand(setLevelCmd(cf))
3828
loggingCmd.AddCommand(revertLevelsCmd(cf))
29+
loggingCmd.AddCommand(getLogSpecCmd(cf))
3930

4031
return loggingCmd
4132
}

peer/clilogging/logging_test.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,7 @@
11
/*
2-
Copyright Digital Asset Holdings, LLC 2016 All Rights Reserved.
2+
Copyright IBM Corp. All Rights Reserved.
33
4-
Licensed under the Apache License, Version 2.0 (the "License");
5-
you may not use this file except in compliance with the License.
6-
You may obtain a copy of the License at
7-
8-
http://www.apache.org/licenses/LICENSE-2.0
9-
10-
Unless required by applicable law or agreed to in writing, software
11-
distributed under the License is distributed on an "AS IS" BASIS,
12-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
See the License for the specific language governing permissions and
14-
limitations under the License.
4+
SPDX-License-Identifier: Apache-2.0
155
*/
166

177
package clilogging
@@ -54,6 +44,8 @@ func initLoggingTest(command string) (*cobra.Command, *LoggingCmdFactory) {
5444
cmd = setLevelCmd(mockCF)
5545
} else if command == "revertlevels" {
5646
cmd = revertLevelsCmd(mockCF)
47+
} else if command == "getlogspec" {
48+
cmd = getLogSpecCmd(mockCF)
5749
} else {
5850
// should only happen when there's a typo in a test case below
5951
}
@@ -108,3 +100,13 @@ func TestRevertLevels(t *testing.T) {
108100
)
109101
runTests(t, "revertlevels", tc)
110102
}
103+
104+
// TestGetLogSpec tests getlogspec with various parameters
105+
func TestGetLogSpec(t *testing.T) {
106+
var tc []testCase
107+
tc = append(tc,
108+
testCase{"Valid", []string{}, false},
109+
testCase{"ExtraParameter", []string{"peer"}, true},
110+
)
111+
runTests(t, "getlogspec", tc)
112+
}

peer/common/mockclient.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,8 @@ func (m *mockAdminClient) SetModuleLogLevel(ctx context.Context, env *cb.Envelop
103103
func (m *mockAdminClient) RevertLogLevels(ctx context.Context, in *cb.Envelope, opts ...grpc.CallOption) (*empty.Empty, error) {
104104
return &empty.Empty{}, m.err
105105
}
106+
107+
func (m *mockAdminClient) GetLogSpec(ctx context.Context, in *cb.Envelope, opts ...grpc.CallOption) (*pb.LogSpecResponse, error) {
108+
response := &pb.LogSpecResponse{LogSpec: "info"}
109+
return response, m.err
110+
}

0 commit comments

Comments
 (0)