Value passed in Pattern tag keys for class name

Asked

Viewed 57 times

1

I am studying about Logback to implement in my company’s system, and at tag pattern I have the following:

[%d{yyyy-MM-dd HH:mm:ss.SSS}] %level [%thread] %class{0} %method %line - %message%n

I noticed that when changing the value inside the keys of %class is displayed variations on the class. I would like to better understand about these variations, and if there is a table about it. I ran Logback’s paperwork, but I couldn’t find anything. I wonder if someone could help me?

These are some examples of the outputs I noticed:

br.com.sosimple.App
App
b.c.s.App

1 answer

1


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).

  • 1

    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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.