flogger

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2021 License: MIT Imports: 5 Imported by: 1

README

flogger

Function call logger. Wrapper around zerolog

Motivation

Automate a process of printing function's input parameters and function execution time.

Usage

import "github.com/axkit/flogger"

type CustomerRepo struct {
    flog flogger.Logger
    ...    
}

type Filter struct {
    Name    string 
    Balance *int64
    ...
}

func NewCustomerRepo(l *zerolog.Logger) *CustomerRepo {
    return &CustomerRepo{flog : l.With("repo", "customer").Logger()}
}

 
func (repo *CustomerRepo)Customers(filter *Filter) []Customer {
    
    // filter = struct {Name : "ego", Balance: 300}
    fc := repo.flog.Enter(filter)
    defer fc.Exit()
    ...
    ..
    fc.Debug().Int("stage", 1).Msg("done")
    .
}

Output:
{"level":"debug","repo":"customer","func":"Customers","params":"[{Name:ego Balance:300}]","message":"enter"}
{"level":"debug","repo":"customer","func":"Customers","stage":1,"message":"done"}
{"level":"debug","repo":"customer","func":"Customers","dur":"12ms","message":"exit"}

func (repo *CustomerRepo)NewCustomers() []Customer {
    
    defer repo.flog.Enter().Exit()
  
    ...
    ..
    .    
}

Output:
{"level":"debug","repo":"customer","func":"NewCustomers","message":"enter"}
{"level":"debug","repo":"customer","func":"NewCustomers","dur":"12ms","message":"exit"}

func (repo *CustomerRepo)OldCustomers(age int) []Customer {
    
    defer repo.flog.Enter("ageLimit", age).Exit()
  
    ...
    ..
    .    
}
Output:
{"level":"debug","repo":"customer","func":"OldCustomers","params":"[ageLimit 42]","message":"enter"}
{"level":"debug","repo":"customer","func":"OldCustomers","dur":"12ms","message":"exit"}


func (repo *CustomerRepo)CustomerByID(id int) *Customer {
    
    // Does not write "enter" message to the log.
    // Writes function's input params to the log on exit. 
    defer repo.flog.EnterSilent("id", id).Flush().Exit()
   
    ...
    ..
    .    
}

Output:
{"level":"debug","repo":"customer","func":"CustomerByID","params":"[id 2]","dur":"3ms","message":"enter/exit"}

func (repo *CustomerRepo)CustomerByID(id int) *Customer {
    
    // Does not write enter and exit messages to the log if everything fine. 
    // Write function's input params if error failed.
    fc := repo.flog.EnterSilent("id", id)
    defer fc.Exit()

    if c, err := orm.SelectOne(&Customer{}, id); err != nil {
        fc.Flush()
        fc.Error().Str("errmsg", err.Error()).Msg("ORM SelectOne() failed")
    }
   
    ...
    ..
    .    
}

Output:
if no error: 
    no line in the log
if error:
    {"level":"error","repo":"customer","func":"CustomerByID","errmsg" : "table not found", "message":"ORM SelectOne() failed"}
    {"level":"debug","repo":"customer","func":"CustomerByID","params":"[id 2]","dur":"3ms","message":"enter/exit"}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FuncCall added in v0.0.2

type FuncCall struct {
	*FuncLogger
	// contains filtered or unexported fields
}

FuncCall holds data needed for a sigle function call tracking and logging.

func (*FuncCall) Debug added in v0.0.2

func (fc *FuncCall) Debug() *zerolog.Event

Debug return zerolog.Event for writing intermetiate log items between Enter() and Exit().

func (*FuncCall) Error added in v0.0.2

func (fc *FuncCall) Error() *zerolog.Event

Error return zerolog.Event for writing intermetiate log items between Enter() and Exit().

func (*FuncCall) Exit added in v0.0.2

func (fc *FuncCall) Exit()

Exit writes exit message to the log together with execution duration. If silent function call was asked to do flush by calling Flush() before, the input parameters will be written as well.

func (*FuncCall) Info added in v0.0.2

func (fc *FuncCall) Info() *zerolog.Event

Info return zerolog.Event for writing intermetiate log items between Enter() and Exit().

func (*FuncCall) Report added in v0.0.2

func (fc *FuncCall) Report() *FuncCall

Report informs silent function call, that input parameters should be written to the log in the Exit() call.

func (*FuncCall) Warn added in v0.0.2

func (fc *FuncCall) Warn() *zerolog.Event

Warn return zerolog.Event for writing intermetiate log items between Enter() and Exit().

type FuncLogger added in v0.0.2

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

FuncLogger wraps zerolog logger with type/name pair.

func New

func New(l *zerolog.Logger, typ, name string) FuncLogger

New builds new FuncLogger object.

func (*FuncLogger) Enter added in v0.0.2

func (s *FuncLogger) Enter(params ...interface{}) *FuncCall

Enter builds FuncCall object and writes message "enter" to the log. Log item gets func name and all input parameters.

func (*FuncLogger) EnterSilent added in v0.0.2

func (s *FuncLogger) EnterSilent(params ...interface{}) *FuncCall

EnterSilent builds FuncCall object but does not write enter message to the log.

func (*FuncLogger) Logger added in v0.0.2

func (s *FuncLogger) Logger() zerolog.Logger

Logger returns logger instance.

Jump to

Keyboard shortcuts

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