code.vegaprotocol.io/vega@v0.79.0/logging/fields.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package logging
    17  
    18  import (
    19  	"encoding/hex"
    20  	"fmt"
    21  	"time"
    22  
    23  	ptypes "code.vegaprotocol.io/vega/protos/vega"
    24  	commandspb "code.vegaprotocol.io/vega/protos/vega/commands/v1"
    25  
    26  	"github.com/ethereum/go-ethereum/common"
    27  	"go.uber.org/zap"
    28  )
    29  
    30  func Hash(h []byte) zap.Field {
    31  	hs := hex.EncodeToString(h)
    32  	return zap.String("hash", hs)
    33  }
    34  
    35  // Binary constructs a field that carries an opaque binary blob.
    36  //
    37  // Binary data is serialized in an encoding-appropriate format. For example,
    38  // zap's JSON encoder base64-encodes binary blobs. To log UTF-8 encoded text,
    39  // use ByteString.
    40  func Binary(key string, val []byte) zap.Field {
    41  	return zap.Binary(key, val)
    42  }
    43  
    44  // Bool constructs a field that carries a bool.
    45  func Bool(key string, val bool) zap.Field {
    46  	return zap.Bool(key, val)
    47  }
    48  
    49  // ByteString constructs a field that carries UTF-8 encoded text as a []byte.
    50  // To log opaque binary blobs (which aren't necessarily valid UTF-8), use
    51  // Binary.
    52  func ByteString(key string, val []byte) zap.Field {
    53  	return zap.ByteString(key, val)
    54  }
    55  
    56  // Complex128 constructs a field that carries a complex number. Unlike most
    57  // numeric fields, this costs an allocation (to convert the complex128 to
    58  // interface{}).
    59  func Complex128(key string, val complex128) zap.Field {
    60  	return zap.Complex128(key, val)
    61  }
    62  
    63  // Complex64 constructs a field that carries a complex number. Unlike most
    64  // numeric fields, this costs an allocation (to convert the complex64 to
    65  // interface{}).
    66  func Complex64(key string, val complex64) zap.Field {
    67  	return zap.Complex64(key, val)
    68  }
    69  
    70  // Float64 constructs a field that carries a float64. The way the
    71  // floating-point value is represented is encoder-dependent, so marshaling is
    72  // necessarily lazy.
    73  func Float64(key string, val float64) zap.Field {
    74  	return zap.Float64(key, val)
    75  }
    76  
    77  // Float32 constructs a field that carries a float32. The way the
    78  // floating-point value is represented is encoder-dependent, so marshaling is
    79  // necessarily lazy.
    80  func Float32(key string, val float32) zap.Field {
    81  	return zap.Float32(key, val)
    82  }
    83  
    84  // Int constructs a field with the given key and value.
    85  func Int(key string, val int) zap.Field {
    86  	return Int64(key, int64(val))
    87  }
    88  
    89  // Int64 constructs a field with the given key and value.
    90  func Int64(key string, val int64) zap.Field {
    91  	return zap.Int64(key, val)
    92  }
    93  
    94  // Int32 constructs a field with the given key and value.
    95  func Int32(key string, val int32) zap.Field {
    96  	return zap.Int32(key, val)
    97  }
    98  
    99  // Int16 constructs a field with the given key and value.
   100  func Int16(key string, val int16) zap.Field {
   101  	return zap.Int16(key, val)
   102  }
   103  
   104  // Int8 constructs a field with the given key and value.
   105  func Int8(key string, val int8) zap.Field {
   106  	return zap.Int8(key, val)
   107  }
   108  
   109  // String constructs a field with the given key and value.
   110  func String(key string, val string) zap.Field {
   111  	return zap.String(key, val)
   112  }
   113  
   114  // Strings constructs a field with the given key and value.
   115  func Strings(key string, val []string) zap.Field {
   116  	return zap.Strings(key, val)
   117  }
   118  
   119  // Decimal constructs a field with the given key and value.
   120  func Decimal(key string, val fmt.Stringer) zap.Field {
   121  	return String(key, val.String())
   122  }
   123  
   124  // BigUint constructs a field with the given key and value.
   125  func BigUint(key string, val fmt.Stringer) zap.Field {
   126  	return String(key, val.String())
   127  }
   128  
   129  // BigInt constructs a field with the given key and value.
   130  func BigInt(key string, val fmt.Stringer) zap.Field {
   131  	return String(key, val.String())
   132  }
   133  
   134  func EthereumAddresses(addresses []common.Address) zap.Field {
   135  	strings := []string{}
   136  	for _, v := range addresses {
   137  		strings = append(strings, v.String())
   138  	}
   139  
   140  	return Strings("addresses", strings)
   141  }
   142  
   143  // Uint constructs a field with the given key and value.
   144  func Uint(key string, val uint) zap.Field {
   145  	return Uint64(key, uint64(val))
   146  }
   147  
   148  // Uint64 constructs a field with the given key and value.
   149  func Uint64(key string, val uint64) zap.Field {
   150  	return zap.Uint64(key, val)
   151  }
   152  
   153  // Uint32 constructs a field with the given key and value.
   154  func Uint32(key string, val uint32) zap.Field {
   155  	return zap.Uint32(key, val)
   156  }
   157  
   158  // Uint16 constructs a field with the given key and value.
   159  func Uint16(key string, val uint16) zap.Field {
   160  	return zap.Uint16(key, val)
   161  }
   162  
   163  // Uint8 constructs a field with the given key and value.
   164  func Uint8(key string, val uint8) zap.Field {
   165  	return zap.Uint8(key, val)
   166  }
   167  
   168  // Uintptr constructs a field with the given key and value.
   169  func Uintptr(key string, val uintptr) zap.Field {
   170  	return zap.Uintptr(key, val)
   171  }
   172  
   173  func Duration(key string, value time.Duration) zap.Field {
   174  	return zap.String(key, value.String())
   175  }
   176  
   177  // Error constructs a field with the given error value.
   178  func Error(val error) zap.Field {
   179  	return zap.Error(val)
   180  }
   181  
   182  // Candle constructs a field with the given VEGA candle proto value.
   183  func Candle(c fmt.Stringer) zap.Field {
   184  	return zap.String("candle", c.String())
   185  }
   186  
   187  // CandleWithTag constructs a field with the given VEGA candle proto value and key equal to the tag string.
   188  func CandleWithTag(c fmt.Stringer, tag string) zap.Field {
   189  	return zap.String(tag, c.String())
   190  }
   191  
   192  // Order constructs a field with the given VEGA order value.
   193  func Order(o fmt.Stringer) zap.Field {
   194  	return zap.String("order", o.String())
   195  }
   196  
   197  // ProtoOrder constructs a field with the given VEGA order proto value.
   198  func ProtoOrder(o ptypes.Order) zap.Field {
   199  	return zap.String("order", o.String())
   200  }
   201  
   202  func OrderID(id string) zap.Field {
   203  	return zap.String("order-id", id)
   204  }
   205  
   206  // Time display a time.
   207  func Time(key string, t time.Time) zap.Field {
   208  	return zap.Time(key, t)
   209  }
   210  
   211  // OrderWithTag constructs a field with the given VEGA order proto value and key equal to the tag string.
   212  func OrderWithTag(o fmt.Stringer, tag string) zap.Field {
   213  	return zap.String(tag, o.String())
   214  }
   215  
   216  // Trade constructs a field with the given VEGA trade proto value.
   217  func Trade(t fmt.Stringer) zap.Field {
   218  	return zap.String("trade", t.String())
   219  }
   220  
   221  // Market constructs a field with the given VEGA market proto value.
   222  func Market(m fmt.Stringer) zap.Field {
   223  	return zap.String("market", m.String())
   224  }
   225  
   226  func MarketID(id string) zap.Field {
   227  	return zap.String("market-id", id)
   228  }
   229  
   230  func AssetID(id string) zap.Field {
   231  	return zap.String("asset-id", id)
   232  }
   233  
   234  func WithdrawalID(id string) zap.Field {
   235  	return zap.String("withdrawal-id", id)
   236  }
   237  
   238  func LiquidityID(id string) zap.Field {
   239  	return zap.String("liquidity-id", id)
   240  }
   241  
   242  func LiquidityProvisionSubmissionProto(
   243  	lp *commandspb.LiquidityProvisionSubmission,
   244  ) zap.Field {
   245  	return zap.String("liquidity-provision-submission", lp.String())
   246  }
   247  
   248  func LiquidityProvisionSubmission(
   249  	lp fmt.Stringer,
   250  ) zap.Field {
   251  	return zap.String("liquidity-provision-submission", lp.String())
   252  }
   253  
   254  func LiquidityProvisionCancellationProto(
   255  	lp *commandspb.LiquidityProvisionCancellation,
   256  ) zap.Field {
   257  	return zap.String("liquidity-provision-cancellation", lp.String())
   258  }
   259  
   260  func LiquidityProvisionCancellation(
   261  	lp fmt.Stringer,
   262  ) zap.Field {
   263  	return zap.String("liquidity-provision-cancellation", lp.String())
   264  }
   265  
   266  func LiquidityProvisionAmendmentProto(
   267  	lp *commandspb.LiquidityProvisionAmendment,
   268  ) zap.Field {
   269  	return zap.String("liquidity-provision-amendment", lp.String())
   270  }
   271  
   272  func LiquidityProvisionAmendment(
   273  	lp fmt.Stringer,
   274  ) zap.Field {
   275  	return zap.String("liquidity-provision-amendment", lp.String())
   276  }
   277  
   278  func WithdrawSubmissionProto(
   279  	lp *commandspb.WithdrawSubmission,
   280  ) zap.Field {
   281  	return zap.String("withdraw-submission", lp.String())
   282  }
   283  
   284  func WithdrawSubmission(
   285  	lp fmt.Stringer,
   286  ) zap.Field {
   287  	return zap.String("withdraw-submission", lp.String())
   288  }
   289  
   290  // Party constructs a field with the given VEGA party proto value.
   291  func Party(p fmt.Stringer) zap.Field {
   292  	return zap.String("party", p.String())
   293  }
   294  
   295  func PartyID(id string) zap.Field {
   296  	return zap.String("party", id)
   297  }
   298  
   299  func ProposalBatchID(id string) zap.Field {
   300  	return zap.String("proposal-batch-id", id)
   301  }
   302  
   303  func ProposalID(id string) zap.Field {
   304  	return zap.String("proposal-id", id)
   305  }
   306  
   307  // Account constructs a field with the given VEGA account proto value.
   308  func Account(a fmt.Stringer) zap.Field {
   309  	return zap.String("account", a.String())
   310  }
   311  
   312  // ProtoAccount constructs a field with the given VEGA account proto value.
   313  func ProtoAccount(a ptypes.Account) zap.Field {
   314  	return zap.String("account", a.String())
   315  }
   316  
   317  // OrderAmendmentProto constructs a single string field to contain all the object information.
   318  func OrderAmendmentProto(oa *commandspb.OrderAmendment) zap.Field {
   319  	return zap.String("order-amendment", oa.String())
   320  }
   321  
   322  // OrderAmendment constructs a single string field to contain all the object information.
   323  func OrderAmendment(oa fmt.Stringer) zap.Field {
   324  	return zap.String("order-amendment", oa.String())
   325  }
   326  
   327  // OrderSubmissionProto constructs a single string field to contain all the object information.
   328  func OrderSubmissionProto(os *commandspb.OrderSubmission) zap.Field {
   329  	return zap.String("order-submission", os.String())
   330  }
   331  
   332  // OrderSubmission constructs a single string field to contain all the object information.
   333  func OrderSubmission(os fmt.Stringer) zap.Field {
   334  	return zap.String("order-submission", os.String())
   335  }
   336  
   337  // StopOrderSubmission constructs a single string field to contain all the object information.
   338  func StopOrderSubmission(os fmt.Stringer) zap.Field {
   339  	return zap.String("stop-order-submission", os.String())
   340  }
   341  
   342  // StopOrderCanmcellation constructs a single string field to contain all the object information.
   343  func StopOrderCancellation(os fmt.Stringer) zap.Field {
   344  	return zap.String("stop-order-cancellation", os.String())
   345  }
   346  
   347  func OrderCancellation(oc fmt.Stringer) zap.Field {
   348  	return zap.String("order-cancellation", oc.String())
   349  }
   350  
   351  // Reflect constructs a field by running reflection over all the
   352  // field of value passed as a parameter.
   353  // This should never be used we basic log level,
   354  // only in the case of Debug log level, and with guards on  top
   355  // of the actual log call for this level.
   356  func Reflect(key string, val interface{}) zap.Field {
   357  	return zap.Reflect(key, val)
   358  }
   359  
   360  type Tracer interface {
   361  	TraceID() string
   362  }
   363  
   364  // TraceID logs the event traceID.
   365  func TraceID(e Tracer) zap.Field {
   366  	return zap.String("trace-id", e.TraceID())
   367  }