Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duplicate print in log #123

Open
shamanis opened this issue Mar 29, 2018 · 3 comments
Open

Duplicate print in log #123

shamanis opened this issue Mar 29, 2018 · 3 comments

Comments

@shamanis
Copy link

shamanis commented Mar 29, 2018

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?

@mashuiping
Copy link

@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")

@RussoNC
Copy link

RussoNC commented Apr 10, 2019

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!

@channprj
Copy link

It would be nice if more samples provided.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants