flog

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2022 License: MIT Imports: 12 Imported by: 8 modules, 11 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Flog

Build Status

⚙ Installation

go get -u github.com/eininst/flog

⚡ Quickstart

package main

import (
	"github.com/eininst/flog"
)

func main() {
    flog.Trace("Something very low level.")
    flog.Debug("Useful debugging information.")
    flog.Info("Something noteworthy happened!")
    flog.Warn("You should probably take a look at this.")
    flog.Error("Something failed but I'm not quitting.")

    flog.Info("1", "2", "3")
    flog.Info(flog.Sprintf("My name is ${name}", flog.H{
        "name": "jack",
    }))
    flog.Infof("My name is %s", "tomi")

    //Calls os.Exit(1) after logging
    flog.Fatal("Bye.")
    //Calls panic() after logging
    flog.Panic("I'm bailing.")
}
2022/08/28 17:44:13 [TRACE] test.go:8 Something very low level. 
2022/08/28 17:44:13 [DEBUG] test.go:9 Useful debugging information.     
2022/08/28 17:44:13 [INFO] test.go:10 Something noteworthy happened!    
2022/08/28 17:44:13 [WARN] test.go:11 You should probably take a look at this.  
2022/08/28 17:44:13 [ERROR] test.go:12 Something failed but I'm not quitting.   
2022/08/28 17:44:13 [INFO] test.go:14 1 2 3     
2022/08/28 17:44:13 [INFO] test.go:15 My name is jack   
2022/08/28 17:44:13 [INFO] test.go:18 My name is tomi   
2022/08/28 17:44:13 [FATAL] test.go:21 Bye. 

With Fields

package main

import (
	"github.com/eininst/flog"
)

func main() {
    flog.Debug("Start with fields!")

    flog.With(flog.Fields{
        "animal": "walrus",
        "size":   10,
    }).Info("A group of walrus emerges from the ocean")

    flog.With(flog.Fields{
        "omg":    true,
        "number": 122,
    }).Warn("The group's number increased tremendously!")

    flog.With(flog.Fields{
        "name":   "wzq",
        "omg":    true,
        "number": 100,
    }).Error("The ice breaks!")

    contextLogger := flog.With(flog.Fields{
        "common": "this is a common field",
        "other":  "I also should be logged always",
    })

    contextLogger.Info("I'll be logged with common and other field")
    contextLogger.Info("Me too")
}
2022/08/29 20:55:41 [DEBUG] main.go:12 Start with fields!
2022/08/29 20:55:41 [INFO] main.go:17 A group of walrus emerges from the ocean  animal=walrus size=10
2022/08/29 20:55:41 [WARN] main.go:22 The group's number increased tremendously!  number=122 omg=true
2022/08/29 20:55:41 [ERROR] main.go:28 The ice breaks!  name=wzq number=100 omg=true
2022/08/29 20:55:41 [INFO] main.go:35 I'll be logged with common and other field  common=this is a common field other=I also should be logged always
2022/08/29 20:55:41 [INFO] main.go:36 Me too  common=this is a common field other=I also should be logged always

You can customize it all you want:

func init() {
    flog.SetLevel(flog.InfoLevel)
    
    flog.SetTimeFormat("2006.01.02 15:04:05.000")

    logf := "%s[${pid}]%s ${time} ${level} ${path} ${msg}"
    flog.SetFormat(fmt.Sprintf(logf, flog.Cyan, flog.Reset))

    flog.SetFullPath(true)
    
    flog.SetMsgMinLen(50)
}
[79338] 2022.09.04 10:02:55.140 [INFO] /Users/wangziqing/go/flog/examples/main.go:27 A group of walrus emerges from the ocean            animal=walrus size=10
[79338] 2022.09.04 10:02:55.140 [WARN] /Users/wangziqing/go/flog/examples/main.go:32 The group's number increased tremendously!          number=122 omg=true
[79338] 2022.09.04 10:02:55.140 [ERROR] /Users/wangziqing/go/flog/examples/main.go:38 The ice breaks!                                     name=wzq number=100 omg=true
[79338] 2022.09.04 10:02:55.140 [INFO] /Users/wangziqing/go/flog/examples/main.go:45 I'll be logged with common and other field          common=this is a common field other=I also should be logged always
[79338] 2022.09.04 10:02:55.140 [INFO] /Users/wangziqing/go/flog/examples/main.go:46 Me too                                              common=this is a common field other=I also should be logged always

Dump Json

func init() {
    flog.DumpJson()
}
{"level":"DEBUG","msg":"Start with fields!","path":"/Users/wangziqing/go/flog/examples/main.go:24","pid":"79546","time":"2022.09.04 10:04:59.105"}
{"animal":"walrus","level":"INFO","msg":"A group of walrus emerges from the ocean","path":"/Users/wangziqing/go/flog/examples/main.go:29","pid":"79546","size":10,"time":"2022.09.04 10:04:59.105"}
{"level":"WARN","msg":"The group's number increased tremendously!","number":122,"omg":true,"path":"/Users/wangziqing/go/flog/examples/main.go:34","pid":"79546","time":"2022.09.04 10:04:59.105"}
{"level":"ERROR","msg":"The ice breaks!","name":"wzq","number":100,"omg":true,"path":"/Users/wangziqing/go/flog/examples/main.go:40","pid":"79546","time":"2022.09.04 10:04:59.105"}
{"common":"this is a common field","level":"INFO","msg":"I'll be logged with common and other field","other":"I also should be logged always","path":"/Users/wangziqing/go/flog/examples/main.go:47","pid":"79546","time":"2022.09.04 10:04:59.105"}
{"common":"this is a common field","level":"INFO","msg":"Me too","other":"I also should be logged always","path":"/Users/wangziqing/go/flog/examples/main.go:48","pid":"79546","time":"2022.09.04 10:04:59.105"}

See examples

License

MIT

Documentation

Index

Constants

View Source
const (
	Reset       = "\033[0m"
	Red         = "\033[31m"
	Green       = "\033[32m"
	Yellow      = "\033[33m"
	Blue        = "\033[34m"
	Magenta     = "\033[35m"
	Cyan        = "\033[36m"
	White       = "\033[37m"
	GreenBold   = "\033[32;1m"
	BlueBold    = "\033[34;1m"
	MagentaBold = "\033[35;1m"
	RedBold     = "\033[31;1m"
	YellowBold  = "\033[33;1m"
)

Variables

View Source
var (
	DefaultFormat     = "${time} ${level} ${path} ${msg}"
	DefaultTimeFormat = "2006/01/02 15:04:05"
)

Functions

func Debug

func Debug(a ...any)

func Debugf

func Debugf(format string, a ...any)

func DumpJson

func DumpJson()

func Error

func Error(a ...any)

func Errorf

func Errorf(format string, a ...any)

func Fatal

func Fatal(a ...any)

func Fatalf

func Fatalf(format string, a ...any)

func Info

func Info(a ...any)

func Infof

func Infof(format string, a ...any)

func Panic

func Panic(a ...any)

func Panicf

func Panicf(format string, a ...any)

func SetFormat

func SetFormat(format string)

func SetFullPath

func SetFullPath(fullPath bool)

func SetLevel

func SetLevel(level LogLevel)

func SetMsgMinLen

func SetMsgMinLen(mlen int)

func SetTimeFormat

func SetTimeFormat(format string)

func Sprintf

func Sprintf(format string, h map[string]any) string

func Trace

func Trace(a ...any)

func Tracef

func Tracef(format string, a ...any)

func Warn

func Warn(a ...any)

func Warnf

func Warnf(format string, a ...any)

Types

type Config

type Config struct {
	Json       bool
	Format     string
	TimeFormat string
	LogLevel   LogLevel
	FullPath   bool
	MsgMinLen  int
}

Config logger config

type Entry

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

func Is

func Is(ok bool) *Entry

func NewEntry

func NewEntry(logger *logger, ok bool) *Entry

func With

func With(fields Fields) *Entry

func (*Entry) Debug

func (e *Entry) Debug(a ...any)

func (*Entry) Debugf

func (e *Entry) Debugf(format string, a ...any)

func (*Entry) Error

func (e *Entry) Error(a ...any)

func (*Entry) Errorf

func (e *Entry) Errorf(format string, a ...any)

func (*Entry) Fatal

func (e *Entry) Fatal(a ...any)

func (*Entry) Fatalf

func (e *Entry) Fatalf(format string, a ...any)

func (*Entry) Info

func (e *Entry) Info(a ...any)

func (*Entry) Infof

func (e *Entry) Infof(format string, a ...any)

func (*Entry) Panic

func (e *Entry) Panic(a ...any)

func (*Entry) Panicf

func (e *Entry) Panicf(format string, a ...any)

func (*Entry) Trace

func (e *Entry) Trace(a ...any)

func (*Entry) Tracef

func (e *Entry) Tracef(format string, a ...any)

func (*Entry) Warn

func (e *Entry) Warn(a ...any)

func (*Entry) Warnf

func (e *Entry) Warnf(format string, a ...any)

func (*Entry) With

func (e *Entry) With(fields Fields) *Entry

type Fields

type Fields map[string]interface{}

type H

type H map[string]interface{}

type Interface

type Interface interface {
	DumpJson()
	SetConfig(cfg Config)
	SetLevel(LogLevel)
	SetFormat(string)
	SetTimeFormat(string)
	SetFullPath(bool)
	SetMsgMinLen(mlen int)
	Is(bool) *Entry

	With(fields Fields) *Entry
	Trace(a ...any)
	Tracef(format string, a ...any)
	Debug(a ...any)
	Debugf(format string, a ...any)
	Info(a ...any)
	Infof(format string, a ...any)
	Warn(a ...any)
	Warnf(format string, a ...any)
	Error(a ...any)
	Errorf(format string, a ...any)
	Fatal(a ...any)
	Fatalf(format string, a ...any)
	Panic(a ...any)
	Panicf(format string, a ...any)
}

func DefaultInstance

func DefaultInstance() Interface

func New

func New(config Config) Interface

type LogLevel

type LogLevel int

LogLevel log level

const (
	// Silent silent log level
	Silent LogLevel = iota + 1
	// Error error log level
	ErrorLevel
	// Warn warn log level
	WarnLevel
	// Info info log level
	InfoLevel
	// Debug debug log level
	DebugLevel
	// Trace debug log level
	TraceLevel
)

type MutexWrap

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

func (*MutexWrap) Disable

func (mw *MutexWrap) Disable()

func (*MutexWrap) Lock

func (mw *MutexWrap) Lock()

func (*MutexWrap) Unlock

func (mw *MutexWrap) Unlock()

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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