golog

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2020 License: Apache-2.0 Imports: 11 Imported by: 15

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

Documentation

Index

Constants

View Source
const (
	// NONE = 0
	NONE = iota
	// FATAL = 1
	FATAL
	// ERROR = 2
	ERROR
	// WARN = 3
	WARN
	// INFO = 4
	INFO
	// DEBUG = 5
	DEBUG
)
View Source
const (
	// PRINT = 0
	PRINT = iota
	// PRINTF = 1
	PRINTF
	// PRINTLN = 2
	PRINTLN
	// PRINTW = 3
	PRINTW
)
View Source
const (
	// FTIMESTAMP enables timestamp field in message log
	FTIMESTAMP = 1 << iota
	// FCALLER enables caller field in message log
	FCALLER
	// FFULLSTRUCTUREDLOG enables structured log for all fields in message log
	FFULLSTRUCTUREDLOG
)

Variables

This section is empty.

Functions

func AddHandler added in v0.2.0

func AddHandler(h Handler)

AddHandler add a new handler in the handler list

func Debug

func Debug(v ...interface{})

Debug logs with debug level

func Debugf

func Debugf(f string, v ...interface{})

Debugf logs with debug level

func Debugln

func Debugln(v ...interface{})

Debugln logs with debug level

func Debugw

func Debugw(msg string, v ...interface{})

Debugw logs with debug level

func DisableColor

func DisableColor()

DisableColor disables color for all log levels

func DisableLevelColor

func DisableLevelColor(level int)

DisableLevelColor enables color for a specific level

func DisableLogFormat

func DisableLogFormat()

DisableLogFormat disables format log of message. It will print unformatted msg as normally like with any function printf without color or prefix etc.

func EnableColor

func EnableColor()

EnableColor enables color for all log levels

func EnableLevelColor

func EnableLevelColor(level int)

EnableLevelColor enables color for a specific level

func EnableLogFormat

func EnableLogFormat()

EnableLogFormat enables format log of message. It will print msg as a log with color or prefix etc.

func Error

func Error(v ...interface{})

Error logs with error level

func Errorf

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

Errorf logs with error level

func Errorln

func Errorln(v ...interface{})

Errorln logs with error level

func Errorw

func Errorw(msg string, v ...interface{})

Errorw logs with error level

func Fatal

func Fatal(v ...interface{})

Fatal logs with Print() followed by os.Exit(1)

func Fatalf

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

Fatalf logs with Printf() followed by os.Exit(1)

func Fatalln

func Fatalln(v ...interface{})

Fatalln logs with Println() followed by os.Exit(1)

func Fatalw

func Fatalw(msg string, v ...interface{})

Fatalw logs with error level

func GetFlags added in v0.2.0

func GetFlags() int

GetFlags gets flags for message log output

func GetVerbosity

func GetVerbosity() int

GetVerbosity returns the current log level

func Info

func Info(v ...interface{})

Info logs with info level

func Infof

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

Infof logs with info level

func Infoln

func Infoln(v ...interface{})

Infoln logs with info level

func Infow

func Infow(msg string, v ...interface{})

Infow logs with debug level

func Log

func Log(p int, l *Logger, level int, f string, v ...interface{})

Log wraps print function but using goroutine and waitgroup to have a synchronization of logs.

func SetFlags

func SetFlags(flag int)

SetFlags sets flags for message log output

func SetLevelOutput

func SetLevelOutput(level int, w io.Writer)

SetLevelOutput sets output destination for a specific level

func SetOutput

func SetOutput(w io.Writer)

SetOutput sets output destination for a specific level

func SetTimeFormat

func SetTimeFormat(format string)

SetTimeFormat sets timestamp with the given format

func SetVerbosity

func SetVerbosity(v int)

SetVerbosity sets log level. If verbose < NONE, it will be set to NONE. If verbose > DEBUG, it will be set to DEBUG

func Warn

func Warn(v ...interface{})

Warn logs with warn level

func Warnf

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

Warnf logs with warn level

func Warnln

func Warnln(v ...interface{})

Warnln logs with warn level

func Warnw

func Warnw(msg string, v ...interface{})

Warnw logs with debug level

Types

type Field added in v0.2.0

type Field struct {
	Key   string
	Value string
}

Field defines a log field in case of structured log

type Fields added in v0.2.0

type Fields struct {
	Prefix []*Field
	Log    []*Field
}

Fields stores all log fields: prefix, static and user log

type Handler added in v0.2.0

type Handler interface {
	PrintMsg(p int, l *Logger, level int, fields Fields) error
}

Handler defines an interface for golog handler

type Logger

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

Logger is a wrapper of go log integrating log level

func NewLogger

func NewLogger() *Logger

NewLogger returns a new instance logger By default, it uses stderr for error and stdout for other levels

func (*Logger) AddHandler added in v0.2.0

func (l *Logger) AddHandler(h Handler)

AddHandler add a new handler in the handler list

func (*Logger) Debug

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

Debug logs with debug level

func (*Logger) Debugf

func (l *Logger) Debugf(f string, v ...interface{})

Debugf logs with debug level

func (*Logger) Debugln

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

Debugln logs with debug level

func (*Logger) Debugw

func (l *Logger) Debugw(msg string, v ...interface{})

Debugw logs with debug level with structured log format

func (*Logger) DisableColor

func (l *Logger) DisableColor()

DisableColor disables color for all log levels

func (*Logger) DisableLevelColor

func (l *Logger) DisableLevelColor(level int)

DisableLevelColor enables color for a specific level

func (*Logger) DisableLogFormat

func (l *Logger) DisableLogFormat()

DisableLogFormat disables format log of message. It will print unformatted msg as normally like with any function printf without color or prefix etc.

func (*Logger) EnableColor

func (l *Logger) EnableColor()

EnableColor enables color for all log levels

func (*Logger) EnableLevelColor

func (l *Logger) EnableLevelColor(level int)

EnableLevelColor enables color for a specific level

func (*Logger) EnableLogFormat

func (l *Logger) EnableLogFormat()

EnableLogFormat enables format log of message. It will print msg as a log with color or prefix etc.

func (*Logger) Error

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

Error logs with error level

func (*Logger) Errorf

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

Errorf logs with error level

func (*Logger) Errorln

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

Errorln logs with error level

func (*Logger) Errorw

func (l *Logger) Errorw(msg string, v ...interface{})

Errorw logs with error level with structured log format

func (*Logger) Fatal

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

Fatal logs with Print() followed by os.Exit(1)

func (*Logger) Fatalf

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

Fatalf logs with Printf() followed by os.Exit(1)

func (*Logger) Fatalln

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

Fatalln logs with Println() followed by os.Exit(1)

func (*Logger) Fatalw

func (l *Logger) Fatalw(msg string, v ...interface{})

Fatalw logs with fatal level with structured log format

func (*Logger) GetFlags added in v0.2.0

func (l *Logger) GetFlags() int

GetFlags gets flags for message log output

func (*Logger) GetVerbosity

func (l *Logger) GetVerbosity() int

GetVerbosity returns the current log level

func (*Logger) Info

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

Info logs with info level

func (*Logger) Infof

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

Infof logs with info level

func (*Logger) Infoln

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

Infoln logs with info level

func (*Logger) Infow

func (l *Logger) Infow(msg string, v ...interface{})

Infow logs with info level with structured log format

func (*Logger) SetFlags

func (l *Logger) SetFlags(flag int)

SetFlags sets flags for message log output

func (*Logger) SetLevelOutput

func (l *Logger) SetLevelOutput(level int, w io.Writer)

SetLevelOutput sets output destination for a specific level

func (*Logger) SetOutput

func (l *Logger) SetOutput(w io.Writer)

SetOutput sets output destination for a specific level

func (*Logger) SetTimeFormat

func (l *Logger) SetTimeFormat(format string)

SetTimeFormat sets timestamp with the given format

func (*Logger) SetVerbosity

func (l *Logger) SetVerbosity(v int)

SetVerbosity sets log level. If verbose < NONE, it will be set to NONE. If verbose > DEBUG, it will be set to DEBUG

func (*Logger) Warn

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

Warn logs with warn level

func (*Logger) Warnf

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

Warnf logs with warn level

func (*Logger) Warnln

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

Warnln logs with warn level

func (*Logger) Warnw

func (l *Logger) Warnw(msg string, v ...interface{})

Warnw logs with warn level with structured log format

type Mutex added in v0.2.0

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

Mutex is struct of mutex and locked to know if mutex is locked or not

func (*Mutex) IsLocked added in v0.2.0

func (m *Mutex) IsLocked() bool

IsLocked returns if mutex is locked

func (*Mutex) LockOnce added in v0.2.0

func (m *Mutex) LockOnce()

LockOnce locks mutex if it isnt yet

func (*Mutex) UnlockOnce added in v0.2.0

func (m *Mutex) UnlockOnce()

UnlockOnce unlocks mutex if it is

Directories

Path Synopsis
handlers

Jump to

Keyboard shortcuts

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