According to the documentation, the number that goes between the brackets is the size (length) that the class name will have, but there are some details as to its value and the final result.
When value is omitted (ie you put only %class
), the full name of the class (including package names) is printed, and when the value is zero (%class{0}
), only the class name (without packages) is printed.
For example, if the class is log.test.TestLogback
(name TestLogback
, in the package log.test
), %class
prints log.test.TestLogback
, and %class{0}
prints only TestLogback
.
For the other values, if the full name of the class is larger than indicated, some package names can be abbreviated. But there is a "limit" to abbreviation, in which each package name is reduced to only one letter.
In the case, log.test.TestLogback
has 20 characters, so depending on the value used, the result can be:
| Tamanho | Resultado |
|--------------|----------------------|
| 20 ou mais | log.test.TestLogback |
| 18 ou 19 | l.test.TestLogback |
| entre 1 e 17 | l.t.TestLogback |
An important detail is that the class name is not abbreviated, only the package names as needed. There are more examples in documentation.
Of course the above values and the respective results are for this specific class name. If I change the class name to TesteLogback
(added one more e
before the L
), now the full name of the class has 21 characters. This means that %class{20}
will result in an abbreviation and the result will be l.test.TesteLogback
. To have the full name, I’d have to use %class{21}
(or any value greater than 21).
That is, the same value may or may not abbreviate packages, and will depend on the names of the classes and packages your application has.
If you want to know more about how the algorithm works, you can also look at source code (and in their respective Unit test).
Finally, for negative values, the full name of the class is printed, without abbreviations (I tested with version 1.2.3 of Logback, I do not know if other versions treat negative values differently).
Thanks @hkotsubo, I had tested with various values, but I didn’t come to this conclusion, and for some reason at the time, I didn’t get to read the documentation. Thank you
– Fabio Souza