gol

package module
v0.0.0-...-0e02856 Latest Latest
Warning

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

Go to latest
Published: Jan 3, 2020 License: MIT Imports: 7 Imported by: 0

README

Gol

Gol, the ananym of log, go log, go logrus or whatever, is logrus wrapper.
This package is focused on writing log to files purpose.

Features

  • Support auto Sync config.
  • Auto Rotate log file by date.
  • Minimum logging levels: info/error/debug.

Example

    // Create log dir
    os.MkdirAll("log", os.ModePerm)

    // Log to log/ingress_YYYYMMDD.log, Rotate file by Bangkok date time.
    x, err := gol.NewWithHookAutoFileDate("log/ingress.log", "Asia/Bangkok")
    if err != nil {
        panic(err)
    }

    // Log to log/egress_YYYYMMDD.log, Rotate file by Bangkok date time.
    y, err := gol.NewJSONWithHookAutoFileDate("log/egress.log", "Asia/Bangkok")
    if err != nil {
        panic(err)
    }

    x.GDebugf("GInfof debug")

    x.WithFields(gol.Fields{
        "isAwesome": true,
        "star":      9999,
    }).GInfof("Thailand has many awesome places to explore")

    x.WithFields(gol.Fields{
        "isBeauty": true,
        "star":      9999,
    }).GErrorf("Music is beautiful in a way that nothing else can be")

    y.WithFields(gol.Fields{
        "service":     "golapp",
        "status code": 0,
    }).GDebugf("Healthy")

Guideline

Timezone Problems when using container

Gol use go function time.LoadLocation() that may found panic: unknown time zone xxx and other time zone problems, especially when in alpine container. I recommend installing time zone information to container to fixed the issues.

FROM alpine:3.10.3
...
RUN apk add --no-cache tzdata && \
    cp /usr/share/zoneinfo/Asia/Bangkok /etc/localtime && \
    echo "Asia/Bangkok" >  /etc/timezone
...

References

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Entry

type Entry struct {
	logrus.Entry
	Logger *Gol
}

func (*Entry) GDebugf

func (entry *Entry) GDebugf(format string, args ...interface{})

GDebugf developer log & system information we don't want user to see.

func (*Entry) GErrorf

func (entry *Entry) GErrorf(format string, args ...interface{})

GErrorf user log case error

func (*Entry) GInfof

func (entry *Entry) GInfof(format string, args ...interface{})

GInfof user log

func (*Entry) Printf

func (entry *Entry) Printf(format string, args ...interface{})

Printf similar to GInfof

type Fields

type Fields logrus.Fields

type Gol

type Gol struct {
	*logrus.Logger
	// Mutex Locking is enabled by Default to protect OSFile and sensitive feilds
	MU             logrus.MutexWrap
	OSFile         *os.File
	FullPath       string
	EnableAutoSync bool // Disable by default
}

Gol logrus.Logger with additional features

func New

func New() *Gol

New Creates a new gol logger. Configuration should be set by changing `Formatter`, `Out` and `Hooks` directly on the default logger instance. You can also just instantiate your own:

 var log = &Gol{
		Logger: &logrus.Logger{
			Out:          os.Stderr,
			Formatter:    new(logrus.TextFormatter),
			Hooks:        make(logrus.LevelHooks),
			Level:        logrus.InfoLevel,
	 	},
		OSFile: nil,
		FullPath: "",
   }

It's recommended to make this a global instance called `log`.

func NewJSONWithHookAutoFileDate

func NewJSONWithHookAutoFileDate(filepath string, rotatefileTimezone string) (gol *Gol, err error)

func NewWithHookAutoFileDate

func NewWithHookAutoFileDate(filepath string, rotatefileTimezone string) (gol *Gol, err error)

func NewWithHookPrinter

func NewWithHookPrinter() *Gol

func (*Gol) FileSync

func (gol *Gol) FileSync()

FileSync sync os file

func (*Gol) GDebugf

func (gol *Gol) GDebugf(format string, args ...interface{})

GDebugf developer log & system information we don't want user to see.

func (*Gol) GErrorf

func (gol *Gol) GErrorf(format string, args ...interface{})

GErrorf user log case error

func (*Gol) GInfof

func (gol *Gol) GInfof(format string, args ...interface{})

GInfof user log

func (*Gol) GWithFields

func (gol *Gol) GWithFields(fields Fields) *Entry

GWithFields Adds a struct of fields to the log entry.

func (*Gol) Printf

func (gol *Gol) Printf(format string, args ...interface{})

Printf similar to GInfof

func (*Gol) SetFileOutput

func (gol *Gol) SetFileOutput(filename string) error

SetFileOutput Util method to set output to file

func (*Gol) WithFields

func (gol *Gol) WithFields(fields Fields) *Entry

WithFields Shadowing low level WithFields() method to prevent argument type error.

type HookAutoFileDate

type HookAutoFileDate struct {
	Logger       *Gol
	TimeLocation *time.Location

	Logpath string
	// contains filtered or unexported fields
}

func (*HookAutoFileDate) FileDateHandler

func (h *HookAutoFileDate) FileDateHandler()

func (*HookAutoFileDate) Fire

func (h *HookAutoFileDate) Fire(entry *logrus.Entry) error

Fire HookAutoFileDate Auto print log to stdout

func (*HookAutoFileDate) Levels

func (h *HookAutoFileDate) Levels() []logrus.Level

type HookPrinter

type HookPrinter struct {
}

HookPrinter ...

func (*HookPrinter) Fire

func (h *HookPrinter) Fire(entry *logrus.Entry) error

func (*HookPrinter) Levels

func (h *HookPrinter) Levels() []logrus.Level

Jump to

Keyboard shortcuts

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