Skip to content

posener/state-logger

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

state-logger

Build Status codecov

A package used to log state changes.

Features

  • Log using your favorite log library
  • Set log interval
  • Different log levels for errors and for success

Usage

See examples.

Sometimes you may have a function that fails. The code below shows a loop that tries to connect

var c Connection
for {
    c, err = connect()
    if err == nil {
        break
    }
    l.Println(err)
    time.Sleep(10 * time.Second)
}

If the connection fails, this function returns an error, and your logs will get an error message every 10 seconds. This makes them ugly, and also harder to debug.

This is the place you would like to use the state-logger:

import "github.com/posener/state-logger"

var c Connection
sl := logger.NewStateLogger("connection", log.Printf)
for {
    c, err = connect()
    if err == nil {
        break
    }
    sl.LogError(err)
    time.Sleep(10 * time.Second)
}
sl.Fixed()

This could make your logs from:

Failed connecting to server...
Failed connecting to server...
Failed connecting to server...
Failed connecting to server...
Failed connecting to server...

To:

[connection] error: Failed connecting to server... 
[connection] fixed!

In case that several errors occurred:

[connection] error: Failed connecting to server... 
[connection] error: DNS lookup failed
[connection] fixed!

About

A logging tool to log on state changes

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors