github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/log/slog/handler.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/context"
     9  )
    10  
    11  type commonHandler struct{}
    12  
    13  // A Handler handles log records produced by a Logger.
    14  //
    15  // 典型的なハンドラは、ログレコードを標準エラーに出力したり、
    16  // ファイルやデータベースに書き込んだり、
    17  // または追加の属性を追加して別のハンドラに渡すことができます。
    18  //
    19  // Handlerのメソッドのいずれかは、自身または他のメソッドと同時に呼び出される可能性があります。
    20  // Handlerは、この並行性を管理する責任があります。
    21  //
    22  // slogパッケージのユーザーは、Handlerメソッドを直接呼び出すべきではありません。
    23  // 代わりに、[Logger]のメソッドを使用する必要があります。
    24  type Handler interface {
    25  	Enabled(context.Context, Level) bool
    26  
    27  	Handle(context.Context, Record) error
    28  
    29  	WithAttrs(attrs []Attr) Handler
    30  
    31  	WithGroup(name string) Handler
    32  }
    33  
    34  // HandlerOptionsは、[TextHandler] または [JSONHandler] のオプションです。
    35  // ゼロ値のHandlerOptionsは、完全にデフォルト値で構成されています。
    36  type HandlerOptions struct {
    37  	// AddSource causes the handler to compute the source code position
    38  	// of the log statement and add a SourceKey attribute to the output.
    39  	AddSource bool
    40  
    41  	// Level reports the minimum record level that will be logged.
    42  	// The handler discards records with lower levels.
    43  	// If Level is nil, the handler assumes LevelInfo.
    44  	// The handler calls Level.Level for each record processed;
    45  	// to adjust the minimum level dynamically, use a LevelVar.
    46  	Level Leveler
    47  
    48  	// ReplaceAttr is called to rewrite each non-group attribute before it is logged.
    49  	// The attribute's value has been resolved (see [Value.Resolve]).
    50  	// If ReplaceAttr returns a zero Attr, the attribute is discarded.
    51  	//
    52  	// The built-in attributes with keys "time", "level", "source", and "msg"
    53  	// are passed to this function, except that time is omitted
    54  	// if zero, and source is omitted if AddSource is false.
    55  	//
    56  	// The first argument is a list of currently open groups that contain the
    57  	// Attr. It must not be retained or modified. ReplaceAttr is never called
    58  	// for Group attributes, only their contents. For example, the attribute
    59  	// list
    60  	//
    61  	//     Int("a", 1), Group("g", Int("b", 2)), Int("c", 3)
    62  	//
    63  	// results in consecutive calls to ReplaceAttr with the following arguments:
    64  	//
    65  	//     nil, Int("a", 1)
    66  	//     []string{"g"}, Int("b", 2)
    67  	//     nil, Int("c", 3)
    68  	//
    69  	// ReplaceAttr can be used to change the default keys of the built-in
    70  	// attributes, convert types (for example, to replace a `time.Time` with the
    71  	// integer seconds since the Unix epoch), sanitize personal information, or
    72  	// remove attributes from the output.
    73  	ReplaceAttr func(groups []string, a Attr) Attr
    74  }
    75  
    76  // "built-in"属性のキー。
    77  const (
    78  	// TimeKeyは、ログメソッドが呼び出されたときの時間を表すために、
    79  	// 組み込みハンドラによって使用されるキーです。
    80  	// 関連する値は[time.Time]です。
    81  	TimeKey = "time"
    82  	// LevelKeyは、ログ呼び出しのレベルを表すために、
    83  	// 組み込みハンドラによって使用されるキーです。
    84  	// 関連する値は[Level]です。
    85  	LevelKey = "level"
    86  	// MessageKeyは、ログ呼び出しのメッセージを表すために、
    87  	// 組み込みハンドラによって使用されるキーです。
    88  	// 関連する値は文字列です。
    89  	MessageKey = "msg"
    90  	// SourceKey は、ログ呼び出しのソースファイルと行のためにビルトインハンドラによって使用されるキーです。
    91  	// 関連する値は *[Source] です。
    92  	SourceKey = "source"
    93  )