Categorygithub.com/google/logger
modulepackage
1.1.1
Repository: https://github.com/google/logger.git
Documentation: pkg.go.dev

# README

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

CodeExample
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

# Functions

Close closes the default logger.
Error uses the default logger and logs with the Error severity.
ErrorDepth acts as Error but uses depth to determine which call frame to log.
Errorf uses the default logger and logs with the Error severity.
Errorln uses the default logger and logs with the Error severity.
Fatal uses the default logger, logs with the Fatal severity, and ends with os.Exit(1).
FatalDepth acts as Fatal but uses depth to determine which call frame to log.
Fatalf uses the default logger, logs with the Fatal severity, and ends with os.Exit(1).
Fatalln uses the default logger, logs with the Fatal severity, and ends with os.Exit(1).
Info uses the default logger and logs with the Info severity.
InfoDepth acts as Info but uses depth to determine which call frame to log.
Infof uses the default logger and logs with the Info severity.
Infoln uses the default logger and logs with the Info severity.
Init sets up logging and should be called before log functions, usually in the caller's main().
SetFlags sets the output flags for the logger.
SetLevel sets the verbosity level for verbose info logging in the default logger.
V generates a log record depends on the setting of the Level or none by default using the default logger.
Warning uses the default logger and logs with the Warning severity.
WarningDepth acts as Warning but uses depth to determine which call frame to log.
Warningf uses the default logger and logs with the Warning severity.
Warningln uses the default logger and logs with the Warning severity.

# Structs

A Logger represents an active logging object.
Verbose is type that implements Infof, etc.

# Type aliases

Level describes the level of verbosity for info messages when using V style logging.