You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@logging.apache.org by "Piotr P. Karwasz" <pi...@mailing.copernik.eu> on 2024/10/31 15:11:52 UTC
New API for log level manipulation
Hi all,
In last few days I have encountered the problem of retrieving and
setting the configured level of a logger twice (in Nashorn[1] and Apache
Kafka[2]). What do you think about introducing a public API to do this
sort of things in an implementation independent way?
Currently we have `o.a.l.l.c.config.Configurator` that allows level
manipulation, but this is a Log4j Core specific class, which only works
with Log4j Core. We could publish a small API (e.g. called LogAdmin)
that allows to:
* list the configured level of all loggers,
* get the configured level of a single logger,
* set the configured level of a single logger.
The advantage of this API would be to remove a lot of custom user code
that modifies logger levels. Spring Boot has some[3], Kafka has some[4]
and I also wrote some code of this sort for my former employer (although
that one use JUL, since they refused to use Log4j Core and I bound
everything to `log4j-to-jul` instead).
I could probably sketch a small prototype that supports Log4j Core,
Logback and JUL over the weekend.
Piotr
[1] https://github.com/openjdk/nashorn/pull/19
[2] https://github.com/apache/kafka/pull/17373#discussion_r1824026165
[3]
https://docs.spring.io/spring-boot/api/java/org/springframework/boot/logging/LoggingSystem.html
[4]
https://cwiki.apache.org/confluence/display/KAFKA/KIP-495%3A+Dynamically+Adjust+Log+Levels+in+Connect
Re: New API for log level manipulation
Posted by Gary Gregory <ga...@gmail.com>.
Hi Piotr,
I would expect the LCD to allow
LogManager.setLevel("com.gary.app", "debug")
Having a Level version is OK as well.
Gary
On Thu, Oct 31, 2024, 1:25 PM Piotr P. Karwasz <pi...@mailing.copernik.eu>
wrote:
> Hi Gary,
>
> On 31.10.2024 17:12, Gary Gregory wrote:
> >
> >> set the configured level of a single logger
> > Some people (like me) will want to enable debug all in one go for
> example,
> > even for loggers that have not been instantiated yet.
> >
> > I think we should distinguish whether this is set the level for a
> hierarchy
> > for just the one and the one only.
>
> I would start with most common case of setting the (default) log level
> for a hierarchy. That requires a single operation in all the logging
> implementations I know.
>
> The signature of a `setLevel` method might be tricky:
>
> * Log4j Core does not create `Logger` instances for the internal nodes
> of the hierarchy (those that correspond to Java packages), so
> `setLevel(Logger, Level)` would be wasteful.
>
> * JUL on the other hand does not guarantee the uniqueness of logger
> names and Tomcat and others use this to create a separate logger
> hierarchy per application, which makes `setLevel(String, Level)`
> trickier to implement.
>
> * Logback is the easiest of the 3, since it just creates all the loggers
> and makes sure that their names are unique.
>
> One thing is constant: logger names are dot-separated strings and the
> name of the parent logger omits the last component of the logger's name.
>
> Piotr
>
>
Re: New API for log level manipulation
Posted by "Piotr P. Karwasz" <pi...@mailing.copernik.eu>.
Hi Gary,
On 31.10.2024 17:12, Gary Gregory wrote:
>
>> set the configured level of a single logger
> Some people (like me) will want to enable debug all in one go for example,
> even for loggers that have not been instantiated yet.
>
> I think we should distinguish whether this is set the level for a hierarchy
> for just the one and the one only.
I would start with most common case of setting the (default) log level
for a hierarchy. That requires a single operation in all the logging
implementations I know.
The signature of a `setLevel` method might be tricky:
* Log4j Core does not create `Logger` instances for the internal nodes
of the hierarchy (those that correspond to Java packages), so
`setLevel(Logger, Level)` would be wasteful.
* JUL on the other hand does not guarantee the uniqueness of logger
names and Tomcat and others use this to create a separate logger
hierarchy per application, which makes `setLevel(String, Level)`
trickier to implement.
* Logback is the easiest of the 3, since it just creates all the loggers
and makes sure that their names are unique.
One thing is constant: logger names are dot-separated strings and the
name of the parent logger omits the last component of the logger's name.
Piotr
Re: New API for log level manipulation
Posted by Gary Gregory <ga...@gmail.com>.
Good idea!
One way I like to think about this is what is the API I need to build a
tree view UI that shows the logger hierarchy where I can toggle levels for
one logger or one logger tree (including the root).
>set the configured level of a single logger
Some people (like me) will want to enable debug all in one go for example,
even for loggers that have not been instantiated yet.
I think we should distinguish whether this is set the level for a hierarchy
for just the one and the one only.
A simpler primitive might be get all current logger names, not sure.
Ty,
Gary
On Thu, Oct 31, 2024, 11:12 AM Piotr P. Karwasz <pi...@mailing.copernik.eu>
wrote:
> Hi all,
>
> In last few days I have encountered the problem of retrieving and
> setting the configured level of a logger twice (in Nashorn[1] and Apache
> Kafka[2]). What do you think about introducing a public API to do this
> sort of things in an implementation independent way?
>
> Currently we have `o.a.l.l.c.config.Configurator` that allows level
> manipulation, but this is a Log4j Core specific class, which only works
> with Log4j Core. We could publish a small API (e.g. called LogAdmin)
> that allows to:
>
> * list the configured level of all loggers,
>
> * get the configured level of a single logger,
>
> * set the configured level of a single logger.
>
> The advantage of this API would be to remove a lot of custom user code
> that modifies logger levels. Spring Boot has some[3], Kafka has some[4]
> and I also wrote some code of this sort for my former employer (although
> that one use JUL, since they refused to use Log4j Core and I bound
> everything to `log4j-to-jul` instead).
>
> I could probably sketch a small prototype that supports Log4j Core,
> Logback and JUL over the weekend.
>
> Piotr
>
> [1] https://github.com/openjdk/nashorn/pull/19
>
> [2] https://github.com/apache/kafka/pull/17373#discussion_r1824026165
>
> [3]
>
> https://docs.spring.io/spring-boot/api/java/org/springframework/boot/logging/LoggingSystem.html
>
> [4]
>
> https://cwiki.apache.org/confluence/display/KAFKA/KIP-495%3A+Dynamically+Adjust+Log+Levels+in+Connect
>
>
Re: New API for log level manipulation
Posted by "Piotr P. Karwasz" <pi...@mailing.copernik.eu>.
Hi Gary,
On 2.11.2024 23:14, Gary D. Gregory wrote:
> In general, talking about API signatures without (pseudo)-Javadoc is not great, there is too much wrong for miscommunication or plain old guesing.
FWIW, the JavaDoc is there in my repo[1] together with a short
README[2]. I only removed it to shorten my post to `dev@logging`.
[1]
https://github.com/ppkarwasz/logging-admin/blob/main/api/src/main/java/org/apache/logging/admin/LoggingConfigurationAdmin.java
[2] https://github.com/ppkarwasz/logging-admin/blob/main/README.adoc
>> static LoggingConfigurationAdmin getInstance(Object token,
>> ClassLoader classLoader);
>> static LoggingConfigurationAdmin getInstance(Object token);
> Why do I need those AND getLoggerContext()?
>
> I though the point of this interface was a one-stop-shop. What's LoggingConfigurationAdmin doing for me?
>
> ...
>> 2. How do we prevent users from abusing this API?
> You don't. Or, you don't deliver the JAR with your app. You can't stop users from hacking, formally or informally. Reflection is always around the corner to boot.
The `token` parameter of `getInstance` is my attempt to limit the abuse
of the API, by limiting the access to each logger context to the code
that has access to `token`. This is currently done on a per-logger
context level, which might explain the `getLoggerContext` method (used
internally).
Sure, we can not count on a bullet proof mechanism that prevents
multiple entities from modifying the logger configuration, but we could
develop a mechanism of discretionary access control. Currently Spring
Boot modifies the logging configuration even if it is running in a
Servlet Container with a single Logger Context. We could provide some
methods that inform Spring Boot of this fact.
>> 4. What should be the lifecycle of this API and where should it live?
> KISS 101: Not ANOTHER repo and version numbering system. Match Log4j and be done with it. Unless... you want to make this a new component as a sibling to Apache Logging Services Log4j: Apache Logging Services Admin.
I guess I want to make this a sibling project.
Once I have integrated all the feedback you have given into the draft, I
can create a `logging-admin` repo and we can discuss the details in PRs.
Piotr
Re: New API for log level manipulation
Posted by "Gary D. Gregory" <gg...@apache.org>.
Hi Piotr,
In general, talking about API signatures without (pseudo)-Javadoc is not great, there is too much wrong for miscommunication or plain old guesing.
> Set<String> getSupportedLevels();
It is not helpful to return a set IMO . This should be a list in a documented order from one min or max level of specificity to the other end.
> Object getLoggerContext();
Returning Object is not helpful but I don't know how to solve that or if the API is needed at all.
I'm not a fan of Optional in general FWIW.
> Optional<String> getLoggerLevel(String loggerName);
> void setLoggerLevel(String loggerName, @Nullable String level);
Since you can only set the level on a Logger, using "Logger" in the API names is superfluous.
> static LoggingConfigurationAdmin getInstance(Object token,
> ClassLoader classLoader);
> static LoggingConfigurationAdmin getInstance(Object token);
Why do I need those AND getLoggerContext()?
I though the point of this interface was a one-stop-shop. What's LoggingConfigurationAdmin doing for me?
> 1. How should this API be called? I think that the name should NOT
> contain "Log4j", which would cause the same confusion as with Log4j API.
> Users would assume that the API only works with Log4j Core.
Do you mean the Maven artifact ID? How about "logging-admin"?
> 2. How do we prevent users from abusing this API?
You don't. Or, you don't deliver the JAR with your app. You can't stop users from hacking, formally or informally. Reflection is always around the corner to boot.
> 4. What should be the lifecycle of this API and where should it live?
KISS 101: Not ANOTHER repo and version numbering system. Match Log4j and be done with it. Unless... you want to make this a new component as a sibling to Apache Logging Services Log4j: Apache Logging Services Admin.
HTH,
Gary
On 2024/11/02 19:42:15 "Piotr P. Karwasz" wrote:
> Hi all,
>
> On 31.10.2024 16:11, Piotr P. Karwasz wrote:
> > Currently we have `o.a.l.l.c.config.Configurator` that allows level
> > manipulation, but this is a Log4j Core specific class, which only
> > works with Log4j Core. We could publish a small API (e.g. called
> > LogAdmin) that allows to:
> >
> > * list the configured level of all loggers,
> >
> > * get the configured level of a single logger,
> >
> > * set the configured level of a single logger.
> >
> I have attempted a first sketch of the Apache Logging Admin API in my
> personal repo[1], which ended up having the following methods (the
> Javadoc is in the repo):
>
> public interface LoggingConfigurationAdmin {
> Object getLoggerContext();
> Set<String> getSupportedLevels();
> Map<String, Optional<String>> getLoggerLevels();
> Optional<String> getLoggerLevel(String loggerName);
> void setLoggerLevel(String loggerName, @Nullable String level);
> static LoggingConfigurationAdmin getInstance(Object token,
> ClassLoader classLoader);
> static LoggingConfigurationAdmin getInstance(Object token);
> }
>
> While the implementation is rather straightforward, there are some
> aspects we should think about:
>
> 1. How should this API be called? I think that the name should NOT
> contain "Log4j", which would cause the same confusion as with Log4j API.
> Users would assume that the API only works with Log4j Core.
>
> 2. How do we prevent users from abusing this API? I call "abuse" the
> kind of help some libraries provide to application developers, by
> "improving" the logging configuration behind the application developer's
> back. As an attempt to solve this problem I added a "security token" to
> the `getInstance` method. It works like the token used in Tomcat's JNDI
> implementation[2]: the first caller chooses the token, all subsequent
> calls to `getInstance` must provide the same token.
>
> 3. Should we provide some parameters (in the example above
> `ClassLoader`) to help implementations choose the correct logger context
> to modify? Only the Log4j API offers such a parameter to retrieve the
> appropriate `LoggerContext` and in practice the parameter is only used
> by `ClassLoaderContextSelector`, which usually infers the value of the
> parameter anyway.
>
> 4. What should be the lifecycle of this API and where should it live? I
> would prefer to keep it in a separate repo, with a separate lifecycle
> (start with version `0.1.0`, so we can make breaking changes in the
> incubating phase) and possibly use a different Maven `groupId` (e.g.
> `org.apache.logging.admin`).
>
> 5. Where should the implementations of this API live? It would be nice
> to add implementations of this API directly to the logging
> implementation artifacts (Log4j Core and Logback), but there is a JPMS
> catch: if a JPMS module exports a service, the dependency that defines
> the service type can NOT be optional! Therefore Log4j Core (and maybe
> Logback) would need to have a hard dependency on this API.
>
> What do you think?
>
> Piotr
>
> [1] https://github.com/ppkarwasz/logging-admin
>
> [2]
> https://tomcat.apache.org/tomcat-11.0-doc/api/org/apache/naming/ContextBindings.html
>
>
>
Re: New API for log level manipulation
Posted by "Piotr P. Karwasz" <pi...@mailing.copernik.eu>.
Hi Gary,
On 3.11.2024 13:23, Gary Gregory wrote:
> On Sat, Nov 2, 2024 at 11:15 PM Ralph Goers<ra...@dslextreme.com>
> wrote:
>
>> I think you missed my point. I don’t think Log4j-core, Logback or whatever
>> should need to implement anything. The point of such an API is to provide a
>> standard API that implements whatever is necessary to perform the action.
>> i.e. in Log4j’s case setLEvel would call Configurator.setLevel.
>>
> I like Ralph's POV. We define the API, and since we have our core
> implementations and bridges for other logging systems, we put the new code
> in there.
>
I merged everything into a single artifact and applied your other
suggestions[1]. If we more or less agree on how this should look like, I
can create an `apache/logging-admin` repo and work out the details in PRs.
Piotr
[1] https://github.com/ppkarwasz/logging-admin
Re: New API for log level manipulation
Posted by Gary Gregory <ga...@gmail.com>.
On Sat, Nov 2, 2024 at 11:15 PM Ralph Goers <ra...@dslextreme.com>
wrote:
>
>
> > On Nov 2, 2024, at 3:51 PM, Piotr P. Karwasz <pi...@mailing.copernik.eu>
> wrote:
> >
> > This would be the same setting as in Commons Logging, so I guess I would
> be OK with that, if we keep an extension mechanism in the API. We could
> probably split the implementation in two parts: Log4j Core can receive his
> implementation, while Logback and JUL will be integrated in an internal
> package of the API (so we can remove them later).
> >
> > Once we smooth the API (naming, naming and again naming ;-) ), I can
> ping Ceki and see if he is willing to adopt it in Logback. Potentially
> there are a lot of places, where the API could replace logging
> implementation specific code ([1] and [2]). I pretty much doubt that those
> projects have both an implementation for Log4j Core and Logback.
>
> I think you missed my point. I don’t think Log4j-core, Logback or whatever
> should need to implement anything. The point of such an API is to provide a
> standard API that implements whatever is necessary to perform the action.
> i.e. in Log4j’s case setLEvel would call Configurator.setLevel.
>
I like Ralph's POV. We define the API, and since we have our core
implementations and bridges for other logging systems, we put the new code
in there.
Gary
>
> Ralph
Re: New API for log level manipulation
Posted by Ralph Goers <ra...@dslextreme.com>.
> On Nov 2, 2024, at 3:51 PM, Piotr P. Karwasz <pi...@mailing.copernik.eu> wrote:
>
> This would be the same setting as in Commons Logging, so I guess I would be OK with that, if we keep an extension mechanism in the API. We could probably split the implementation in two parts: Log4j Core can receive his implementation, while Logback and JUL will be integrated in an internal package of the API (so we can remove them later).
>
> Once we smooth the API (naming, naming and again naming ;-) ), I can ping Ceki and see if he is willing to adopt it in Logback. Potentially there are a lot of places, where the API could replace logging implementation specific code ([1] and [2]). I pretty much doubt that those projects have both an implementation for Log4j Core and Logback.
I think you missed my point. I don’t think Log4j-core, Logback or whatever should need to implement anything. The point of such an API is to provide a standard API that implements whatever is necessary to perform the action. i.e. in Log4j’s case setLEvel would call Configurator.setLevel.
Ralph
Re: New API for log level manipulation
Posted by "Piotr P. Karwasz" <pi...@mailing.copernik.eu>.
Hi Ralph,
On 2.11.2024 22:48, Ralph Goers wrote:
> 1. getLoggerContext returns a meaningless object. The object returned only has value if you know what it is and if you do then you might as well be tied to whatever the implementation is.
> 2. You are passing in an arbitrary parameter to getInstance(). In reality you would have to know what the object should be for the implementation you are using.
These two methods are part of the "locking" mechanism and they could
certainly be refactored in a more meaningful way.
The object passed to `getInstance` can be any Java object. Its purpose
is only to prevent access to the API to all subsequent callers that do
not own the object. An example of usage:
private static final Object LOGGING_ADMIN_TOKEN = new Object();
private final LoggingConfigurationAdmin =
LoggingConfigurationAdmin.getInstance(LOGGING_ADMIN_TOKEN);
The `getLoggerContext` method is only used internally, so that
implementations can tell which "logger context" has been locked.
Internally there is a map "logger context -> security token" that is
used to prevent unauthorized access.À priori I assumed that in a
multi-user environment, each application can configure its own logging
subsystem.
> 3. The only thing of real value you are getting out of this are the “Level” methods.
Yes, this is the main purpose of the API.
> I would think this would have to live in its own repo. It should be able to support Logback, Log4j-Core, and JuL simply by checking which is present. In other words, the API and the implementation would all reside together in one jar. I know you hate doing that but anything else is overkill iMO.
This would be the same setting as in Commons Logging, so I guess I would
be OK with that, if we keep an extension mechanism in the API. We could
probably split the implementation in two parts: Log4j Core can receive
his implementation, while Logback and JUL will be integrated in an
internal package of the API (so we can remove them later).
Once we smooth the API (naming, naming and again naming ;-) ), I can
ping Ceki and see if he is willing to adopt it in Logback. Potentially
there are a lot of places, where the API could replace logging
implementation specific code ([1] and [2]). I pretty much doubt that
those projects have both an implementation for Log4j Core and Logback.
Piotr
[1] https://github.com/search?q=LogManager.getContext%28false%29&type=code
[2]
https://github.com/search?q=LoggerFactory.getILoggerFactory%28%29&type=code
Re: New API for log level manipulation
Posted by Ralph Goers <ra...@dslextreme.com>.
To be honest, I have never been a fan of this kind of interface because:
1. getLoggerContext returns a meaningless object. The object returned only has value if you know what it is and if you do then you might as well be tied to whatever the implementation is.
2. You are passing in an arbitrary parameter to getInstance(). In reality you would have to know what the object should be for the implementation you are using.
3. The only thing of real value you are getting out of this are the “Level” methods.
I would think this would have to live in its own repo. It should be able to support Logback, Log4j-Core, and JuL simply by checking which is present. In other words, the API and the implementation would all reside together in one jar. I know you hate doing that but anything else is overkill iMO.
Ralph
> On Nov 2, 2024, at 12:42 PM, Piotr P. Karwasz <pi...@mailing.copernik.eu> wrote:
>
> Hi all,
>
> On 31.10.2024 16:11, Piotr P. Karwasz wrote:
>> Currently we have `o.a.l.l.c.config.Configurator` that allows level manipulation, but this is a Log4j Core specific class, which only works with Log4j Core. We could publish a small API (e.g. called LogAdmin) that allows to:
>>
>> * list the configured level of all loggers,
>>
>> * get the configured level of a single logger,
>>
>> * set the configured level of a single logger.
>>
> I have attempted a first sketch of the Apache Logging Admin API in my personal repo[1], which ended up having the following methods (the Javadoc is in the repo):
>
> public interface LoggingConfigurationAdmin {
> Object getLoggerContext();
> Set<String> getSupportedLevels();
> Map<String, Optional<String>> getLoggerLevels();
> Optional<String> getLoggerLevel(String loggerName);
> void setLoggerLevel(String loggerName, @Nullable String level);
> static LoggingConfigurationAdmin getInstance(Object token, ClassLoader classLoader);
> static LoggingConfigurationAdmin getInstance(Object token);
> }
>
> While the implementation is rather straightforward, there are some aspects we should think about:
>
> 1. How should this API be called? I think that the name should NOT contain "Log4j", which would cause the same confusion as with Log4j API. Users would assume that the API only works with Log4j Core.
>
> 2. How do we prevent users from abusing this API? I call "abuse" the kind of help some libraries provide to application developers, by "improving" the logging configuration behind the application developer's back. As an attempt to solve this problem I added a "security token" to the `getInstance` method. It works like the token used in Tomcat's JNDI implementation[2]: the first caller chooses the token, all subsequent calls to `getInstance` must provide the same token.
>
> 3. Should we provide some parameters (in the example above `ClassLoader`) to help implementations choose the correct logger context to modify? Only the Log4j API offers such a parameter to retrieve the appropriate `LoggerContext` and in practice the parameter is only used by `ClassLoaderContextSelector`, which usually infers the value of the parameter anyway.
>
> 4. What should be the lifecycle of this API and where should it live? I would prefer to keep it in a separate repo, with a separate lifecycle (start with version `0.1.0`, so we can make breaking changes in the incubating phase) and possibly use a different Maven `groupId` (e.g. `org.apache.logging.admin`).
>
> 5. Where should the implementations of this API live? It would be nice to add implementations of this API directly to the logging implementation artifacts (Log4j Core and Logback), but there is a JPMS catch: if a JPMS module exports a service, the dependency that defines the service type can NOT be optional! Therefore Log4j Core (and maybe Logback) would need to have a hard dependency on this API.
>
> What do you think?
>
> Piotr
>
> [1] https://github.com/ppkarwasz/logging-admin
>
> [2] https://tomcat.apache.org/tomcat-11.0-doc/api/org/apache/naming/ContextBindings.html
>
>
Re: New API for log level manipulation
Posted by "Piotr P. Karwasz" <pi...@mailing.copernik.eu>.
Hi all,
On 31.10.2024 16:11, Piotr P. Karwasz wrote:
> Currently we have `o.a.l.l.c.config.Configurator` that allows level
> manipulation, but this is a Log4j Core specific class, which only
> works with Log4j Core. We could publish a small API (e.g. called
> LogAdmin) that allows to:
>
> * list the configured level of all loggers,
>
> * get the configured level of a single logger,
>
> * set the configured level of a single logger.
>
I have attempted a first sketch of the Apache Logging Admin API in my
personal repo[1], which ended up having the following methods (the
Javadoc is in the repo):
public interface LoggingConfigurationAdmin {
Object getLoggerContext();
Set<String> getSupportedLevels();
Map<String, Optional<String>> getLoggerLevels();
Optional<String> getLoggerLevel(String loggerName);
void setLoggerLevel(String loggerName, @Nullable String level);
static LoggingConfigurationAdmin getInstance(Object token,
ClassLoader classLoader);
static LoggingConfigurationAdmin getInstance(Object token);
}
While the implementation is rather straightforward, there are some
aspects we should think about:
1. How should this API be called? I think that the name should NOT
contain "Log4j", which would cause the same confusion as with Log4j API.
Users would assume that the API only works with Log4j Core.
2. How do we prevent users from abusing this API? I call "abuse" the
kind of help some libraries provide to application developers, by
"improving" the logging configuration behind the application developer's
back. As an attempt to solve this problem I added a "security token" to
the `getInstance` method. It works like the token used in Tomcat's JNDI
implementation[2]: the first caller chooses the token, all subsequent
calls to `getInstance` must provide the same token.
3. Should we provide some parameters (in the example above
`ClassLoader`) to help implementations choose the correct logger context
to modify? Only the Log4j API offers such a parameter to retrieve the
appropriate `LoggerContext` and in practice the parameter is only used
by `ClassLoaderContextSelector`, which usually infers the value of the
parameter anyway.
4. What should be the lifecycle of this API and where should it live? I
would prefer to keep it in a separate repo, with a separate lifecycle
(start with version `0.1.0`, so we can make breaking changes in the
incubating phase) and possibly use a different Maven `groupId` (e.g.
`org.apache.logging.admin`).
5. Where should the implementations of this API live? It would be nice
to add implementations of this API directly to the logging
implementation artifacts (Log4j Core and Logback), but there is a JPMS
catch: if a JPMS module exports a service, the dependency that defines
the service type can NOT be optional! Therefore Log4j Core (and maybe
Logback) would need to have a hard dependency on this API.
What do you think?
Piotr
[1] https://github.com/ppkarwasz/logging-admin
[2]
https://tomcat.apache.org/tomcat-11.0-doc/api/org/apache/naming/ContextBindings.html