github.com/grailbio/base@v0.0.11/log/golog.go (about)

     1  // Copyright 2018 GRAIL, Inc. All rights reserved.
     2  // Use of this source code is governed by the Apache 2.0
     3  // license that can be found in the LICENSE file.
     4  
     5  package log
     6  
     7  import (
     8  	"flag"
     9  	"fmt"
    10  	"io"
    11  	golog "log"
    12  	"runtime/debug"
    13  	"sync/atomic"
    14  )
    15  
    16  var golevel = Info
    17  
    18  var called int32 = 0
    19  
    20  // AddFlags adds a standard log level flags to the flag.CommandLine
    21  // flag set.
    22  func AddFlags() {
    23  	if atomic.AddInt32(&called, 1) != 1 {
    24  		Error.Printf("log.AddFlags: called twice!")
    25  		debug.PrintStack()
    26  		return
    27  	}
    28  	flag.Var(new(logFlag), "log", "set log level (off, error, info, debug)")
    29  }
    30  
    31  // Logger is an alternative spelling of "log".Logger.
    32  type Logger = golog.Logger
    33  
    34  const (
    35  	Ldate         = golog.Ldate         // the date in the local time zone: 2009/01/23
    36  	Ltime         = golog.Ltime         // the time in the local time zone: 01:23:23
    37  	Lmicroseconds = golog.Lmicroseconds // microsecond resolution: 01:23:23.123123.  assumes Ltime.
    38  	Llongfile     = golog.Llongfile     // full file name and line number: /a/b/c/d.go:23
    39  	Lshortfile    = golog.Lshortfile    // final file name element and line number: d.go:23. overrides Llongfile
    40  	LUTC          = golog.LUTC          // if Ldate or Ltime is set, use UTC rather than the local time zone
    41  	LstdFlags     = Ldate | Ltime       // initial values for the standard logger
    42  )
    43  
    44  // SetFlags sets the output flags for the Go standard logger.
    45  func SetFlags(flag int) {
    46  	golog.SetFlags(flag)
    47  }
    48  
    49  // SetOutput sets the output destination for the Go standard logger.
    50  func SetOutput(w io.Writer) {
    51  	golog.SetOutput(w)
    52  }
    53  
    54  // SetPrefix sets the output prefix for the Go standard logger.
    55  func SetPrefix(prefix string) {
    56  	golog.SetPrefix(prefix)
    57  }
    58  
    59  // SetLevel sets the log level for the Go standard logger.
    60  // It should be called once at the beginning of a program's main.
    61  func SetLevel(level Level) {
    62  	golevel = level
    63  }
    64  
    65  type logFlag string
    66  
    67  func (f logFlag) String() string {
    68  	return string(f)
    69  }
    70  
    71  func (f *logFlag) Set(level string) error {
    72  	var l Level
    73  	switch level {
    74  	case "off":
    75  		l = Off
    76  	case "error":
    77  		l = Error
    78  	case "info":
    79  		l = Info
    80  	case "debug":
    81  		l = Debug
    82  	default:
    83  		return fmt.Errorf("invalid log level %q", level)
    84  	}
    85  	golevel = l
    86  	return nil
    87  }
    88  
    89  // Get implements flag.Getter.
    90  func (logFlag) Get() interface{} {
    91  	return golevel
    92  }
    93  
    94  type gologOutputter struct{}
    95  
    96  func (gologOutputter) Level() Level { return golevel }
    97  
    98  func (gologOutputter) Output(calldepth int, level Level, s string) error {
    99  	if golevel < level {
   100  		return nil
   101  	}
   102  	return golog.Output(calldepth+1, s)
   103  }