Skip to content
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

Using custom Lookups for Virtual Properties #36

Open
wageeshagunasena opened this issue Mar 6, 2020 · 6 comments
Open

Using custom Lookups for Virtual Properties #36

wageeshagunasena opened this issue Mar 6, 2020 · 6 comments
Labels

Comments

@wageeshagunasena
Copy link

wageeshagunasena commented Mar 6, 2020

I want to use a custom lookup extended from ValueResolver, for a virtual property to resolve the value. The virtual property is given as in the following configuration.

<JacksonJsonLayout>
   <VirtualProperty name="AdditionalField" value="UnresolvedValue" dynamic="true"/>
</JacksonJsonLayout>

I see the value resolver for JacksonJsonLayout is set for Log4j2Lookup. How can I set my custom value resolver to do this?

@rfoltyns
Copy link
Owner

rfoltyns commented Mar 6, 2020

You have to extend JacksonJsonLayout.Builder. There are a few ways you can do that:

  1. You can extend JacksonJsonLayout and write your own Builder.createValueResolver() method. Sth like this maybe?
import com.fasterxml.jackson.databind.ObjectWriter;
import org.apache.logging.log4j.core.Layout;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.Node;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory;

@Plugin(name = CustomJacksonJsonLayout.PLUGIN_NAME, category = Node.CATEGORY, elementType = Layout.ELEMENT_TYPE, printObject = true)
public class CustomJacksonJsonLayout extends JacksonJsonLayout {

    public static final String PLUGIN_NAME = "CustomJacksonJsonLayout";

    protected CustomJacksonJsonLayout(Configuration config, ObjectWriter configuredWriter, ItemSourceFactory itemSourceFactory) {
        super(config, configuredWriter, itemSourceFactory);
    }

    @PluginBuilderFactory
    public static CustomJacksonJsonLayout.Builder newBuilder() {
        return new CustomJacksonJsonLayout.Builder();
    }

    public static class Builder extends JacksonJsonLayout.Builder {

        @Override
        protected ValueResolver createValueResolver() {
            return new ValueResolver() {
                @Override
                public String resolve(String unresolved) {
                    return "whatever_you_need";
                }

                @Override
                public String resolve(VirtualProperty property) {
                    return "and_more";
                }
            };
        }
    }
}
  1. Or do the similar with programmatic config (not the nicest ever in this case, but very convenient):
JacksonJsonLayout.Builder layoutBuilder = new JacksonJsonLayout.Builder() {
            @Override
            protected ValueResolver createValueResolver() {
                return new ValueResolver() {
                    @Override
                    public String resolve(String unresolved) {
                        return "whatever_you_need";
                    }

                    @Override
                    public String resolve(VirtualProperty property) {
                        return "and_more";
                    }
                };
            }
        }
        .setConfiguration(ctx.getConfiguration())
        .withVirtualProperties(
            // see Jest and HC SmokeTests for examples
        );
  1. Or (for file config) add new ValueResolver plugin, allow to set it on current JacksonJsonLayout.Builder and create a PR if you'd like to share it.

@rfoltyns rfoltyns added the howto label Mar 7, 2020
@wageeshagunasena
Copy link
Author

Sure will do and create a PR for the 3rd option.

However, I managed to do this using a custom Lookup plugin in the category of StrLookup.

@Plugin( name="lookupliteral", category = StrLookup.CATEGORY)
public class CustomLookup implements StrLookup
{
    @Override public String lookup( String key )
    {
        return getResolvedValue(key);
    }

    @Override public String lookup( LogEvent event, String key )
    {
        return lookup( key );
    }

    /**
     * my way of resolving the value
     */
    private String getResolvedValue(key)
    {
        return "resolved_value"
    }   
}

@rfoltyns
Copy link
Owner

@wageeshagunasena Custom StrLookup plugin makes sense as well! Nice one!

@rfoltyns
Copy link
Owner

@wageeshagunasena would you like to create any PRs for this?

@manushakaru
Copy link

@rfoltyns is Custom StrLookup plugin solution still works with log4j 2.16.0 or 2.17.1 ? Do you have any idea ?

@rfoltyns
Copy link
Owner

@manushakaru It does. Have a look at this branch in log42-elasticsearch-examples

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants