github.com/v2fly/tools@v0.100.0/internal/lsp/debug/log/log.go (about)

     1  // Copyright 2020 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 log provides helper methods for exporting log events to the
     6  // internal/event package.
     7  package log
     8  
     9  import (
    10  	"context"
    11  	"fmt"
    12  
    13  	"github.com/v2fly/tools/internal/event"
    14  	"github.com/v2fly/tools/internal/event/label"
    15  	"github.com/v2fly/tools/internal/lsp/debug/tag"
    16  )
    17  
    18  // Level parameterizes log severity.
    19  type Level int
    20  
    21  const (
    22  	_ Level = iota
    23  	Error
    24  	Warning
    25  	Info
    26  	Debug
    27  	Trace
    28  )
    29  
    30  // Log exports a log event labeled with level l.
    31  func (l Level) Log(ctx context.Context, msg string) {
    32  	event.Log(ctx, msg, tag.Level.Of(int(l)))
    33  }
    34  
    35  // Logf formats and exports a log event labeled with level l.
    36  func (l Level) Logf(ctx context.Context, format string, args ...interface{}) {
    37  	l.Log(ctx, fmt.Sprintf(format, args...))
    38  }
    39  
    40  // LabeledLevel extracts the labeled log l
    41  func LabeledLevel(lm label.Map) Level {
    42  	return Level(tag.Level.Get(lm))
    43  }