fvbock / endless

Zero downtime restarts for go servers (Drop in replacement for http.ListenAndServe)
MIT License
4.05k stars 342 forks source link

Ways to configure the logger #27

Open kartlee opened 8 years ago

kartlee commented 8 years ago

Hi Folks,

Is there a way to configure a new logger? I am thinking of using logrus to structure the logging in json format for a project, so if you have any suggestion, please recommend.

-Kartlee

fvbock commented 8 years ago

hi @kartlee

i am not familiar with logrus. what i did in some other project when using go-kit's logger was redirecting the output from the std lib log to the adapter gokit provides:

import (
    stdlog "log"
    kitlog "github.com/go-kit/kit/log"
)

stdlog.SetOutput(kitlog.NewStdlibAdapter(logger))

does something like that work for you?

Zambiorix commented 7 years ago

I think there is a better solution to implement in endless:

var logger = log.New(os.Stdout, "", 0)

var loggerDiscard = log.New(ioutil.Discard, "", 0)

// SetLogger ...
//
func SetLogger(lg *log.Logger) {

    if lg == nil {

        lg = loggerDiscard
    }

    logger = lg
}

func useLogger() {

    logger.Print("foo", "bar")
}

This way I can externally controle the logger (prefix, flags, ...) or even discard as I see fit. I use endless in a daemon and I want to eliminate the output, with the above code implemented in endless it would be possible...