github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/log/slog/record.go (about)

     1  // Copyright 2022 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package slog
     6  
     7  import (
     8  	"github.com/shogo82148/std/time"
     9  )
    10  
    11  const nAttrsInline = 4
    12  
    13  // Recordは、ログイベントに関する情報を保持します。
    14  // Recordのコピーは状態を共有します。
    15  // Recordをコピーしてから変更しないでください。
    16  // 新しいRecordを作成するには、 [NewRecord] を呼び出します。
    17  // 共有状態のないコピーを作成するには、 [Record.Clone] を使用します。
    18  type Record struct {
    19  	// The time at which the output method (Log, Info, etc.) was called.
    20  	Time time.Time
    21  
    22  	// The log message.
    23  	Message string
    24  
    25  	// The level of the event.
    26  	Level Level
    27  
    28  	// The program counter at the time the record was constructed, as determined
    29  	// by runtime.Callers. If zero, no program counter is available.
    30  	//
    31  	// The only valid use for this value is as an argument to
    32  	// [runtime.CallersFrames]. In particular, it must not be passed to
    33  	// [runtime.FuncForPC].
    34  	PC uintptr
    35  
    36  	// Allocation optimization: an inline array sized to hold
    37  	// the majority of log calls (based on examination of open-source
    38  	// code). It holds the start of the list of Attrs.
    39  	front [nAttrsInline]Attr
    40  
    41  	// The number of Attrs in front.
    42  	nFront int
    43  
    44  	// The list of Attrs except for those in front.
    45  	// Invariants:
    46  	//   - len(back) > 0 iff nFront == len(front)
    47  	//   - Unused array elements are zero. Used to detect mistakes.
    48  	back []Attr
    49  }
    50  
    51  // NewRecordは、指定された引数から [Record] を作成します。
    52  // Recordに属性を追加するには、 [Record.AddAttrs] を使用します。
    53  //
    54  // NewRecordは、 [Handler] をバックエンドとしてサポートするログAPIに使用することを想定しています。
    55  func NewRecord(t time.Time, level Level, msg string, pc uintptr) Record
    56  
    57  // Cloneは、共有状態のないレコードのコピーを返します。
    58  // オリジナルのレコードとクローンの両方を変更できます。
    59  // 互いに干渉しません。
    60  func (r Record) Clone() Record
    61  
    62  // NumAttrsは、[Record] の属性の数を返します。
    63  func (r Record) NumAttrs() int
    64  
    65  // Attrsは、[Record] 内の各Attrに対してfを呼び出します。
    66  // fがfalseを返すと、反復処理が停止します。
    67  func (r Record) Attrs(f func(Attr) bool)
    68  
    69  // AddAttrsは、指定されたAttrsを [Record] のAttrsリストに追加します。
    70  // 空のグループは省略されます。
    71  func (r *Record) AddAttrs(attrs ...Attr)
    72  
    73  // Addは、[Logger.Log]で説明されているように、argsをAttrsに変換し、
    74  // [Record] のAttrsリストにAttrsを追加します。
    75  // 空のグループは省略されます。
    76  func (r *Record) Add(args ...any)
    77  
    78  // Sourceは、ソースコードの行の場所を記述します。
    79  type Source struct {
    80  	// Function is the package path-qualified function name containing the
    81  	// source line. If non-empty, this string uniquely identifies a single
    82  	// function in the program. This may be the empty string if not known.
    83  	Function string `json:"function"`
    84  	// File and Line are the file name and line number (1-based) of the source
    85  	// line. These may be the empty string and zero, respectively, if not known.
    86  	File string `json:"file"`
    87  	Line int    `json:"line"`
    88  }