Class ObjectLogger

java.lang.Object
de.stefanfrings.utils.ObjectLogger

public class ObjectLogger extends Object
Print the content of any object to human readable String.

Examples how to get the content of object o:

  • String s=new ObjectLogger(o).toString();
  • String s=new ObjectLogger().toString(o);
  • String s=new ObjectLogger().addFieldNamesToHideValue("keycode").toString(o);
  • log.debug("Processing {}", new ObjectLogger(o));

The default settings are:

  • maximum nesting dept of classes: 10
  • maximum number of elements in arrays and containers: 10
  • maximum total character: 16000 (this is not a hard limit, the actual result my be a little larger)
  • maximum size of strings: 80
  • hide values of: password
  • hide totally: serialVersionUID, log and logger
  • fields with value=null are skipped
Author:
Stefan Frings, http://stefanfrings.de/javautils
  • Constructor Details

    • ObjectLogger

      public ObjectLogger()
      Call this constructor, then optionally call some setters to configure behaviour, then toString(o) which generates the output.
    • ObjectLogger

      public ObjectLogger(Object o)
      Call this constructor, then optionally call some setters to configure behaviour, then toString() which generates the output.
      Parameters:
      o - The object to log
  • Method Details

    • setMaxNestingDepth

      public ObjectLogger setMaxNestingDepth(int maxNestingDepth)
      Set the maximum nesting depth of sub-objects.
      Parameters:
      maxNestingDepth - Maximum nesting depth of sub-objects.
      Returns:
      A reference to this object.
    • setMaxListSize

      public ObjectLogger setMaxListSize(int maxListSize)
      Set the maximum number of elements in arrays and lists.
      Parameters:
      maxListSize - Maximum number of elements in arrays and lists.
      Returns:
      A reference to this object.
    • setMaxTotalCharacters

      public ObjectLogger setMaxTotalCharacters(int maxTotalCharacters)
      Set the maximum number of characters in the result.
      Parameters:
      maxTotalCharacters - Maximum number of characters in the result.
      Returns:
      A reference to this object.
    • setMaxStringSize

      public ObjectLogger setMaxStringSize(int maxStringSize)
      Set the maximum number of characters per String
      Parameters:
      maxStringSize - Maximum number of characters per String
      Returns:
      A reference to this object.
    • setShowNull

      public ObjectLogger setShowNull(boolean showNull)
      Configure whether fields with value null shall appear in the output.
      Parameters:
      showNull - Boolean flag, true enables the output.
      Returns:
      A reference to this object.
    • addFieldNamesToHideValue

      public ObjectLogger addFieldNamesToHideValue(String... fieldNames)
      Add field names whose values shall be hidden.
      Parameters:
      fieldNames - Maximum number of characters per String
      Returns:
      A reference to this object.
    • addFieldNamesToHideValue

      public ObjectLogger addFieldNamesToHideValue(Collection<String> fieldNames)
      Add field names whose values shall be hidden.
      Parameters:
      fieldNames - Maximum number of characters per String
      Returns:
      A reference to this object.
    • addFieldNamesToHideTotally

      public ObjectLogger addFieldNamesToHideTotally(String... fieldNames)
      Add field names which shall be hidden totally.
      Parameters:
      fieldNames - Maximum number of characters per String
      Returns:
      A reference to this object.
    • addFieldNamesToHideTotally

      public ObjectLogger addFieldNamesToHideTotally(Collection<String> fieldNames)
      Add field names which shall be hidden totally.
      Parameters:
      fieldNames - Maximum number of characters per String
      Returns:
      A reference to this object.
    • toString

      public String toString(Object o)
      Returns the content of the object as human readable string.
      Parameters:
      o - The object to convert.
      Returns:
      The human readable String representation of the object.
    • toString

      public String toString()
      Returns the content of the object which was passed to the construtor as human readable string.
      Overrides:
      toString in class Object
      Returns:
      The human readable String representation of the object.