logger

package module
v0.0.0-...-8bdecff Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 1, 2024 License: Apache-2.0 Imports: 7 Imported by: 0

README

#google/logger 增加了提示颜色 未做其它更改

logger

Logger is a simple cross platform Go logging library for Windows, Linux, FreeBSD, and macOS, it can log to the Windows event log, Linux/macOS syslog, and an io.Writer.

This is not an official Google product.

Usage

Set up the default logger to log the system log (event log or syslog) and a file, include a flag to turn up verbosity:

import (
  "flag"
  "os"

  "github.com/google/logger"
)

const logPath = "/some/location/example.log"

var verbose = flag.Bool("verbose", false, "print info level logs to stdout")

func main() {
  flag.Parse()

  lf, err := os.OpenFile(logPath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0660)
  if err != nil {
    logger.Fatalf("Failed to open log file: %v", err)
  }
  defer lf.Close()

  defer logger.Init("LoggerExample", *verbose, true, lf).Close()

  logger.Info("I'm about to do something!")
  if err := doSomething(); err != nil {
    logger.Errorf("Error running doSomething: %v", err)
  }
}

The Init function returns a logger so you can setup multiple instances if you wish, only the first call to Init will set the default logger:

lf, err := os.OpenFile(logPath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0660)
if err != nil {
  logger.Fatalf("Failed to open log file: %v", err)
}
defer lf.Close()

// Log to system log and a log file, Info logs don't write to stdout.
loggerOne := logger.Init("LoggerExample", false, true, lf)
defer loggerOne.Close()
// Don't to system log or a log file, Info logs write to stdout..
loggerTwo := logger.Init("LoggerExample", true, false, ioutil.Discard)
defer loggerTwo.Close()

loggerOne.Info("This will log to the log file and the system log")
loggerTwo.Info("This will only log to stdout")
logger.Info("This is the same as using loggerOne")

Custom Format

Code Example
logger.SetFlags(log.Ldate) ERROR: 2018/11/11 Error running Foobar: message
logger.SetFlags(log.Ltime) ERROR: 09:42:45 Error running Foobar: message
logger.SetFlags(log.Lmicroseconds) ERROR: 09:42:50.776015 Error running Foobar: message
logger.SetFlags(log.Llongfile) ERROR: /src/main.go:31: Error running Foobar: message
logger.SetFlags(log.Lshortfile) ERROR: main.go:31: Error running Foobar: message
logger.SetFlags(log.LUTC) ERROR: Error running Foobar: message
logger.SetFlags(log.LstdFlags) ERROR: 2018/11/11 09:43:12 Error running Foobar: message
func main() {
    lf, err := os.OpenFile(logPath, …, 0660)
    defer logger.Init("foo", *verbose, true, lf).Close()
    logger.SetFlags(log.LstdFlags)
}

More info: https://golang.org/pkg/log/#pkg-constants

Documentation

Overview

Package logger offers simple cross platform logging for Windows and Linux. Available logging endpoints are event log (Windows), syslog (Linux), and an io.Writer.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Close

func Close()

Close closes the default logger.

func Error

func Error(v ...interface{})

Error uses the default logger and logs with the Error severity. Arguments are handled in the manner of fmt.Print.

func ErrorDepth

func ErrorDepth(depth int, v ...interface{})

ErrorDepth acts as Error but uses depth to determine which call frame to log. ErrorDepth(0, "msg") is the same as Error("msg").

func Errorf

func Errorf(format string, v ...interface{})

Errorf uses the default logger and logs with the Error severity. Arguments are handled in the manner of fmt.Printf.

func Errorln

func Errorln(v ...interface{})

Errorln uses the default logger and logs with the Error severity. Arguments are handled in the manner of fmt.Println.

func Fatal

func Fatal(v ...interface{})

Fatal uses the default logger, logs with the Fatal severity, and ends with os.Exit(1). Arguments are handled in the manner of fmt.Print.

func FatalDepth

func FatalDepth(depth int, v ...interface{})

FatalDepth acts as Fatal but uses depth to determine which call frame to log. FatalDepth(0, "msg") is the same as Fatal("msg").

func Fatalf

func Fatalf(format string, v ...interface{})

Fatalf uses the default logger, logs with the Fatal severity, and ends with os.Exit(1). Arguments are handled in the manner of fmt.Printf.

func Fatalln

func Fatalln(v ...interface{})

Fatalln uses the default logger, logs with the Fatal severity, and ends with os.Exit(1). Arguments are handled in the manner of fmt.Println.

func Info

func Info(v ...interface{})

Info uses the default logger and logs with the Info severity. Arguments are handled in the manner of fmt.Print.

func InfoDepth

func InfoDepth(depth int, v ...interface{})

InfoDepth acts as Info but uses depth to determine which call frame to log. InfoDepth(0, "msg") is the same as Info("msg").

func Infof

func Infof(format string, v ...interface{})

Infof uses the default logger and logs with the Info severity. Arguments are handled in the manner of fmt.Printf.

func Infoln

func Infoln(v ...interface{})

Infoln uses the default logger and logs with the Info severity. Arguments are handled in the manner of fmt.Println.

func SetFlags

func SetFlags(flag int)

SetFlags sets the output flags for the logger.

func SetLevel

func SetLevel(lvl Level)

SetLevel sets the verbosity level for verbose info logging in the default logger.

func Warning

func Warning(v ...interface{})

Warning uses the default logger and logs with the Warning severity. Arguments are handled in the manner of fmt.Print.

func WarningDepth

func WarningDepth(depth int, v ...interface{})

WarningDepth acts as Warning but uses depth to determine which call frame to log. WarningDepth(0, "msg") is the same as Warning("msg").

func Warningf

func Warningf(format string, v ...interface{})

Warningf uses the default logger and logs with the Warning severity. Arguments are handled in the manner of fmt.Printf.

func Warningln

func Warningln(v ...interface{})

Warningln uses the default logger and logs with the Warning severity. Arguments are handled in the manner of fmt.Println.

Types

type Level

type Level int

Level describes the level of verbosity for info messages when using V style logging. See documentation for the V function for more information.

type Logger

type Logger struct {
	// contains filtered or unexported fields
}

A Logger represents an active logging object. Multiple loggers can be used simultaneously even if they are using the same same writers.

func Init

func Init(name string, verbose, systemLog bool, logFile io.Writer) *Logger

Init sets up logging and should be called before log functions, usually in the caller's main(). Default log functions can be called before Init(), but log output will only go to stderr (along with a warning). The first call to Init populates the default logger and returns the generated logger, subsequent calls to Init will only return the generated logger. If the logFile passed in also satisfies io.Closer, logFile.Close will be called when closing the logger.

func (*Logger) Close

func (l *Logger) Close()

Close closes all the underlying log writers, which will flush any cached logs. Any errors from closing the underlying log writers will be printed to stderr. Once Close is called, all future calls to the logger will panic.

func (*Logger) Error

func (l *Logger) Error(v ...interface{})

Error logs with the ERROR severity. Arguments are handled in the manner of fmt.Print.

func (*Logger) ErrorDepth

func (l *Logger) ErrorDepth(depth int, v ...interface{})

ErrorDepth acts as Error but uses depth to determine which call frame to log. ErrorDepth(0, "msg") is the same as Error("msg").

func (*Logger) Errorf

func (l *Logger) Errorf(format string, v ...interface{})

Errorf logs with the Error severity. Arguments are handled in the manner of fmt.Printf.

func (*Logger) Errorln

func (l *Logger) Errorln(v ...interface{})

Errorln logs with the ERROR severity. Arguments are handled in the manner of fmt.Println.

func (*Logger) Fatal

func (l *Logger) Fatal(v ...interface{})

Fatal logs with the Fatal severity, and ends with os.Exit(1). Arguments are handled in the manner of fmt.Print.

func (*Logger) FatalDepth

func (l *Logger) FatalDepth(depth int, v ...interface{})

FatalDepth acts as Fatal but uses depth to determine which call frame to log. FatalDepth(0, "msg") is the same as Fatal("msg").

func (*Logger) Fatalf

func (l *Logger) Fatalf(format string, v ...interface{})

Fatalf logs with the Fatal severity, and ends with os.Exit(1). Arguments are handled in the manner of fmt.Printf.

func (*Logger) Fatalln

func (l *Logger) Fatalln(v ...interface{})

Fatalln logs with the Fatal severity, and ends with os.Exit(1). Arguments are handled in the manner of fmt.Println.

func (*Logger) Info

func (l *Logger) Info(v ...interface{})

Info logs with the Info severity. Arguments are handled in the manner of fmt.Print.

func (*Logger) InfoDepth

func (l *Logger) InfoDepth(depth int, v ...interface{})

InfoDepth acts as Info but uses depth to determine which call frame to log. InfoDepth(0, "msg") is the same as Info("msg").

func (*Logger) Infof

func (l *Logger) Infof(format string, v ...interface{})

Infof logs with the Info severity. Arguments are handled in the manner of fmt.Printf.

func (*Logger) Infoln

func (l *Logger) Infoln(v ...interface{})

Infoln logs with the Info severity. Arguments are handled in the manner of fmt.Println.

func (*Logger) SetLevel

func (l *Logger) SetLevel(lvl Level)

SetLevel sets the logger verbosity level for verbose info logging.

func (*Logger) V

func (l *Logger) V(lvl Level) Verbose

V generates a log record depends on the setting of the Level; or none default. It uses the specified logger.

func (*Logger) Warning

func (l *Logger) Warning(v ...interface{})

Warning logs with the Warning severity. Arguments are handled in the manner of fmt.Print.

func (*Logger) WarningDepth

func (l *Logger) WarningDepth(depth int, v ...interface{})

WarningDepth acts as Warning but uses depth to determine which call frame to log. WarningDepth(0, "msg") is the same as Warning("msg").

func (*Logger) Warningf

func (l *Logger) Warningf(format string, v ...interface{})

Warningf logs with the Warning severity. Arguments are handled in the manner of fmt.Printf.

func (*Logger) Warningln

func (l *Logger) Warningln(v ...interface{})

Warningln logs with the Warning severity. Arguments are handled in the manner of fmt.Println.

type Verbose

type Verbose struct {
	// contains filtered or unexported fields
}

Verbose is type that implements Infof, etc.

func V

func V(lvl Level) Verbose

V generates a log record depends on the setting of the Level or none by default using the default logger.

func (Verbose) Info

func (v Verbose) Info(args ...interface{})

Info is equivalent to the global Info function, guarded by the value of v.

func (Verbose) Infof

func (v Verbose) Infof(format string, args ...interface{})

Infof is equivalent to the global Infof function, guarded by the value of v. See the documentation of V for usage.

func (Verbose) Infoln

func (v Verbose) Infoln(args ...interface{})

Infoln is equivalent to the global Infoln function, guarded by the value of v. See the documentation of V for usage.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL