github.com/darrenli6/fabric-sdk-example@v0.0.0-20220109053535-94b13b56df8c/docs/source/logging-control.rst (about)

     1  Logging Control
     2  ===============
     3  
     4  Overview
     5  --------
     6  
     7  Logging in the ``peer`` application and in the ``shim`` interface to
     8  chaincodes is programmed using facilities provided by the
     9  ``github.com/op/go-logging`` package. This package supports
    10  
    11  -  Logging control based on the severity of the message
    12  -  Logging control based on the software *module* generating the message
    13  -  Different pretty-printing options based on the severity of the
    14     message
    15  
    16  All logs are currently directed to ``stderr``, and the pretty-printing
    17  is currently fixed. However global and module-level control of logging
    18  by severity is provided for both users and developers. There are
    19  currently no formalized rules for the types of information provided at
    20  each severity level, however when submitting bug reports the developers
    21  may want to see full logs down to the DEBUG level.
    22  
    23  In pretty-printed logs the logging level is indicated both by color and
    24  by a 4-character code, e.g, "ERRO" for ERROR, "DEBU" for DEBUG, etc. In
    25  the logging context a *module* is an arbitrary name (string) given by
    26  developers to groups of related messages. In the pretty-printed example
    27  below, the logging modules "peer", "rest" and "main" are generating
    28  logs.
    29  
    30  ::
    31  
    32      16:47:09.634 [peer] GetLocalAddress -> INFO 033 Auto detected peer address: 9.3.158.178:7051
    33      16:47:09.635 [rest] StartOpenchainRESTServer -> INFO 035 Initializing the REST service...
    34      16:47:09.635 [main] serve -> INFO 036 Starting peer with id=name:"vp1" , network id=dev, address=9.3.158.178:7051, discovery.rootnode=, validator=true
    35  
    36  An arbitrary number of logging modules can be created at runtime,
    37  therefore there is no "master list" of modules, and logging control
    38  constructs can not check whether logging modules actually do or will
    39  exist. Also note that the logging module system does not understand
    40  hierarchy or wildcarding: You may see module names like "foo/bar" in the
    41  code, but the logging system only sees a flat string. It doesn't
    42  understand that "foo/bar" is related to "foo" in any way, or that
    43  "foo/\*" might indicate all "submodules" of foo.
    44  
    45  peer
    46  ----
    47  
    48  The logging level of the ``peer`` command can be controlled from the
    49  command line for each invocation using the ``--logging-level`` flag, for
    50  example
    51  
    52  ::
    53  
    54      peer node start --logging-level=debug
    55  
    56  The default logging level for each individual ``peer`` subcommand can
    57  also be set in the
    58  `core.yaml <https://github.com/hyperledger/fabric/blob/master/sampleconfig/core.yaml>`__
    59  file. For example the key ``logging.node`` sets the default level for
    60  the ``node`` subcommmand. Comments in the file also explain how the
    61  logging level can be overridden in various ways by using environment
    62  varaibles.
    63  
    64  Logging severity levels are specified using case-insensitive strings
    65  chosen from
    66  
    67  ::
    68  
    69      CRITICAL | ERROR | WARNING | NOTICE | INFO | DEBUG
    70  
    71  The full logging level specification for the ``peer`` is of the form
    72  
    73  ::
    74  
    75      [<module>[,<module>...]=]<level>[:[<module>[,<module>...]=]<level>...]
    76  
    77  A logging level by itself is taken as the overall default. Otherwise,
    78  overrides for individual or groups of modules can be specified using the
    79  
    80  ::
    81  
    82      <module>[,<module>...]=<level>
    83  
    84  syntax. Examples of specifications (valid for all of
    85  ``--logging-level``, environment variable and
    86  `core.yaml <https://github.com/hyperledger/fabric/blob/master/sampleconfig/core.yaml>`__
    87  settings):
    88  
    89  ::
    90  
    91      info                                       - Set default to INFO
    92      warning:main,db=debug:chaincode=info       - Default WARNING; Override for main,db,chaincode
    93      chaincode=info:main=debug:db=debug:warning - Same as above
    94  
    95  Go chaincodes
    96  -------------
    97  
    98  The standard mechanism to log within a chaincode application is to
    99  integrate with the logging transport exposed to each chaincode instance
   100  via the peer. The chaincode ``shim`` package provides APIs that allow a
   101  chaincode to create and manage logging objects whose logs will be
   102  formatted and interleaved consistently with the ``shim`` logs.
   103  
   104  As independently executed programs, user-provided chaincodes may
   105  technically also produce output on stdout/stderr. While naturally useful
   106  for "devmode", these channels are normally disabled on a production
   107  network to mitigate abuse from broken or malicious code. However, it is
   108  possible to enable this output even for peer-managed containers (e.g.
   109  "netmode") on a per-peer basis via the
   110  CORE\_VM\_DOCKER\_ATTACHSTDOUT=true configuration option.
   111  
   112  Once enabled, each chaincode will receive its own logging channel keyed
   113  by its container-id. Any output written to either stdout or stderr will
   114  be integrated with the peer's log on a per-line basis. It is not
   115  recommended to enable this for production.
   116  
   117  API
   118  ~~~
   119  
   120  ``NewLogger(name string) *ChaincodeLogger`` - Create a logging object
   121  for use by a chaincode
   122  
   123  ``(c *ChaincodeLogger) SetLevel(level LoggingLevel)`` - Set the logging
   124  level of the logger
   125  
   126  ``(c *ChaincodeLogger) IsEnabledFor(level LoggingLevel) bool`` - Return
   127  true if logs will be generated at the given level
   128  
   129  ``LogLevel(levelString string) (LoggingLevel, error)`` - Convert a
   130  string to a ``LoggingLevel``
   131  
   132  A ``LoggingLevel`` is a member of the enumeration
   133  
   134  ::
   135  
   136      LogDebug, LogInfo, LogNotice, LogWarning, LogError, LogCritical
   137  
   138  which can be used directly, or generated by passing a case-insensitive
   139  version of the strings
   140  
   141  ::
   142  
   143      DEBUG, INFO, NOTICE, WARNING, ERROR, CRITICAL
   144  
   145  to the ``LogLevel`` API.
   146  
   147  Formatted logging at various severity levels is provided by the
   148  functions
   149  
   150  ::
   151  
   152      (c *ChaincodeLogger) Debug(args ...interface{})
   153      (c *ChaincodeLogger) Info(args ...interface{})
   154      (c *ChaincodeLogger) Notice(args ...interface{})
   155      (c *ChaincodeLogger) Warning(args ...interface{})
   156      (c *ChaincodeLogger) Error(args ...interface{})
   157      (c *ChaincodeLogger) Critical(args ...interface{})
   158  
   159      (c *ChaincodeLogger) Debugf(format string, args ...interface{})
   160      (c *ChaincodeLogger) Infof(format string, args ...interface{})
   161      (c *ChaincodeLogger) Noticef(format string, args ...interface{})
   162      (c *ChaincodeLogger) Warningf(format string, args ...interface{})
   163      (c *ChaincodeLogger) Errorf(format string, args ...interface{})
   164      (c *ChaincodeLogger) Criticalf(format string, args ...interface{})
   165  
   166  The ``f`` forms of the logging APIs provide for precise control over the
   167  formatting of the logs. The non-\ ``f`` forms of the APIs currently
   168  insert a space between the printed representations of the arguments, and
   169  arbitrarily choose the formats to use.
   170  
   171  In the current implementation, the logs produced by the ``shim`` and a
   172  ``ChaincodeLogger`` are timestamped, marked with the logger *name* and
   173  severity level, and written to ``stderr``. Note that logging level
   174  control is currently based on the *name* provided when the
   175  ``ChaincodeLogger`` is created. To avoid ambiguities, all
   176  ``ChaincodeLogger`` should be given unique names other than "shim". The
   177  logger *name* will appear in all log messages created by the logger. The
   178  ``shim`` logs as "shim".
   179  
   180  Go language chaincodes can also control the logging level of the
   181  chaincode ``shim`` interface through the ``SetLoggingLevel`` API.
   182  
   183  ``SetLoggingLevel(LoggingLevel level)`` - Control the logging level of
   184  the shim
   185  
   186  The default logging level for the shim is ``LogDebug``.
   187  
   188  Below is a simple example of how a chaincode might create a private
   189  logging object logging at the ``LogInfo`` level, and also control the
   190  amount of logging provided by the ``shim`` based on an environment
   191  variable.
   192  
   193  ::
   194  
   195      var logger = shim.NewLogger("myChaincode")
   196  
   197      func main() {
   198  
   199          logger.SetLevel(shim.LogInfo)
   200  
   201          logLevel, _ := shim.LogLevel(os.Getenv("SHIM_LOGGING_LEVEL"))
   202          shim.SetLoggingLevel(logLevel)
   203          ...
   204      }
   205  
   206  .. Licensed under Creative Commons Attribution 4.0 International License
   207     https://creativecommons.org/licenses/by/4.0/
   208