-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Add a helper function that makes it easier to log from anywhere #26582
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like a step in the right direction!
But CI says no |
Fix the path in composer.json. I don't know why composer dump-autoload produces a different result.
|
Use composer 2 |
Good point. I think we need composer 1 for some horde repositories. Will install it as composer2 and run dump-autoload again. |
92cb7d4
to
29b66f5
Compare
I gave this another thought over the last few days based on some input by @miaulalala. I would still say a free function is preferable over a static method on a class. Putting some bad memories from the old static APIs aside, I think the class approach will make it too tempting to introduce state via static variables, and therefore pull in all the trouble of impure "functions". The current free function is pure-ish, the result does depend on some side effects but at least the function is guaranteed to not change any state. IMO this should be the preferred way. |
Please rebase :) |
d068d47
to
9c80fe5
Compare
9c80fe5
to
57f39b1
Compare
/rebase |
57f39b1
to
f1d97a3
Compare
Our DI is able to inject a logger implementation to any server and app class if they want one. However, sometimes DI isn't applicable or hard to add. In those cases we typically fell back to the *service locator* pattern where we acquired a logger from the server via a global variable. There were some issues with that * `\OC` is a private class, apps are not supposed to use it * `\OC::$server` is a global variable, a well known anti-pattern * `\OC::$server->get(...)` uses the service locator anti-pattern * `\OC::$server->get(...)` may throw * `\OC::$server->get(LoggerInterface::class)` is not scoped to an app With this patch I'm proposing a new helper function ``\OCP\Log\logger`` that can be used to acquire a logger more easily. This function is meant to be public API and therefore apps may use it and there is an optional parameter to specifiy the app ID. The function hides all the ugly details about the global variable and the potentially thrown exceptions from the user. Therefore it's guaranteed that you always get a logger instance. In the worst case you get a noop, though those occasions should be rare. Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
f1d97a3
to
450136b
Compare
Any changes resulting from |
#26582 (comment) I stand by that. |
@juliushaertl @PVince81 @icewind1991 any preference on your side? |
I'd be all in favour for the current approach of that PR to have it somehow independent API wise from the server container. |
Our DI is able to inject a logger implementation to any server and app
class if they want one. However, sometimes DI isn't applicable or hard
to add. In those cases we typically fell back to the service locator
pattern where we acquired a logger from the server via a global
variable.
There were some issues with that
\OC
is a private class, apps are not supposed to use it\OC::$server
is a global variable, a well known anti-pattern\OC::$server->get(...)
uses the service locator anti-pattern\OC::$server->get(...)
may throw\OC::$server->get(LoggerInterface::class)
is not scoped to an appWith this patch I'm proposing a new helper function
\OCP\Log\logger
that can be used to acquire a logger more easily. This function is meant
to be public API and therefore apps may use it and there is an optional
parameter to specifiy the app ID.
The function hides all the ugly details about the global variable and
the potentially thrown exceptions from the user. Therefore it's
guaranteed that you always get a logger instance. In the worst case you
get a noop, though those occasions should be rare.
The intended usage is either
or quick and dirty (e.g. for some debugging)
This idea is based on a recent discussion with @nickvergessen and @rullzer. Let me know what you think.