Class ParametersLogBuilder


  • 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 Objects. The builder also provides API to use custom:

    Example:

     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(), new ValueToStringConverter() {
    
                 public String convert(final Object value) {
                     return "custom object";
                 }
             });
    
             // simple sanitizer
             logger.append("booleanTrueValue", true, new SimpleSanitizer(true, Boolean.TRUE));
             logger.append("booleanFalseValue", false, new SimpleSanitizer(false, Boolean.TRUE));
    
             // lazy sanitizer
             boolean condition1 = true;
             logger.append("stringLazyNotExecuted", "lazy-not-executed", new LazySimpleSanitizer(condition1, () -> {
                 // never executed, because condition1 is equal to true 
                 return "heavyOperation1()";
             }));
             boolean condition2 = false;
             logger.append("stringLazyExecuted", "lazy-executed", new LazySimpleSanitizer(condition2, () -> {
                 // executed, because condition2 is equal to false 
                 return "heavyOperation2()";
             }));
    
             // custom sanitizer
             final String customSanitizerValue = null;
             logger.append("customSanitizerValue", customSanitizerValue, new ValueSanitizer() {
    
                 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())
     
    Since:
    1.3.0
    • Constructor Detail

      • ParametersLogBuilder

        public ParametersLogBuilder​(Log logger)
        Constructs a new object.
        Parameters:
        logger - the logger used to log parameters.
        Throws:
        IllegalArgumentException - if the logger is equal to null.
        Since:
        1.3.0
      • ParametersLogBuilder

        protected ParametersLogBuilder​(Map<String,​ParametersLogBuilder.Container> parameters,
                                       Log logger)
        Constructs a new object.
        Parameters:
        parameters - the map which stores parameters names and associated containers.
        logger - the logger used to log parameters.
        Throws:
        IllegalArgumentException - if the parameters or logger is equal to null.
        Since:
        1.3.0
    • Method Detail

      • append

        public ParametersLogBuilder append​(String name,
                                           Object value,
                                           ValueToStringConverter converter)
        Appends a parameter with a valid value. If parameter has been appended before, then it removes its previous value and adds a new.
        Parameters:
        name - the parameter name.
        value - the parameter value.
        converter - the converter responsible for converting parameter value to string representation.
        Returns:
        this builder.
        Since:
        1.3.0
        See Also:
        AlwaysValidSanitizer
      • append

        public ParametersLogBuilder append​(String name,
                                           Object value,
                                           ValueSanitizer sanitizer)
        Appends a parameter with a valid value. If parameter has been appended before, then it removes its previous value and adds a new.
        Parameters:
        name - the parameter name.
        value - the parameter value.
        sanitizer - the sanitizer responsible for sanitizing parameter value.
        Returns:
        this builder.
        Since:
        1.3.0
        See Also:
        DefaultValueToStringConverter
      • append

        public ParametersLogBuilder append​(String name,
                                           Object value,
                                           ValueToStringConverter converter,
                                           ValueSanitizer sanitizer)
        Appends a parameter with a valid value. If parameter has been appended before, then it removes its previous value and adds a new.
        Parameters:
        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.
        Returns:
        this builder.
        Since:
        1.3.0
      • info

        public boolean info()
        Logs appended parameters using info mode.
        Returns:
        true whether the logger logged any data, otherwise false.
        Since:
        1.3.0
      • debug

        public boolean debug()
        Logs appended parameters using debug mode.
        Returns:
        true whether the logger logged any data, otherwise false.
        Since:
        1.3.0
      • createLines

        protected List<String> createLines()
        Creates all lines which will be logged by the logger.
        Returns:
        the lines which will be logged by the logger.
        Since:
        1.3.0
        See Also:
        log(InternalLogger)
      • createParametersLines

        protected List<String> createParametersLines()
        Creates one line for every appended parameter.
        Returns:
        the lines with parameters.
        Since:
        1.3.0
        See Also:
        createLines()
      • createParemeterLine

        protected String createParemeterLine​(String name,
                                             ParametersLogBuilder.Container container)
        Creates a line for single parameter.
        Parameters:
        name - the parameter name.
        container - the container which stores the parameter value and associated converter and sanitizer.
        Returns:
        the line with the parameter name and value.
        Since:
        1.3.0
        See Also:
        createParametersLines()
      • createParameterValue

        protected String createParameterValue​(ParametersLogBuilder.Container container)
        Creates a string representation of the parameter value.
        Parameters:
        container - the container which stores the parameter value and associated converter and sanitizer.
        Returns:
        the string representation of the parameter value.
        Since:
        1.3.0
        See Also:
        createParemeterLine(String, Container)