public class ParametersLogBuilder extends Object
Builder which allows to log Mojos parameters values. It supports basic types like arrays and collections. All other
types are treat as Object
s. The builder also provides API to use custom:
ValueToStringConverter
- responsible for converting parameter value to string representationValueSanitizer
- responsible for sanitizing Mojo parameter valueExample:
public class ExampleMojo extends AbstractMojo { public void execute() { logParameters(); ... } private void logParameters() { ParametersLogBuilder logger = new ParametersLogBuilder(getLog()); // overwrite parameter logger.append("parameter", 1); logger.append("parameter", 2); // default converter and sanitizer logger.append("booleanParameter", true); logger.append("stringArray", {"one", "two", "three"}); // custom converter logger.append("customObject", new Object(), newValueToStringConverter
() { public String convert(final Object value) { return "custom object"; } }); // simple sanitizer logger.append("booleanTrueValue", true, newSimpleSanitizer
(true, Boolean.TRUE)); logger.append("booleanFalseValue", false, newSimpleSanitizer
(false, Boolean.TRUE)); // lazy sanitizer boolean condition1 = true; logger.append("stringLazyNotExecuted", "lazy-not-executed", newLazySimpleSanitizer
(condition1, new ValueContainer() { public Object getValue() { // never executed, because condition1 is equal to true return "heavyOperation1()"; } })); boolean condition2 = false; logger.append("stringLazyExecuted", "lazy-executed", newLazySimpleSanitizer
(condition2, new ValueContainer() { public Object getValue() { // executed, because condition2 is equal to false return "heavyOperation2()"; } })); // custom sanitizer final String customSanitizerValue = null; logger.append("customSanitizerValue", customSanitizerValue, newValueSanitizer
() { private Object sanitizedValue; public boolean isValid(final Object value) { if (customSanitizerValue != null) { return true; } sanitizedValue = "heavyOperation3()"; return sanitizedValue == null; } public Object sanitize(final Object value) { return sanitizedValue; } }); // log data logger.debug(); } ... }
Output:
[DEBUG] Job parameters: [DEBUG] parameter = 2 [DEBUG] booleanParameter = true [DEBUG] stringArray = ["one", "two", "three"] [DEBUG] customObject = custom object [DEBUG] booleanTrueValue = true [DEBUG] booleanFalseValue = false (calculated: true) [DEBUG] stringLazyNotExecuted = lazy-not-executed [DEBUG] stringLazyExecuted = lazy-executed (calculated: heavyOperation2()) [DEBUG] customSanitizerValue = null (calculated: heavyOperation3())
Modifier and Type | Class and Description |
---|---|
protected static class |
ParametersLogBuilder.Container
Container which stores parameter value and associated converter and sanitizer.
|
protected static class |
ParametersLogBuilder.DebugInternalLoggerAdapter
Adapts
Log to ParametersLogBuilder.InternalLogger API which uses debug mode. |
protected static class |
ParametersLogBuilder.InfoInternalLoggerAdapter
Adapts
Log to ParametersLogBuilder.InternalLogger API which uses info mode. |
protected static interface |
ParametersLogBuilder.InternalLogger
Used to adaptation instance of the
Log to logger used in the
log(InternalLogger) method. |
Modifier and Type | Field and Description |
---|---|
protected Log |
logger
Logger used to log parameters.
|
protected Map<String,ParametersLogBuilder.Container> |
parameters
Stores parameters names and associated containers.
|
Modifier | Constructor and Description |
---|---|
|
ParametersLogBuilder(Log logger)
Constructs a new object.
|
protected |
ParametersLogBuilder(Map<String,ParametersLogBuilder.Container> parameters,
Log logger)
Constructs a new object.
|
Modifier and Type | Method and Description |
---|---|
ParametersLogBuilder |
append(String name,
Object value)
Appends a parameter with a valid value.
|
ParametersLogBuilder |
append(String name,
Object value,
ValueSanitizer sanitizer)
Appends a parameter with a valid value.
|
ParametersLogBuilder |
append(String name,
Object value,
ValueToStringConverter converter)
Appends a parameter with a valid value.
|
ParametersLogBuilder |
append(String name,
Object value,
ValueToStringConverter converter,
ValueSanitizer sanitizer)
Appends a parameter with a valid value.
|
protected List<String> |
createLines()
Creates all lines which will be logged by the logger.
|
protected List<String> |
createParametersLines()
Creates one line for every appended parameter.
|
protected String |
createParameterValue(ParametersLogBuilder.Container container)
Creates a string representation of the parameter value.
|
protected String |
createParemeterLine(String name,
ParametersLogBuilder.Container container)
Creates a line for single parameter.
|
boolean |
debug()
Logs appended parameters using debug mode.
|
boolean |
info()
Logs appended parameters using info mode.
|
protected boolean |
log(ParametersLogBuilder.InternalLogger internalLogger)
Logs appended parameters using
ParametersLogBuilder.InternalLogger . |
protected final Map<String,ParametersLogBuilder.Container> parameters
ParametersLogBuilder.Container
protected final Log logger
public ParametersLogBuilder(Log logger)
logger
- the logger used to log parameters.IllegalArgumentException
- if the logger is equal to null
.protected ParametersLogBuilder(Map<String,ParametersLogBuilder.Container> parameters, Log logger)
parameters
- the map which stores parameters names and associated containers.logger
- the logger used to log parameters.IllegalArgumentException
- if the parameters or logger is equal to null
.public ParametersLogBuilder append(String name, Object value)
name
- the parameter name.value
- the parameter value.this
builder.DefaultValueToStringConverter
,
AlwaysValidSanitizer
public ParametersLogBuilder append(String name, Object value, ValueToStringConverter converter)
name
- the parameter name.value
- the parameter value.converter
- the converter responsible for converting parameter value to string representation.this
builder.AlwaysValidSanitizer
public ParametersLogBuilder append(String name, Object value, ValueSanitizer sanitizer)
name
- the parameter name.value
- the parameter value.sanitizer
- the sanitizer responsible for sanitizing parameter value.this
builder.DefaultValueToStringConverter
public ParametersLogBuilder append(String name, Object value, ValueToStringConverter converter, ValueSanitizer sanitizer)
name
- the parameter name.value
- the parameter value.converter
- the converter responsible for converting parameter value to string representation.sanitizer
- the sanitizer responsible for sanitizing parameter value.this
builder.public boolean info()
true
whether the logger logged any data, otherwise false
.public boolean debug()
true
whether the logger logged any data, otherwise false
.protected boolean log(ParametersLogBuilder.InternalLogger internalLogger)
ParametersLogBuilder.InternalLogger
.protected List<String> createLines()
log(InternalLogger)
protected List<String> createParametersLines()
createLines()
protected String createParemeterLine(String name, ParametersLogBuilder.Container container)
name
- the parameter name.container
- the container which stores the parameter value and associated converter and sanitizer.createParametersLines()
protected String createParameterValue(ParametersLogBuilder.Container container)
container
- the container which stores the parameter value and associated converter and sanitizer.createParemeterLine(String, Container)
Copyright © 2015–2016 gabrys.biz. All rights reserved.