4 ms·
This is a general problem with code examples. You want the example to simple enough to easily understand. But many features only really makes sense in larger pr
by goto11 4y ago
This is a general problem with code examples. You want the example to simple enough to easily understand. But many features only really makes sense in larger programs, so in a minimal example they seem overly complicated.
You don't want the code which logs a message to be coupled to the logging mechanism. You should be able to transparently change the target (file/socket/database or a combination) without having to change code and dependencies every place a message is logged.
If you don't need any of that, you just use print().
- wizofaus 4y agoActually there's a decent argument you shouldn't allow arbitrary logging targets, especially anything that requires network calls. Writing to a file that's then monitored and uploaded to a database or other managed logging service is more robust. But at least allowing console output, file or in Windows OutputDebugString output is definitely needed.
- BoorishBears 4y agoI think that's a pretty uncharitable reading. Saying network target implies eventually. the data ends up on the network, not that this person is proposing blocking execution to make API calls on logging.
- citrin_ru 4y agoLogging framework which is too flexible and too transparent for end users is one of the reasons behind log4shell IMHO.
- BoorishBears 4y agoAgain, being pretty uncharitable. There is a vast and mighty ocean between "allow configurable targets" and intentionally supporting querying LDAP via string substitution. The kitchen sink approach is its own poison.
- jcelerier 4y agoIf your library does not allow logging to network it does not mean that things won't be logged to the network, just that someone somewhere will have an additional script that netcats the local output, overrides your function calls with LD_PRELOAD to inject the network calls or something like this. Preventing features from existing never makes things better.
- wizofaus 4y agoThere's always that risk, but there's not much worse than a logging framework that makes it's far too easy to affect system behaviour in unpredictable ways (looking at you log4j), or be unreliable in a way that you have no source of what's happened when it falls down itself.
- wizofaus 4y agoHaving said that, I know of at least two legitimate use cases for allowing arbitrary logging targets, both because of annoying limitations of the hosting environment. One being certain unit testing frameworks (e.g. Xunit in the .NET world requires you write to a custom destination for log messages to be captured by the test runner) and the other being AWS lambdas - where you don't even have the option of a separate process reading the log files for further processing. Ideally it would always be sufficient just to write to stdout or stderr.
- goto11 4y agoSure, but this choice shouldn't be the concern of the code writing the log message.
- wizofaus 4y agoNo, but it might be an argument for not using generic interfaces where an implementation that literally does anything at all might be passed in. In C# and similar languages this could be done with an abstract base class with an internal constructor for instance. No idea if Python supports anything like that.
- goto11 4y agoIsn't this the approach the article is recommending against? Prefer composition over inheritance.
- wizofaus 4y agoThe logging target may be a property that is part of the composition of the logger itself, but it would still need to support interface- or abstract-"inheritance" (even if via duck-typing) to be useful.
- toolslive 4y agoThere's also the question whether side effects (IO) are involved in the logging or not. Can it fail (silently)? Is it synchronous ? Does the source care about all of this? Damned, decent logging can be hard ;) The use of print was also endorsed by the twelve-factor methodology[1] and for simple cloud based applications, it could be as simple as that. [1] https://12factor.net/logs https://12factor.net/logs
- Der_Einzige 4y agoI still don't know how people get non trivial experience with kubternetes from the very beginning. Nearly all the code examples of it on the internet are these tiny contrived examples where ultimately for that example use case something other than kubternetes would have been a bad choice.