Skip to content

Commit a538903

Browse files
committed
Add environment lookup and regex replacement for pattern layout
git-svn-id: https://svn.apache.org/repos/asf/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers@1185987 13f79535-47bb-0310-9956-ffa450edef68
1 parent c0e509d commit a538903

File tree

15 files changed

+408
-46
lines changed

15 files changed

+408
-46
lines changed

log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/PatternProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ private RolloverFrequency calculateFrequency(String pattern) {
182182

183183
private PatternParser createPatternParser() {
184184

185-
return new PatternParser(KEY, null);
185+
return new PatternParser(null, KEY, null);
186186
}
187187

188188
private boolean patternContains(String pattern, char[] chars) {

log4j2-core/src/main/java/org/apache/logging/log4j/core/config/BaseConfiguration.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import org.apache.logging.log4j.core.filter.Filterable;
3232
import org.apache.logging.log4j.core.filter.Filters;
3333
import org.apache.logging.log4j.core.helpers.NameUtil;
34+
import org.apache.logging.log4j.core.lookup.Interpolator;
35+
import org.apache.logging.log4j.core.lookup.StrLookup;
3436
import org.apache.logging.log4j.core.lookup.StrSubstitutor;
3537
import org.apache.logging.log4j.status.StatusLogger;
3638

@@ -74,6 +76,8 @@ public class BaseConfiguration extends Filterable implements Configuration {
7476

7577
private boolean started = false;
7678

79+
private ConcurrentMap<String, Object> componentMap = new ConcurrentHashMap<String, Object>();
80+
7781
/**
7882
* Constructor.
7983
*/
@@ -112,6 +116,14 @@ public void stop() {
112116
protected void setup() {
113117
}
114118

119+
public Object getComponent(String name) {
120+
return componentMap.get(name);
121+
}
122+
123+
public void addComponent(String name, Object obj) {
124+
componentMap.putIfAbsent(name, obj);
125+
}
126+
115127
protected void doConfigure() {
116128
boolean setRoot = false;
117129
boolean setLoggers = false;
@@ -121,8 +133,17 @@ protected void doConfigure() {
121133
continue;
122134
}
123135
if (child.getName().equalsIgnoreCase("properties")) {
124-
subst = (StrSubstitutor) child.getObject();
125-
} else if (child.getName().equalsIgnoreCase("appenders")) {
136+
if (subst.getVariableResolver() == null) {
137+
subst.setVariableResolver((StrLookup) child.getObject());
138+
} else {
139+
logger.error("Properties declaration must be the first element in the configuration");
140+
}
141+
continue;
142+
}
143+
else if (subst.getVariableResolver() == null) {
144+
subst.setVariableResolver(new Interpolator(null));
145+
}
146+
if (child.getName().equalsIgnoreCase("appenders")) {
126147
appenders = (ConcurrentMap<String, Appender>) child.getObject();
127148
} else if (child.getName().equalsIgnoreCase("filters")) {
128149
setFilters((Filters) child.getObject());

log4j2-core/src/main/java/org/apache/logging/log4j/core/config/Configuration.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,8 @@ public interface Configuration extends Filtering {
5555
StrSubstitutor getSubst();
5656

5757
void createConfiguration(Node node, LogEvent event);
58+
59+
Object getComponent(String name);
60+
61+
void addComponent(String name, Object object);
5862
}

log4j2-core/src/main/java/org/apache/logging/log4j/core/config/plugins/PropertiesPlugin.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.apache.logging.log4j.core.config.Property;
2020
import org.apache.logging.log4j.core.lookup.Interpolator;
2121
import org.apache.logging.log4j.core.lookup.MapLookup;
22+
import org.apache.logging.log4j.core.lookup.StrLookup;
2223
import org.apache.logging.log4j.core.lookup.StrSubstitutor;
2324

2425
import java.util.HashMap;
@@ -31,16 +32,16 @@
3132
public class PropertiesPlugin {
3233

3334
@PluginFactory
34-
public static StrSubstitutor configureSubstitutor(@PluginElement("properties") Property[] properties) {
35+
public static StrLookup configureSubstitutor(@PluginElement("properties") Property[] properties) {
3536
if (properties == null) {
36-
return new StrSubstitutor(new Interpolator(null));
37+
return new Interpolator(null);
3738
}
3839
Map<String, String> map = new HashMap<String, String>();
3940

4041
for (Property prop : properties) {
4142
map.put(prop.getName(), prop.getValue());
4243
}
4344

44-
return new StrSubstitutor(new Interpolator(new MapLookup(map)));
45+
return new Interpolator(new MapLookup(map));
4546
}
4647
}

log4j2-core/src/main/java/org/apache/logging/log4j/core/layout/PatternLayout.java

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,17 @@
1818
package org.apache.logging.log4j.core.layout;
1919

2020
import org.apache.logging.log4j.core.LogEvent;
21+
import org.apache.logging.log4j.core.config.Configuration;
2122
import org.apache.logging.log4j.core.config.plugins.Plugin;
2223
import org.apache.logging.log4j.core.config.plugins.PluginAttr;
24+
import org.apache.logging.log4j.core.config.plugins.PluginConfiguration;
25+
import org.apache.logging.log4j.core.config.plugins.PluginElement;
2326
import org.apache.logging.log4j.core.config.plugins.PluginFactory;
2427
import org.apache.logging.log4j.core.helpers.OptionConverter;
2528
import org.apache.logging.log4j.core.pattern.LogEventPatternConverter;
2629
import org.apache.logging.log4j.core.pattern.PatternConverter;
2730
import org.apache.logging.log4j.core.pattern.PatternParser;
31+
import org.apache.logging.log4j.core.pattern.RegexReplacement;
2832

2933
import java.nio.charset.Charset;
3034
import java.util.List;
@@ -415,7 +419,7 @@ public class PatternLayout extends AbstractStringLayout {
415419
*/
416420
private List<PatternConverter> converters;
417421

418-
private static final String KEY = "Converter";
422+
public static final String KEY = "Converter";
419423

420424
/**
421425
* Conversion pattern.
@@ -427,13 +431,20 @@ public class PatternLayout extends AbstractStringLayout {
427431
*/
428432
private boolean handlesExceptions;
429433

434+
/**
435+
* The current Configuration.
436+
*/
437+
private final Configuration config;
438+
439+
private final RegexReplacement replace;
440+
430441
/**
431442
* Constructs a EnhancedPatternLayout using the DEFAULT_LAYOUT_PATTERN.
432443
* <p/>
433444
* The default pattern just produces the application supplied message.
434445
*/
435446
public PatternLayout() {
436-
this(DEFAULT_CONVERSION_PATTERN, Charset.defaultCharset());
447+
this(null, null, DEFAULT_CONVERSION_PATTERN, Charset.defaultCharset());
437448
}
438449

439450
/**
@@ -442,18 +453,30 @@ public PatternLayout() {
442453
* The default pattern just produces the application supplied message.
443454
*/
444455
public PatternLayout(final String pattern) {
445-
this(pattern, Charset.defaultCharset());
456+
this(null, null, pattern, Charset.defaultCharset());
457+
}
458+
459+
/**
460+
* Constructs a EnhancedPatternLayout using the DEFAULT_LAYOUT_PATTERN.
461+
* <p/>
462+
* The default pattern just produces the application supplied message.
463+
*/
464+
public PatternLayout(Configuration config, final String pattern) {
465+
this(config, null, pattern, Charset.defaultCharset());
446466
}
447467

448468
/**
449469
* Constructs a EnhancedPatternLayout using the supplied conversion pattern.
450470
*
451471
* @param pattern conversion pattern.
452472
*/
453-
public PatternLayout(final String pattern, final Charset charset) {
473+
public PatternLayout(Configuration config, final RegexReplacement replace, final String pattern,
474+
final Charset charset) {
454475
super(charset);
476+
this.replace = replace;
455477
this.conversionPattern = pattern;
456-
PatternParser parser = createPatternParser();
478+
this.config = config;
479+
PatternParser parser = createPatternParser(config);
457480
converters = parser.parse((pattern == null) ? DEFAULT_CONVERSION_PATTERN : pattern);
458481
handlesExceptions = parser.handlesExceptions();
459482

@@ -471,7 +494,7 @@ public void setConversionPattern(final String conversionPattern) {
471494
if (pattern == null) {
472495
return;
473496
}
474-
PatternParser parser = createPatternParser();
497+
PatternParser parser = createPatternParser(this.config);
475498
converters = parser.parse(pattern);
476499
handlesExceptions = parser.handlesExceptions();
477500
}
@@ -486,12 +509,24 @@ public String formatAs(final LogEvent event) {
486509
for (PatternConverter c : converters) {
487510
c.format(event, buf);
488511
}
489-
return buf.toString();
512+
String str = buf.toString();
513+
if (replace != null) {
514+
str = replace.format(str);
515+
}
516+
return config == null ? str : config.getSubst().replace(event, str);
490517
}
491518

492-
private PatternParser createPatternParser() {
493-
494-
return new PatternParser(KEY, LogEventPatternConverter.class);
519+
private PatternParser createPatternParser(Configuration config) {
520+
if (config == null) {
521+
return new PatternParser(config, KEY, LogEventPatternConverter.class);
522+
}
523+
PatternParser parser = (PatternParser) config.getComponent(KEY);
524+
if (parser == null) {
525+
parser = new PatternParser(config, KEY, LogEventPatternConverter.class);
526+
config.addComponent(KEY, parser);
527+
parser = (PatternParser) config.getComponent(KEY);
528+
}
529+
return parser;
495530
}
496531

497532
public String toString() {
@@ -500,6 +535,8 @@ public String toString() {
500535

501536
@PluginFactory
502537
public static PatternLayout createLayout(@PluginAttr("pattern") String pattern,
538+
@PluginConfiguration Configuration config,
539+
@PluginElement("replace") RegexReplacement replace,
503540
@PluginAttr("charset") String charset) {
504541
Charset c = Charset.isSupported("UTF-8") ? Charset.forName("UTF-8") : Charset.defaultCharset();
505542
if (charset != null) {
@@ -510,7 +547,7 @@ public static PatternLayout createLayout(@PluginAttr("pattern") String pattern,
510547
}
511548
}
512549
if (pattern != null) {
513-
return new PatternLayout(pattern, c);
550+
return new PatternLayout(config, replace, pattern, c);
514551
}
515552
logger.error("No pattern specified for PatternLayout");
516553
return null;
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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.lookup;
18+
19+
import org.apache.logging.log4j.core.LogEvent;
20+
import org.apache.logging.log4j.core.config.plugins.Plugin;
21+
22+
/**
23+
* Looks up keys from environment variables.
24+
*/
25+
@Plugin(name="env",type="Lookup")
26+
public class EnvironmentLookup implements StrLookup {
27+
28+
/**
29+
* Get the value of the environment variable.
30+
* @param key the key to be looked up, may be null
31+
* @return The value of the environment variable.
32+
*/
33+
public String lookup(String key) {
34+
return System.getenv(key);
35+
}
36+
37+
/**
38+
* Get the value of the environment variable.
39+
* @param event The current LogEvent (is ignored by this StrLookup).
40+
* @param key the key to be looked up, may be null
41+
* @return The value of the environment variable.
42+
*/
43+
public String lookup(LogEvent event, String key) {
44+
return System.getenv(key);
45+
}
46+
}

log4j2-core/src/main/java/org/apache/logging/log4j/core/lookup/Interpolator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public class Interpolator implements StrLookup {
4040
private final StrLookup defaultLookup;
4141

4242
public Interpolator(StrLookup defaultLookup) {
43-
this.defaultLookup = defaultLookup;
43+
this.defaultLookup = defaultLookup == null ? new MapLookup(new HashMap<String, String>()) : defaultLookup;
4444
PluginManager manager = new PluginManager("Lookup");
4545
manager.collectPlugins();
4646
Map<String, PluginType> plugins = manager.getPlugins();

log4j2-core/src/main/java/org/apache/logging/log4j/core/pattern/MessagePatternConverter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
* Return the event's rendered message in a StringBuffer.
2828
*/
2929
@Plugin(name="MessagePatternConverter", type="Converter")
30-
@ConverterKeys({"m", "message"})
30+
@ConverterKeys({"m", "msg", "message"})
3131
public final class MessagePatternConverter extends LogEventPatternConverter {
3232

3333
private final String format;

0 commit comments

Comments
 (0)