code.gitea.io/gitea@v1.19.3/modules/log/flags.go (about)

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