| 1 | --- |
| 2 | layout: docs |
| 3 | title: Debugging |
| 4 | prev_section: accessible-mutable |
| 5 | next_section: disabling-features |
| 6 | permalink: /docs/debugging/ |
| 7 | --- |
| 8 | |
| 9 | There are some debugging facilities that are available in Properties files that |
| 10 | we wanted to keep in the OWNER API. |
| 11 | |
| 12 | The toString() method |
| 13 | --------------------- |
| 14 | |
| 15 | The `toString()` method is helpful to see the content of a Config object using a |
| 16 | log statement: |
| 17 | |
| 18 | ```java |
| 19 | interface MyConfig extends Config { |
| 20 | @Key("max.threads") |
| 21 | @DefaultValue("25") |
| 22 | int maxThreads(); |
| 23 | |
| 24 | @Key("max.folders") |
| 25 | @DefaultValue("99") |
| 26 | int maxFolders(); |
| 27 | |
| 28 | @Key("default.name") |
| 29 | @DefaultValue("untitled") |
| 30 | String defaultName(); |
| 31 | } |
| 32 | |
| 33 | public static void main(String[] args) { |
| 34 | MyConfig cfg = ConfigFactory.create(MyConfig.class); |
| 35 | System.out.println("cfg = " + cfg); |
| 36 | // output will be: |
| 37 | // "cfg = {default.name=untitled, max.folders=99, max.threads=25}" |
| 38 | } |
| 39 | ``` |
| 40 | |
| 41 | The list() methods |
| 42 | ------------------ |
| 43 | |
| 44 | In your *mapping interfaces* you can optionally define one of the following |
| 45 | methods that may be convenient for debugging: |
| 46 | |
| 47 | ```java |
| 48 | void list(PrintStream out); |
| 49 | void list(PrintWriter out); |
| 50 | ``` |
| 51 | |
| 52 | Those two methods were available in Java [Properties][1] to help the |
| 53 | debugging process. |
| 54 | You can implement [Accessible][2] that defines the above methods, |
| 55 | or just add them manually. |
| 56 | |
| 57 | [1]: http://docs.oracle.com/javase/7/docs/api/java/util/Properties.html |
| 58 | [2]: http://owner.aeonbits.org/apidocs/latest/org/aeonbits/owner/Accessible.html |
| 59 | |
| 60 | You can use them to print the resolved properties (and eventual overrides that |
| 61 | may occur when using the `LoadType.MERGE`): |
| 62 | |
| 63 | ```java |
| 64 | public interface SampleConfig extends Config { |
| 65 | @Key("server.http.port") |
| 66 | @DefaultValue("80") |
| 67 | int httpPort(); |
| 68 | |
| 69 | void list(PrintStream out); // manually defined |
| 70 | } |
| 71 | |
| 72 | ServerConfig cfg = ConfigFactory.create(ServerConfig.class); |
| 73 | cfg.list(System.out); |
| 74 | ``` |
| 75 | |
| 76 | You can also do the same implementing the Accessible interface, that declares |
| 77 | the `list()` methods for you: |
| 78 | |
| 79 | ```java |
| 80 | public interface SampleConfig extends Accessible { |
| 81 | @Key("server.http.port") |
| 82 | @DefaultValue("80") |
| 83 | int httpPort(); |
| 84 | } |
| 85 | |
| 86 | ServerConfig cfg = ConfigFactory.create(ServerConfig.class); |
| 87 | cfg.list(System.out); // list() is defined in Accessible interface |
| 88 | ``` |