flog

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2022 License: MIT Imports: 9 Imported by: 2

README

flog

a simple logger API for Go program that save logs into a file.

NOTE: This package is provided "as is" with no guarantee. Use it at your own risk and always test it yourself before using it in a production environment. If you find any issues, please create a new issue.

Install

go get github.com/twiny/flog

API

Info(msg string, fields ...Field)
Error(msg string, fields ...Field)
Fatal(msg string, fields ...Field)

Usage

package main

import (
	"fmt"

	"github.com/twiny/flog"
)

// main
func main() {
	// config
	conf := &flog.Config{
		Dir:    "./logs", // log directory
		Prefix: "app",  // prefix
		Rotate: 7, // how many days to store logs
	}

	logger, err := flog.NewLogger(conf)
	if err != nil {
		fmt.Println(err)
		return
	}
	defer logger.Close()

	var x = struct {
		Name string
		Age  int
		Jobs map[string]any
	}{
		Name: "John Doe",
		Age:  22,
		Jobs: map[string]any{
			"digital marketer": 2019,
			"golang developer": "github",
		},
	}

	f := []flog.Field{
		flog.NewField("hello", "world"),
		flog.NewField("nice", 123),
		flog.NewField("person", x),
	}

	logger.Info("from_main", f...)
}

Benchmark

goos: darwin
goarch: amd64
pkg: github.com/twiny/flog
cpu: Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz
BenchmarkLogWrite-4       163430              6599 ns/op             718 B/op         11 allocs/op
PASS
ok      github.com/twiny/flog   1.320s
Run tests
test
go test -timeout 30s -run ^TestLogInfoWrite$ github.com/twiny/flog
go test -timeout 30s -run ^TestLogErrorWrite$ github.com/twiny/flog
go test -timeout 30s -run ^TestLogFatalWrite$ github.com/twiny/flog

benchmark
go test -benchmem -run=^$ -bench ^BenchmarkLogWrite$ github.com/twiny/flog
go test -benchmem -run=^$ -bench ^BenchmarkParallelLogWrite$ github.com/twiny/flog

Documentation

Index

Constants

View Source
const (
	DateFormat = "02-01-2006" // dd-mm-yyyy
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Config added in v1.1.0

type Config struct {
	Dir    string
	Prefix string
	Rotate int
}

Config

type Field added in v1.1.0

type Field struct {
	Key string
	Val any
}

Field

func NewField added in v1.1.0

func NewField(k string, v any) Field

NewField

type Log

type Log struct {
	Level   level          `json:"level"`
	Time    string         `json:"time"`
	Message string         `json:"message"`
	Props   map[string]any `json:"properties,omitempty"`
	Line    int            `json:"line,omitempty"`
	File    string         `json:"file,omitempty"`
	Trace   string         `json:"trace,omitempty"`
}

Log

type Logger

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

Logger

func NewLogger

func NewLogger(cnf *Config) (*Logger, error)

NewLogger

func (*Logger) Close

func (l *Logger) Close()

Close

func (*Logger) Debug added in v1.1.0

func (l *Logger) Debug(m string, fields ...Field)

Debug

func (*Logger) Error

func (l *Logger) Error(msg string, fields ...Field)

PrintError

func (*Logger) Fatal added in v1.0.1

func (l *Logger) Fatal(msg string, fields ...Field)

Fatal

func (*Logger) Info

func (l *Logger) Info(msg string, fields ...Field)

PrintInfo

Jump to

Keyboard shortcuts

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