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...@gmail.com> on 2022/02/20 19:54:47 UTC

Plugin attribute constraint validation

Hello,

Inspired by the StackOverflow question [1], where `log4j-1.2-api`
provided the user with a totally useless error message, I was looking
at the validation of components from the perspective of a Log4j 1.x
bridge `Builder` plugin. From what I found there are 3 ways to
validate parameters:

1. There are nice annotations that are checked if the `PluginBuilder`
is used (Log4j 1.x bridge `Builder`s don't use it),
2. The same constraints are also checked in the `Builder#build()`
methods of each component. This is not very consistent: the
`RollingFileAppender.Builder#build()` checks the `name` and
`filePattern` attribute again, but the `FileAppender.Builder#build()`
method does no such thing.
3. Some Log4j 1.x bridge builders like the
`RollingFileAppenderBuilder` and `FileAppenderBuilder` perform
constraint checks themselves before calling the
`RollingFileAppender.Builder` and `FileAppender.Builder` and return
`null` if the check fails. Without those checks the Log4j 1.x bridge
`BuilderManager` would build an `AppenderWrapper(null)` and would not
fall back to direct instantiation of the
`org.apache.log4j.RollingFileAppender` class.

 I would like to put some order in the attribute validation, but what
would be the best approach? Should each core `Builder#build()` method
call the `PluginBuilder`'s annotation-driven validation code and only
deal directly with complex constraints?

Piotr

[1] https://stackoverflow.com/q/71149004/11748454

Re: Plugin attribute constraint validation

Posted by Matt Sicker <bo...@gmail.com>.
I added the annotations a while ago to work on reducing duplication of constraint validation littered throughout the codebase. However, given that it is only invoked during plugin object construction, there’s still plenty of manual checks for things (and not much in the way of validators implemented). Some validation checks require checking more than just the value itself (like seeing if another attribute was set which is incompatible, etc.), so those have to be done manually for now.

Validation can potentially be done more consistently in the dependency injection branch I’ve been working on recently, but that’s limited to 3.x. For 2.x, it might be useful if Builder provided a default method to run all the constraint validators on its fields. The difficulty here is that there’s no easy way to add the same for static factory methods, so those are only enforced when invoked reflectively.
—
Matt Sicker

> On Feb 20, 2022, at 13:54, Piotr P. Karwasz <pi...@gmail.com> wrote:
> 
> Hello,
> 
> Inspired by the StackOverflow question [1], where `log4j-1.2-api`
> provided the user with a totally useless error message, I was looking
> at the validation of components from the perspective of a Log4j 1.x
> bridge `Builder` plugin. From what I found there are 3 ways to
> validate parameters:
> 
> 1. There are nice annotations that are checked if the `PluginBuilder`
> is used (Log4j 1.x bridge `Builder`s don't use it),
> 2. The same constraints are also checked in the `Builder#build()`
> methods of each component. This is not very consistent: the
> `RollingFileAppender.Builder#build()` checks the `name` and
> `filePattern` attribute again, but the `FileAppender.Builder#build()`
> method does no such thing.
> 3. Some Log4j 1.x bridge builders like the
> `RollingFileAppenderBuilder` and `FileAppenderBuilder` perform
> constraint checks themselves before calling the
> `RollingFileAppender.Builder` and `FileAppender.Builder` and return
> `null` if the check fails. Without those checks the Log4j 1.x bridge
> `BuilderManager` would build an `AppenderWrapper(null)` and would not
> fall back to direct instantiation of the
> `org.apache.log4j.RollingFileAppender` class.
> 
> I would like to put some order in the attribute validation, but what
> would be the best approach? Should each core `Builder#build()` method
> call the `PluginBuilder`'s annotation-driven validation code and only
> deal directly with complex constraints?
> 
> Piotr
> 
> [1] https://stackoverflow.com/q/71149004/11748454


Re: Plugin attribute constraint validation

Posted by "Piotr P. Karwasz" <pi...@gmail.com>.
Thank you both for your answers, which helped me solve the problems I
have with constraint validation.
Regarding Ralph's Log4j 1.x bridge validation:

On Sun, Feb 20, 2022 at 9:46 PM Ralph Goers <ra...@dslextreme.com> wrote:
> I suspect if we simply extract the values and pass them to the Log4j 2 component’s
> Builder that it won’t be sufficient (I believe the PluginVisitor logic actually performs
> the validation) or the error produced would end up being confusing since it would
> be coming from a Log4j 2 component, not what the configuration specified.

I agree, configuring a Log4j 1.x `RollingFileAppender` without a
`fileName` and seeing a message about `filePattern` or
`DirectFileRolloverStrategy` might be confusing. I submitted PR #768
to solve the main problem I have with the Log4j 1.x bridge validation:
I want to be able to return `null` from the `FileAppenderBuilder`
without causing an attempt to instantiate
`org.apache.log4j.FileAppender` directly. In 2.17.2 this will be
possible since Gary added the legacy appender classes, the
`FileAppender` will instantiate correctly and will fail on the first
message.

Regarding Matt's annotation-based validation:
On Sun, Feb 20, 2022 at 9:19 PM Matt Sicker <bo...@gmail.com> wrote:
> Validation can potentially be done more consistently in the dependency injection branch I’ve been working on recently, but that’s limited to 3.x. For 2.x, it might be useful if Builder provided a default method to run all the constraint validators on its fields. The difficulty here is that there’s no easy way to add the same for static factory methods, so those are only enforced when invoked reflectively.

In PR #771 I refactored your `PluginBuilder` code, so that it can be
called directly from a default `Builder#validate()` method. It's not
an ideal solution, since components created through `PluginBuilder`
will now validate the same constraints twice, but the alternative was
to add those null checks anyway in the `Builder#build` methods anyway.
This might save some typing, while waiting for Log4j 3.x.

Piotr

Re: Plugin attribute constraint validation

Posted by Ralph Goers <ra...@dslextreme.com>.
As usual you ask really good questions.

I made the builders Plugins primarily because it was an easy way to identify 
them along with the class they are emulating.  Other than that, the log4j-plugins 
builder type support really can’t be used as is because it operates on a Node 
hierarchy while we are still dealing directly with the properties or XML document.

I suspect if we simply extract the values and pass them to the Log4j 2 component’s 
Builder that it won’t be sufficient (I believe the PluginVisitor logic actually performs 
the validation) or the error produced would end up being confusing since it would 
be coming from a Log4j 2 component, not what the configuration specified. But the 
Builders I looked at don’t perform the validation. As I said, I am almost certain that 
happens in the Visitor that calls the builder methods.

So I took the path of manually doing the validation because it didn’t seem like creating 
another layer of injection was worth the effort for the small number of builders needed
 in the 1.2 emulation.

Ralph




> On Feb 20, 2022, at 12:54 PM, Piotr P. Karwasz <pi...@gmail.com> wrote:
> 
> Hello,
> 
> Inspired by the StackOverflow question [1], where `log4j-1.2-api`
> provided the user with a totally useless error message, I was looking
> at the validation of components from the perspective of a Log4j 1.x
> bridge `Builder` plugin. From what I found there are 3 ways to
> validate parameters:
> 
> 1. There are nice annotations that are checked if the `PluginBuilder`
> is used (Log4j 1.x bridge `Builder`s don't use it),
> 2. The same constraints are also checked in the `Builder#build()`
> methods of each component. This is not very consistent: the
> `RollingFileAppender.Builder#build()` checks the `name` and
> `filePattern` attribute again, but the `FileAppender.Builder#build()`
> method does no such thing.
> 3. Some Log4j 1.x bridge builders like the
> `RollingFileAppenderBuilder` and `FileAppenderBuilder` perform
> constraint checks themselves before calling the
> `RollingFileAppender.Builder` and `FileAppender.Builder` and return
> `null` if the check fails. Without those checks the Log4j 1.x bridge
> `BuilderManager` would build an `AppenderWrapper(null)` and would not
> fall back to direct instantiation of the
> `org.apache.log4j.RollingFileAppender` class.
> 
> I would like to put some order in the attribute validation, but what
> would be the best approach? Should each core `Builder#build()` method
> call the `PluginBuilder`'s annotation-driven validation code and only
> deal directly with complex constraints?
> 
> Piotr
> 
> [1] https://stackoverflow.com/q/71149004/11748454