View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.core.appender;
18  
19  import java.io.Serializable;
20  import java.util.Objects;
21  
22  import javax.script.Bindings;
23  
24  import org.apache.logging.log4j.core.Appender;
25  import org.apache.logging.log4j.core.Filter;
26  import org.apache.logging.log4j.core.Layout;
27  import org.apache.logging.log4j.core.LogEvent;
28  import org.apache.logging.log4j.core.config.Configuration;
29  import org.apache.logging.log4j.core.config.plugins.Plugin;
30  import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute;
31  import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory;
32  import org.apache.logging.log4j.core.config.plugins.PluginConfiguration;
33  import org.apache.logging.log4j.core.config.plugins.PluginElement;
34  import org.apache.logging.log4j.core.config.plugins.validation.constraints.Required;
35  import org.apache.logging.log4j.core.script.AbstractScript;
36  import org.apache.logging.log4j.core.script.ScriptManager;
37  
38  @Plugin(name = "ScriptAppenderSelector", category = "Core", elementType = Appender.ELEMENT_TYPE, printObject = true)
39  public class ScriptAppenderSelector extends AbstractAppender {
40  
41      /**
42       * Builds an appender.
43       */
44      public static final class Builder implements org.apache.logging.log4j.core.util.Builder<Appender> {
45  
46          @PluginElement("AppenderSet")
47          @Required
48          private AppenderSet appenderSet;
49  
50          @PluginConfiguration
51          @Required
52          private Configuration configuration;
53  
54          @PluginBuilderAttribute
55          @Required
56          private String name;
57  
58          @PluginElement("Script")
59          @Required
60          private AbstractScript script;
61  
62          @Override
63          public Appender build() {
64              if (name == null) {
65                  LOGGER.error("Name missing.");
66                  return null;
67              }
68              if (script == null) {
69                  LOGGER.error("Script missing for ScriptAppenderSelector appender {}", name);
70                  return null;
71              }
72              if (appenderSet == null) {
73                  LOGGER.error("AppenderSet missing for ScriptAppenderSelector appender {}", name);
74                  return null;
75              }
76              if (configuration == null) {
77                  LOGGER.error("Configuration missing for ScriptAppenderSelector appender {}", name);
78                  return null;
79              }
80              final ScriptManager scriptManager = configuration.getScriptManager();
81              scriptManager.addScript(script);
82              final Bindings bindings = scriptManager.createBindings(script);
83              final Object object = scriptManager.execute(script.getName(), bindings);
84              final String appenderName = Objects.toString(object, null);
85              final Appender appender = appenderSet.createAppender(appenderName, name);
86              return appender;
87          }
88  
89          public AppenderSet getAppenderSet() {
90              return appenderSet;
91          }
92  
93          public Configuration getConfiguration() {
94              return configuration;
95          }
96  
97          public String getName() {
98              return name;
99          }
100 
101         public AbstractScript getScript() {
102             return script;
103         }
104 
105         public Builder withAppenderNodeSet(@SuppressWarnings("hiding") final AppenderSet appenderSet) {
106             this.appenderSet = appenderSet;
107             return this;
108         }
109 
110         public Builder withConfiguration(@SuppressWarnings("hiding") final Configuration configuration) {
111             this.configuration = configuration;
112             return this;
113         }
114 
115         public Builder withName(@SuppressWarnings("hiding") final String name) {
116             this.name = name;
117             return this;
118         }
119 
120         public Builder withScript(@SuppressWarnings("hiding") final AbstractScript script) {
121             this.script = script;
122             return this;
123         }
124 
125     }
126 
127     @PluginBuilderFactory
128     public static Builder newBuilder() {
129         return new Builder();
130     }
131 
132     private ScriptAppenderSelector(final String name, final Filter filter, final Layout<? extends Serializable> layout) {
133         super(name, filter, layout);
134     }
135 
136     @Override
137     public void append(final LogEvent event) {
138         // Do nothing: This appender is only used to discover and build another appender
139     }
140 
141 }