anz-bank / pkg

Common ANZ Go packages
https://pkg.go.dev/github.com/anz-bank/pkg
Apache License 2.0
1 stars 9 forks source link

Should logger fail if there's an error #9

Open nofun97 opened 4 years ago

nofun97 commented 4 years ago

In https://github.com/anz-bank/pkg/blob/master/log/util.go#L17 the library will panic if logger isn't created yet. Should the library fail or not?

nofun97 commented 4 years ago

I don't think it should return error but failing silently can be problematic. Maybe it should just log a warning if there's no logger. Or should it add a standard logger when there's no logger?

nofun97 commented 4 years ago

Currently, the library will panic when trying to log or get a logger and when it fails to apply a configuration. https://github.com/anz-bank/pkg/blob/master/log/util.go#L23-L56

jamesrom commented 4 years ago

I am in favour of not panicking if the logger is not on the context. A default logger could be used when one is missing on the context.

This will allow code to transition to using this package a bit easier. Thinking tactically, for example, if there's some crucial logging that needs to be done but passing context all the way down into to that line of code would require a large refactoring.

Defaults should cover most use cases, and not write a warnings. However, if it's important to let the developer know they should configure a logger manually, I would be in favour of adding a special value to the log line produced, something like unconfigured_logger=true or context=default_logger.

nofun97 commented 4 years ago

Okay, what about during applying a configuration to a logger? We're trying to let user make their own configuration, to handle that, we have the function func Apply(logger Logger) error and currently if would panic if Apply fails.

anzdaddy commented 4 years ago

Agreed, logging should generally not fail. Serving traffic is more important that getting the logs. Monitoring can generate alerts when there's a step change in logging volume. So…

  1. Let's add a default logger.
  2. Follow the same principle for Apply: logging functions shouldn't impact traffic.
anz-rfc commented 3 years ago

If this library automagically creates and starts using a default-configuration-logger if there isnt a logger in a context, i think it needs offer an API that lets the programmer look inside a context to see if it contains a logger or not. Is there already a way of doing that? I can see there is a func From(ctx context.Context) Logger , but it creates a default logger and doesnt give the caller a way to tell which logger they got -- one that was in the input ctx or a new one.

e.g. in a variety of situations when i am trying to configure and wire my app together properly, i want to make sure i am using my special logger instance with custom configuration. if i dont have that exact logger in a context i want to be able to detect it and fail as it indicates a programming error.