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

Bug Fix: Script Resolution fix #3394

Open
wants to merge 12 commits into
base: 2.x
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,30 @@ public void setup() {
rootLogger.getAttributes().put("level", "INFO");
loggers.getChildren().add(rootLogger);

if (map != null) {
// Add Properties Node only if the map is not null and has entries
if (map != null && !map.isEmpty()) {
final Node properties = newNode(rootNode, "Properties");
rootNode.getChildren().add(properties);

for (final Entry<String, String> entry : map.entrySet()) {
// Create Property node with "name" and "value" attributes
final Node property = newNode(properties, "Property");
property.getAttributes().put("name", entry.getKey());
property.getAttributes().put("value", entry.getValue());
properties.getChildren().add(property);
}
}

// Add a Scripts Node if required (Example for flexibility in testing)
final Node scripts = newNode(rootNode, "Scripts");
rootNode.getChildren().add(scripts);

// Add sample script node for testing purposes
final Node script = newNode(scripts, "Script");
script.getAttributes().put("name", "TestScript");
script.getAttributes().put("language", "JavaScript");
script.getAttributes().put("scriptText", "print('Hello, Log4j!');");
scripts.getChildren().add(script);
Suvrat1629 marked this conversation as resolved.
Show resolved Hide resolved
}

private Node newNode(final Node parent, final String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<Appenders>
<List name="List">
<PatternLayout pattern="[%-5level] %c{1.} %msg%n"/>

</List>
</Appenders>
<Loggers>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,7 @@ protected void preConfigure(final Node node) {
/**
* Process conditions by evaluating them and including the children of conditions that are true
* and discarding those that are not.
*
* @param node The node to evaluate.
*/
protected void processConditionals(final Node node) {
Expand Down Expand Up @@ -625,6 +626,7 @@ protected void processConditionals(final Node node) {
/**
* Handle Select nodes. This finds the first child condition that returns true and attaches its children
* to the parent of the Select Node. Other Nodes are discarded.
*
* @param selectNode The Select Node.
* @param type The PluginType of the Select Node.
* @return The list of Nodes to be added to the parent.
Expand Down Expand Up @@ -687,28 +689,35 @@ protected void doConfigure() {
configurationStrSubstitutor.setVariableResolver(interpolator);
}

for (final Node node : rootNode.getChildren()) {
if ("Scripts".equalsIgnoreCase(node.getName())) {
createConfiguration(node, null);
if (node.getObject() != null) {
for (final AbstractScript script : node.getObject(AbstractScript[].class)) {
if (script instanceof ScriptRef) {
LOGGER.error(
"Script reference to {} not added. Scripts definition cannot contain script references",
script.getName());
} else if (scriptManager != null) {
scriptManager.addScript(script);
}
}
}
break;
}
}

boolean setLoggers = false;
boolean setRoot = false;
for (final Node child : rootNode.getChildren()) {
if ("Properties".equalsIgnoreCase(child.getName())) {
// We already used this node
continue;
if ("Properties".equalsIgnoreCase(child.getName()) || "Scripts".equalsIgnoreCase(child.getName())) {
continue; // Skip already processed nodes
}
createConfiguration(child, null);
if (child.getObject() == null) {
continue;
}
if ("Scripts".equalsIgnoreCase(child.getName())) {
for (final AbstractScript script : child.getObject(AbstractScript[].class)) {
if (script instanceof ScriptRef) {
LOGGER.error(
"Script reference to {} not added. Scripts definition cannot contain script references",
script.getName());
} else if (scriptManager != null) {
scriptManager.addScript(script);
}
}
} else if ("Appenders".equalsIgnoreCase(child.getName())) {
if ("Appenders".equalsIgnoreCase(child.getName())) {
appenders = child.getObject();
} else if (child.isInstanceOf(Filter.class)) {
addFilter(child.getObject(Filter.class));
Expand Down Expand Up @@ -895,7 +904,8 @@ public ReliabilityStrategy getReliabilityStrategy(final LoggerConfig loggerConfi
* <p>
* Note: This method is not used when configuring via configuration. It is primarily used by unit tests.
* </p>
* @param logger The Logger the Appender will be associated with.
*
* @param logger The Logger the Appender will be associated with.
* @param appender The Appender.
*/
@Override
Expand Down Expand Up @@ -925,6 +935,7 @@ public synchronized void addLoggerAppender(
* <p>
* Note: This method is not used when configuring via configuration. It is primarily used by unit tests.
* </p>
*
* @param logger The Logger the Footer will be associated with.
* @param filter The Filter.
*/
Expand All @@ -950,7 +961,8 @@ public synchronized void addLoggerFilter(final org.apache.logging.log4j.core.Log
* <p>
* Note: This method is not used when configuring via configuration. It is primarily used by unit tests.
* </p>
* @param logger The Logger the Appender will be associated with.
*
* @param logger The Logger the Appender will be associated with.
* @param additive True if the LoggerConfig should be additive, false otherwise.
*/
@Override
Expand Down Expand Up @@ -1101,6 +1113,7 @@ public void createConfiguration(final Node node, final LogEvent event) {

/**
* This method is used by Arbiters to create specific children.
*
* @param type The PluginType.
* @param node The Node.
* @return The created object or null;
Expand Down Expand Up @@ -1144,8 +1157,9 @@ public Object createPluginObject(final PluginType<?> type, final Node node) {
* Although the happy path works, more work still needs to be done to log incorrect parameters. These will generally
* result in unhelpful InvocationTargetExceptions.
* </p>
* @param type the type of plugin to create.
* @param node the corresponding configuration node for this plugin to create.
*
* @param type the type of plugin to create.
* @param node the corresponding configuration node for this plugin to create.
* @param event the LogEvent that spurred the creation of this plugin
* @return the created plugin object or {@code null} if there was an error setting it up.
* @see org.apache.logging.log4j.core.config.plugins.util.PluginBuilder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
import org.apache.logging.log4j.status.StatusLogger;

/**
* Returns the onMatch result if the script returns True and returns the onMismatch value otherwise.
* Returns the onMatch result if the script returns True and returns the
* onMismatch value otherwise.
*/
@Plugin(name = "ScriptFilter", category = Node.CATEGORY, elementType = Filter.ELEMENT_TYPE, printObject = true)
public final class ScriptFilter extends AbstractFilter {
Expand Down Expand Up @@ -122,10 +123,12 @@ public String toString() {

/**
* Creates the ScriptFilter.
* @param script The script to run. The script must return a boolean value. Either script or scriptFile must be
* provided.
* @param match The action to take if a match occurs.
* @param mismatch The action to take if no match occurs.
*
* @param script The script to run. The script must return a boolean
* value. Either script or scriptFile must be
* provided.
* @param match The action to take if a match occurs.
* @param mismatch The action to take if no match occurs.
* @param configuration the configuration
* @return A ScriptFilter.
*/
Expand Down
Loading