go-logging icon indicating copy to clipboard operation
go-logging copied to clipboard

Duplicate print in log

Open shamanis opened this issue 8 years ago • 3 comments

go 1.9.2

logger := logging.MustGetLogger("main")
format := logging.MustStringFormatter(`%{time:2006-01-02 15:04:05.000} [%{level}] %{message}`)
backend := logging.NewLogBackend(os.Stdout, "", 0)
backendFormatter := logging.NewBackendFormatter(backend, format)
backendLevel := logging.AddModuleLevel(backend)
backendLevel.SetLevel(logging.DEBUG, "")
logging.SetBackend(backendLevel, backendFormatter)

logger.Debug("TestDebug")
logger.Warning("TestWarning")
2018-03-29 12:17:04.794 [DEBUG] TestDebug
TestWarning
2018-03-29 12:17:04.794 [WARNING] TestWarning

such behavior starting with the level WARNING Why?

shamanis avatar Mar 29 '18 07:03 shamanis

@shamanis i thank the readme must have confused a lot of prgrammers:

1. Why the output is duplicate
2. How to set logging level

The answer to both questions is that the readme "example" has two backend! Just look into the logging.SetBeckend func

func SetBackend(backends ...Backend) LeveledBackend {
	var backend Backend
	if len(backends) == 1 {
		backend = backends[0]
	} else {
		backend = MultiLogger(backends...)
	}

	defaultBackend = AddModuleLevel(backend)
	return defaultBackend
}

So, just change your code as the following,

logger := logging.MustGetLogger("main")
format := logging.MustStringFormatter(`%{time:2006-01-02 15:04:05.000} [%{level}] %{message}`)
backend := logging.NewLogBackend(os.Stdout, "", 0)
backendFormatter := logging.NewBackendFormatter(backend, format)
logging.SetBackend(backendFormatter)
logging.SetLevel(logging.DEBUG, "main")
logger.Debug("TestDebug")
logger.Warning("TestWarning")

And if want to change log level, just

logging.SetLevel(logging.xxx, "main")

mashuiping avatar Jan 30 '19 09:01 mashuiping

Yes I really think the readme should have a really simple example, and another where they show us an example with multiple backends, but other than that I love this lib!

RussoNC avatar Apr 10 '19 21:04 RussoNC

It would be nice if more samples provided.

channprj avatar Apr 24 '19 04:04 channprj