logger

package module
v0.0.0-...-78e0282 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2017 License: Apache-2.0 Imports: 6 Imported by: 1

README

logger

Logger is a simple cross platform Go logging library for Windows, Linux, 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/shiguanghuxian/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()

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

  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)
// Don't to system log or a log file, Info logs write to stdout..
loggerTwo := logger.Init("LoggerExample", true, false, ioutil.Discard)

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")

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 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 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{})

Fatalln 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 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 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 Warn

func Warn(v ...interface{})

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

func Warnf

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

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

func Warnln

func Warnln(v ...interface{})

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

Types

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.

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) 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) 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) 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) Warn

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

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

func (*Logger) Warnf

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

Warnf logs with the Warn severity. Arguments are handled in the manner of fmt.Printf.

func (*Logger) Warnln

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

Warnln logs with the Warn severity. Arguments are handled in the manner of fmt.Println.

Jump to

Keyboard shortcuts

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