github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/log/flags.go (about)

     1  // Copyright 2023 The GitBundle Inc. All rights reserved.
     2  // Copyright 2017 The Gitea Authors. All rights reserved.
     3  // Use of this source code is governed by a MIT-style
     4  // license that can be found in the LICENSE file.
     5  
     6  package log
     7  
     8  import "strings"
     9  
    10  // These flags define which text to prefix to each log entry generated
    11  // by the Logger. Bits are or'ed together to control what's printed.
    12  // There is no control over the order they appear (the order listed
    13  // here) or the format they present (as described in the comments).
    14  // The prefix is followed by a colon only if more than time is stated
    15  // is specified. For example, flags Ldate | Ltime
    16  // produce, 2009/01/23 01:23:23 message.
    17  // The standard is:
    18  // 2009/01/23 01:23:23 ...a/logger/c/d.go:23:runtime.Caller() [I]: message
    19  const (
    20  	Ldate          = 1 << iota // the date in the local time zone: 2009/01/23
    21  	Ltime                      // the time in the local time zone: 01:23:23
    22  	Lmicroseconds              // microsecond resolution: 01:23:23.123123.  assumes Ltime.
    23  	Llongfile                  // full file name and line number: /a/logger/c/d.go:23
    24  	Lshortfile                 // final file name element and line number: d.go:23. overrides Llongfile
    25  	Lfuncname                  // function name of the caller: runtime.Caller()
    26  	Lshortfuncname             // last part of the function name
    27  	LUTC                       // if Ldate or Ltime is set, use UTC rather than the local time zone
    28  	Llevelinitial              // Initial character of the provided level in brackets eg. [I] for info
    29  	Llevel                     // Provided level in brackets [INFO]
    30  
    31  	// Last 20 characters of the filename
    32  	Lmedfile = Lshortfile | Llongfile
    33  
    34  	// LstdFlags is the initial value for the standard logger
    35  	LstdFlags = Ldate | Ltime | Lmedfile | Lshortfuncname | Llevelinitial
    36  )
    37  
    38  var flagFromString = map[string]int{
    39  	"none":          0,
    40  	"date":          Ldate,
    41  	"time":          Ltime,
    42  	"microseconds":  Lmicroseconds,
    43  	"longfile":      Llongfile,
    44  	"shortfile":     Lshortfile,
    45  	"funcname":      Lfuncname,
    46  	"shortfuncname": Lshortfuncname,
    47  	"utc":           LUTC,
    48  	"levelinitial":  Llevelinitial,
    49  	"level":         Llevel,
    50  	"medfile":       Lmedfile,
    51  	"stdflags":      LstdFlags,
    52  }
    53  
    54  // FlagsFromString takes a comma separated list of flags and returns
    55  // the flags for this string
    56  func FlagsFromString(from string) int {
    57  	flags := 0
    58  	for _, flag := range strings.Split(strings.ToLower(from), ",") {
    59  		f, ok := flagFromString[strings.TrimSpace(flag)]
    60  		if ok {
    61  			flags |= f
    62  		}
    63  	}
    64  	if flags == 0 {
    65  		return -1
    66  	}
    67  	return flags
    68  }