github.com/kchristidis/fabric@v1.0.4-0.20171028114726-837acd08cde1/peer/clilogging/setlevel.go (about)

     1  /*
     2  Copyright IBM Corp. 2016 All Rights Reserved.
     3  
     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.
    15  */
    16  
    17  package clilogging
    18  
    19  import (
    20  	"golang.org/x/net/context"
    21  
    22  	pb "github.com/hyperledger/fabric/protos/peer"
    23  
    24  	"github.com/spf13/cobra"
    25  )
    26  
    27  func setLevelCmd(cf *LoggingCmdFactory) *cobra.Command {
    28  	var loggingSetLevelCmd = &cobra.Command{
    29  		Use:   "setlevel <module regular expression> <log level>",
    30  		Short: "Sets the logging level for all modules that match the regular expression.",
    31  		Long:  `Sets the logging level for all modules that match the regular expression.`,
    32  		RunE: func(cmd *cobra.Command, args []string) error {
    33  			return setLevel(cf, cmd, args)
    34  		},
    35  	}
    36  	return loggingSetLevelCmd
    37  }
    38  
    39  func setLevel(cf *LoggingCmdFactory, cmd *cobra.Command, args []string) (err error) {
    40  	err = checkLoggingCmdParams(cmd, args)
    41  	if err == nil {
    42  		if cf == nil {
    43  			cf, err = InitCmdFactory()
    44  			if err != nil {
    45  				return err
    46  			}
    47  		}
    48  		logResponse, err := cf.AdminClient.SetModuleLogLevel(context.Background(), &pb.LogLevelRequest{LogModule: args[0], LogLevel: args[1]})
    49  		if err != nil {
    50  			return err
    51  		}
    52  		logger.Infof("Log level set for peer modules matching regular expression '%s': %s", logResponse.LogModule, logResponse.LogLevel)
    53  	}
    54  	return err
    55  }