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

Updating level a logger without creating a new pointer #793

Closed
adibrastegarnia opened this issue Feb 27, 2020 · 5 comments
Closed

Updating level a logger without creating a new pointer #793

adibrastegarnia opened this issue Feb 27, 2020 · 5 comments

Comments

@adibrastegarnia
Copy link

Currently, I was trying to update the level of a logger by creating a new one an assign it to the old one but I noticed these two are pointing to different addresses and won't be useful during runtime. Like this:


newLogger := loggerNode.stdLogger.WithOptions(
				zp.WrapCore(
					func(zc.Core) zc.Core {
						return zc.NewCore(loggerNode.encoder, loggerNode.writer, &newLevel)
					}))

I am wondering is it possible to change the level of a logger without recreating a new logger? Please consider that I wrote a custom logger.

@prashantv
Copy link
Collaborator

You can update the level of a logger by:

  1. Creating an AtomicLevel, hold on to the reference to the AtomicLevel
  2. Create logger by passing in the above AtomicLevel as the level to the logger
  3. Update the level by using using SetLevel on the AtomicLevel:
    https://godoc.org/go.uber.org/zap#AtomicLevel.SetLevel

@adibrastegarnia
Copy link
Author

@prashantv
Thanks. Let me try it.

@adibrastegarnia
Copy link
Author

adibrastegarnia commented Feb 27, 2020

@prashantv
That works fine. Thanks. I have the same question for the writerSyncer. At runtime, I want to change it to a sink without recreating the logger. I did almost the same thing by updating the the value of pointer to the writer but it doesn't work. Any suggestions?

@prashantv
Copy link
Collaborator

We don't have official support for a write syncer that can be changed, but since it's an interface you can build your own implementation that does allow changing the underlying WriteSyncer, something like:

type AtomicWriteSyncer struct{
  underlying atomic.Value // WriteSyncer
}

func NewAtomicWriteSyncer(ws zapcore.WriteSyncer) zapcore.WriteSyncer {
  return &AtomicWriteSyncer{ws}
}

func (w *AtomicWriteSyncer) writeSyncer() zapcore.WriteSyncer {
  return underlying.Load().(zapcore.WriteSyncer)
}

func (w *AtomicWriteSyncer) SetWriteSyncer(new zapcore.WriteSyncer) {
  w.underlying.Store(new)
}

func (w *AtomicWriteSyncer) Write(p []byte) (int, error) {
  return w.writeSyncer().Write(p)
}

func (w *AtomicWriteSyncer) Sync() error {
  return w.writeSyncer().Sync()
}

You would hold the reference to the above AtomicWriteSyncer so you can change it dynamically.

@adibrastegarnia
Copy link
Author

@prashantv
Thanks for your response. I will try it out.

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

No branches or pull requests

2 participants