[logback-dev] [JIRA] Commented: (LBGENERAL-36) Logging of java.lang.reflect.Type[] only displays the first element

Ceki Gulcu (JIRA) noreply-jira at qos.ch
Tue Nov 3 13:25:44 CET 2009


    [ http://jira.qos.ch/browse/LBGENERAL-36?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=11352#action_11352 ] 

Ceki Gulcu commented on LBGENERAL-36:
-------------------------------------

@Test
public void testIntArray() {
  Logger LOG = LoggerFactory.getLogger(LogArrayTest.class);

  int[] firstArray = {1, 2};
  LOG.debug("first array={}", firstArray);

  Integer[] secondArray = new Integer[] {1, 2};
  LOG.debug("second array={}", secondArray);
}

outputs:

12:54:16.046 [main] DEBUG - first array=[1, 2]
12:54:16.062 [main] DEBUG - second array=1

To output elements in Integer[], you would write:

 Integer[] secondArray = new Integer[] {1, 2};
 LOG.debug("second array={}", new Object[]{secondArray});

which prints 

12:54:16.062 [main] DEBUG - second array=[1, 2]

This works because "new Object[] { array }" is the general form for passing an array as a first argument. 
Incidentally, 

  int[] firstArray = {1, 2};
  LOG.debug("first array={}", new Object[] {firstArray});

would print 

12:54:16.046 [main] DEBUG - first array=[1, 2]

as well.

The real question here is why LOG.debug("first array={}", firstArray) treating the contents of firstArray as an Object of type int[] instead of Object[] of type int[]? It has to do with the java compiler's way of resolving overridden methods. For some reason, invoking debug(" ", new int[] {}) is resolved to Logger.debug(String, Object) while invoking logger.debug(" ", new Integer[]{}) resolves to Logger.debug(String, Object[]). I am sure the JLS has some explanation to justify the difference in treatment. For our purposes, let's just say "that's just the way it is" and leave it at that.

To complete the picture, let me add that when SLF4J's MessageFormatter encounters an Object of type Object[] or a primitive array, it will print the contents of the array in the same way as Arrays.toString(Object[]) or Arrays.toString(int[]).

In summary, while the above may be considered a logback or SLF4J bug, a fix would involve changing method signatures in org.slf4j.Logger which is simply not going to happen. Given that a workaround exists ("new Object[] {}) and that a fix would have serious undesirable consequences, I am marking this bug as WONT FIX.


> Logging of java.lang.reflect.Type[] only displays the first element
> -------------------------------------------------------------------
>
>                 Key: LBGENERAL-36
>                 URL: http://jira.qos.ch/browse/LBGENERAL-36
>             Project: logback-general
>          Issue Type: Bug
>    Affects Versions: 0.9.17
>         Environment: Windows 7
>            Reporter: Jeff Jensen
>            Assignee: Logback dev list
>         Attachments: logback.7z
>
>
> Logging of arrays typically works fine.  I encountered a situation where Logback only displays the first element of the array.  I created a test case to demonstrate.

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://jira.qos.ch/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        


More information about the logback-dev mailing list