Categorygithub.com/uthng/golog
modulepackage
0.2.1
Repository: https://github.com/uthng/golog.git
Documentation: pkg.go.dev

# README

golog

Simple logging library using golang log package. It uses io.Writer as output destination so it can log to stdout, stderr or into a file or multiple destinations at the same time using io.MultiWriter. Its is inspired of google/logger with some personal touches.

Documentation

See the Godoc

Usage

Set up a standard logger to stdout:

package main

import (
  "os"
  "github.com/uthng/common/golog"
)

function main() {
  golog.SetVerbosity(golog.DEBUG)

  golog.Debugln("This is debug log")
  golog.Infoln("This is info log")
  golog.Warnln("This is warn log")
  golog.Errorln("This is error log")
}

And when executed, the program will show the following output to the standard output:

DEBUG: 2018/11/12 02:08:57 log.go:161: This is debug log
INFO: 2018/11/12 02:08:57 log.go:161: This is info log
WARN: 2018/11/12 02:08:57 log.go:161: This is warn log
ERROR: 2018/11/12 02:08:57 log.go:161: This is error log

Set up multiple destinations

package main

import (
  "io"
  "os"
  "github.com/uthng/common/golog"
)

function main() {
  file, err := os.OpenFile("file.txt", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
  if err != nil {
	  os.Exit(-1)
  }

  multi := io.MultiWriter(file, os.Stdout)

  logger := golog.NewLogger()
  logger.SetVerbosity(golog.INFO)
  logger.SetOutput(multi)
  
  logger.Debugln("This is debug log")
  logger.Infoln("This is info log")
  logger.Warnln("This is warn log")
  logger.Errorln("This is error log")
}

When executed, the program will log to stdout and into file.txt the following output:

INFO: 2018/11/12 02:19:13 log.go:161: This is info log
WARN: 2018/11/12 02:19:13 log.go:161: This is warn log
ERROR: 2018/11/12 02:19:13 log.go:161: This is error log

# Packages

No description provided by the author

# Functions

AddHandler add a new handler in the handler list.
Debug logs with debug level.
Debugf logs with debug level.
Debugln logs with debug level.
Debugw logs with debug level.
DisableColor disables color for all log levels.
DisableLevelColor enables color for a specific level.
DisableLogFormat disables format log of message.
EnableColor enables color for all log levels.
EnableLevelColor enables color for a specific level.
EnableLogFormat enables format log of message.
Error logs with error level.
Errorf logs with error level.
Errorln logs with error level.
Errorw logs with error level.
Fatal logs with Print() followed by os.Exit(1).
Fatalf logs with Printf() followed by os.Exit(1).
Fatalln logs with Println() followed by os.Exit(1).
Fatalw logs with error level.
GetFlags gets flags for message log output.
GetVerbosity returns the current log level.
Info logs with info level.
Infof logs with info level.
Infoln logs with info level.
Infow logs with debug level.
Log wraps print function but using goroutine and waitgroup to have a synchronization of logs.
NewLogger returns a new instance logger By default, it uses stderr for error and stdout for other levels.
SetFlags sets flags for message log output.
SetLevelOutput sets output destination for a specific level.
SetOutput sets output destination for a specific level.
SetTimeFormat sets timestamp with the given format.
SetVerbosity sets log level.
Warn logs with warn level.
Warnf logs with warn level.
Warnln logs with warn level.
Warnw logs with debug level.

# Constants

DEBUG = 5.
ERROR = 2.
FATAL = 1.
FCALLER enables caller field in message log.
FFULLSTRUCTUREDLOG enables structured log for all fields in message log.
FTIMESTAMP enables timestamp field in message log.
INFO = 4.
NONE = 0.
PRINT = 0.
PRINTF = 1.
PRINTLN = 2.
PRINTW = 3.
WARN = 3.

# Structs

Field defines a log field in case of structured log.
Fields stores all log fields: prefix, static and user log.
Logger is a wrapper of go log integrating log level.
Mutex is struct of mutex and locked to know if mutex is locked or not.

# Interfaces

Handler defines an interface for golog handler.