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.Core;
26  import org.apache.logging.log4j.core.Filter;
27  import org.apache.logging.log4j.core.Layout;
28  import org.apache.logging.log4j.core.LogEvent;
29  import org.apache.logging.log4j.core.config.Configuration;
30  import org.apache.logging.log4j.core.config.plugins.Plugin;
31  import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute;
32  import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory;
33  import org.apache.logging.log4j.core.config.plugins.PluginConfiguration;
34  import org.apache.logging.log4j.core.config.plugins.PluginElement;
35  import org.apache.logging.log4j.core.config.plugins.validation.constraints.Required;
36  import org.apache.logging.log4j.core.script.AbstractScript;
37  import org.apache.logging.log4j.core.script.ScriptManager;
38  
39  @Plugin(name = "ScriptAppenderSelector", category = Core.CATEGORY_NAME, elementType = Appender.ELEMENT_TYPE, printObject = true)
40  public class ScriptAppenderSelector extends AbstractAppender {
41  
42      /**
43       * Builds an appender.
44       */
45      public static final class Builder implements org.apache.logging.log4j.core.util.Builder<Appender> {
46  
47          @PluginElement("AppenderSet")
48          @Required
49          private AppenderSet appenderSet;
50  
51          @PluginConfiguration
52          @Required
53          private Configuration configuration;
54  
55          @PluginBuilderAttribute
56          @Required
57          private String name;
58  
59          @PluginElement("Script")
60          @Required
61          private AbstractScript script;
62  
63          @Override
64          public Appender build() {
65              if (name == null) {
66                  LOGGER.error("Name missing.");
67                  return null;
68              }
69              if (script == null) {
70                  LOGGER.error("Script missing for ScriptAppenderSelector appender {}", name);
71                  return null;
72              }
73              if (appenderSet == null) {
74                  LOGGER.error("AppenderSet missing for ScriptAppenderSelector appender {}", name);
75                  return null;
76              }
77              if (configuration == null) {
78                  LOGGER.error("Configuration missing for ScriptAppenderSelector appender {}", name);
79                  return null;
80              }
81              final ScriptManager scriptManager = configuration.getScriptManager();
82              scriptManager.addScript(script);
83              final Bindings bindings = scriptManager.createBindings(script);
84              final Object object = scriptManager.execute(script.getName(), bindings);
85              final String appenderName = Objects.toString(object, null);
86              final Appender appender = appenderSet.createAppender(appenderName, name);
87              return appender;
88          }
89  
90          public AppenderSet getAppenderSet() {
91              return appenderSet;
92          }
93  
94          public Configuration getConfiguration() {
95              return configuration;
96          }
97  
98          public String getName() {
99              return name;
100         }
101 
102         public AbstractScript getScript() {
103             return script;
104         }
105 
106         public Builder withAppenderNodeSet(@SuppressWarnings("hiding") final AppenderSet appenderSet) {
107             this.appenderSet = appenderSet;
108             return this;
109         }
110 
111         public Builder withConfiguration(@SuppressWarnings("hiding") final Configuration configuration) {
112             this.configuration = configuration;
113             return this;
114         }
115 
116         public Builder withName(@SuppressWarnings("hiding") final String name) {
117             this.name = name;
118             return this;
119         }
120 
121         public Builder withScript(@SuppressWarnings("hiding") final AbstractScript script) {
122             this.script = script;
123             return this;
124         }
125 
126     }
127 
128     @PluginBuilderFactory
129     public static Builder newBuilder() {
130         return new Builder();
131     }
132 
133     private ScriptAppenderSelector(final String name, final Filter filter, final Layout<? extends Serializable> layout) {
134         super(name, filter, layout);
135     }
136 
137     @Override
138     public void append(final LogEvent event) {
139         // Do nothing: This appender is only used to discover and build another appender
140     }
141 
142 }