flog

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2022 License: MIT Imports: 11 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(message string, props map[string]string)
Error(message string, props map[string]string)
Fatal(message string, props map[string]string)

Usage

package main

import "github.com/twiny/flog"

func main() {
	logger, err := flog.NewLogger("./tmp/logs/", "test", 30)
	if err != nil {
		// handler error
		return
	}
	defer logger.Close()

	logger.Info("info", map[string]string{
		// add other info
	})

	logger.Error("error", map[string]string{
		// add other info
	})

	logger.Fatal("fatal", map[string]string{
		// add other info
	})
}

Benchmark

goos: darwin
goarch: amd64
pkg: github.com/twiny/flog
cpu: Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz
BenchmarkLogWrite-4       134395              9049 ns/op            1128 B/op         13 allocs/op
PASS
ok      github.com/twiny/flog   2.330s
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 Log

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

Log

type Logger

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

Logger

func NewLogger

func NewLogger(dir, prefix string, days int) (*Logger, error)

NewLogger

func (*Logger) Close

func (l *Logger) Close()

Close

func (*Logger) Error

func (l *Logger) Error(message string, props map[string]string)

PrintError

func (*Logger) Fatal added in v1.0.1

func (l *Logger) Fatal(message string, props map[string]string)

Fatal

func (*Logger) Info

func (l *Logger) Info(message string, props map[string]string)

PrintInfo

Jump to

Keyboard shortcuts

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