mo

package module
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Sep 13, 2023 License: MIT Imports: 13 Imported by: 3

README

Mo

A leveled logger library for golang.

Features

  • Support level log
  • Support file line number
  • Support for distinguishing log modules
  • Support log colors
  • Support for custom formatting
go get github.com/mengdu/mo
package main
import (
	"github.com/mengdu/mo"
)

func main() {
	mo.Error("Error message")
	mo.Warn("Warn message")
	mo.Info("Warn message")
	mo.Log("Log message")
	mo.Success("Success message")
	mo.Debug("Debug message")
	mo.With(map[string]interface{}{
		"a": 1,
	}).Info("With meta message")
}

Example

Preview

Levels

LEVEL_NONE
LEVEL_ERROR
LEVEL_WARN
LEVEL_INFO
LEVEL_LOG
LEVEL_SUCCESS
LEVEL_DEBUG
LEVEL_ALL
logger := mo.New()
logger.Level = mo.LEVEL_ALL

Production recommendations

file, _ := os.OpenFile("service.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
errfile, _ := os.OpenFile("service-err.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
logger := mo.New()
logger.Formater = &mo.JsonForamter{}
logger.Stdout = file
logger.Stderr = errfile
logger.Level = mo.LEVEL_SUCCESS

Benchmark

go test -cpu=4 -benchmem -benchtime=5s -bench "^Benchmark"
goos: darwin
goarch: amd64
pkg: github.com/mengdu/mo
cpu: Intel(R) Core(TM) i7-8700B CPU @ 3.20GHz
BenchmarkNone-4                 1000000000               0.5773 ns/op          0 B/op          0 allocs/op
BenchmarkDefault-4              19812234               315.4 ns/op           384 B/op         15 allocs/op
BenchmarkWith-4                   936283              6774 ns/op            7416 B/op         68 allocs/op
BenchmarkWithTag-4               7291065               847.5 ns/op           464 B/op         23 allocs/op
BenchmarkCaller-4                5136397              1081 ns/op             800 B/op         25 allocs/op
BenchmarkFull-4                  3293016              1849 ns/op            2057 B/op         66 allocs/op
BenchmarkJsonForamter-4         12249668               497.5 ns/op           460 B/op          8 allocs/op
BenchmarkGoLog-4                12026568               464.6 ns/op           264 B/op          3 allocs/op
PASS
ok      github.com/mengdu/mo    48.118s

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Std = &Logger{
	Stdout:   os.Stdout,
	Stderr:   os.Stderr,
	Level:    LEVEL_ALL,
	Formater: &TextForamter{},
	Caller:   false,
}

Functions

func Debug

func Debug(s ...any)

func Debugf

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

func Error

func Error(s ...any)

func Errorf

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

func Info

func Info(s ...any)

func Infof

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

func Log

func Log(s ...any)

func Logf

func Logf(format string, s ...any)

func Panic added in v0.3.0

func Panic(s ...any)

func Panicf added in v0.3.0

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

func Sprintf added in v0.2.0

func Sprintf(format string, args ...any) string

func Success

func Success(s ...any)

func Successf

func Successf(format string, s ...any)

func Warn

func Warn(s ...any)

func Warnf

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

Types

type Entry

type Entry struct {
	Meta Meta
	Tag  string
	// contains filtered or unexported fields
}

func With

func With(meta map[string]any) *Entry

func WithTag added in v0.4.0

func WithTag(tag string) *Entry

func (*Entry) Debug

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

func (*Entry) Debugf

func (e *Entry) Debugf(fotmat string, s ...any)

func (*Entry) Error

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

func (*Entry) Errorf

func (e *Entry) Errorf(fotmat string, s ...any)

func (*Entry) Info

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

func (*Entry) Infof

func (e *Entry) Infof(fotmat string, s ...any)

func (*Entry) Log

func (e *Entry) Log(s ...any)

func (*Entry) Logf

func (e *Entry) Logf(fotmat string, s ...any)

func (*Entry) Panic added in v0.3.0

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

func (*Entry) Panicf added in v0.3.0

func (e *Entry) Panicf(fotmat string, s ...any)

func (*Entry) Success

func (e *Entry) Success(s ...any)

func (*Entry) Successf

func (e *Entry) Successf(fotmat string, s ...any)

func (*Entry) Warn

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

func (*Entry) Warnf

func (e *Entry) Warnf(fotmat string, s ...any)

func (*Entry) With

func (e *Entry) With(meta map[string]any) *Entry

type Formater

type Formater interface {
	Format(log *Record) ([]byte, error)
}

type JsonForamter

type JsonForamter struct{}

func (*JsonForamter) Format

func (f *JsonForamter) Format(log *Record) ([]byte, error)

type Level

type Level int8
const (
	LEVEL_NONE Level = iota
	LEVEL_ERROR
	LEVEL_WARN
	LEVEL_INFO
	LEVEL_LOG
	LEVEL_SUCCESS
	LEVEL_DEBUG
	LEVEL_ALL
)

func (Level) MarshalText

func (l Level) MarshalText() ([]byte, error)

func (Level) String

func (l Level) String() string

type Logger

type Logger struct {
	Stdout   io.Writer
	Stderr   io.Writer
	Formater Formater
	// log meta fields
	Meta Meta
	// Caller tracing
	Caller bool
	// Caller tracing file is relative path
	RelativeFilePath    bool
	ForceColor          bool
	DisableColor        bool
	DisableSprintfColor bool
	Tag                 string
	Level               Level
	// contains filtered or unexported fields
}

func New

func New() *Logger

func (*Logger) Debug

func (l *Logger) Debug(s ...any)

func (*Logger) Debugf

func (l *Logger) Debugf(fotmat string, s ...any)

func (*Logger) EnableColor added in v0.2.0

func (l *Logger) EnableColor() bool

func (*Logger) Error

func (l *Logger) Error(s ...any)

func (*Logger) Errorf

func (l *Logger) Errorf(fotmat string, s ...any)

func (*Logger) Info

func (l *Logger) Info(s ...any)

func (*Logger) Infof

func (l *Logger) Infof(fotmat string, s ...any)

func (*Logger) IsEnableLevel

func (l *Logger) IsEnableLevel(level Level) bool

func (*Logger) Log

func (l *Logger) Log(s ...any)

func (*Logger) Logf

func (l *Logger) Logf(fotmat string, s ...any)

func (*Logger) Panic added in v0.3.0

func (l *Logger) Panic(s ...any)

func (*Logger) Panicf added in v0.3.0

func (l *Logger) Panicf(fotmat string, s ...any)

func (*Logger) Sprintf added in v0.2.0

func (l *Logger) Sprintf(format string, args ...any) string

func (*Logger) Success

func (l *Logger) Success(s ...any)

func (*Logger) Successf

func (l *Logger) Successf(fotmat string, s ...any)

func (*Logger) Warn

func (l *Logger) Warn(s ...any)

func (*Logger) Warnf

func (l *Logger) Warnf(fotmat string, s ...any)

func (*Logger) With

func (l *Logger) With(meta map[string]any) *Entry

func (*Logger) WithTag added in v0.4.0

func (l *Logger) WithTag(tag string) *Entry

type Meta

type Meta = map[string]any

type Record

type Record struct {
	Logger   *Logger       `json:"-"`
	Buf      *bytes.Buffer `json:"-"`
	At       time.Time     `json:"at"`
	Tag      string        `json:"tag"`
	Level    Level         `json:"level"`
	Message  string        `json:"msg"`
	Meta     Meta          `json:"meta"`
	Filename string        `json:"file"`
}

type TextForamter

type TextForamter struct {
	DisableLevelIcon bool
	EnableTime       bool
	EnableLevel      bool
	ShortLevel       bool
	TimeLayout       string
}

func (*TextForamter) Format

func (f *TextForamter) Format(log *Record) ([]byte, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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