Skip to content
Fabrice Bacchella edited this page Jul 11, 2023 · 8 revisions

Loghub uses extended pattern. They use the same format as standard printf pattern, but used in a slightly different way.

A pattern is usually writer in the format ${variable%format:locale}. variable is a variable. Depending on the use case, it can be a field in an event or any other variable, it should be detailed in the documentation. format is a printf format, it is not given, it defaults to %s and will apply the .toString() to any object given ; the date format (%t or %T) can take a time zone in a pair of <...>. An optional locale can also be given, and apply to every locale dependant variation of the result. It also defines the custom %j format that output the argument as a json object.

The date formatting use the following conversion from java.util.Formatter :

The following conversion characters are used for formatting times:

  • %tH, hour of the day for the 24-hour clock, formatted as two digits with a leading zero as necessary i.e. 00 - 23.
  • %tI, hour for the 12-hour clock, formatted as two digits with a leading zero as necessary, i.e. 01 - 12.
  • %tk, hour of the day for the 24-hour clock, i.e. 0 - 23.
  • %tl, hour for the 12-hour clock, i.e. 1 - 12.
  • %tM, minute within the hour formatted as two digits with a leading zero as necessary, i.e. 00 - 59.
  • %tS, seconds within the minute, formatted as two digits with a leading zero as necessary, i.e. 00 - 60 ("60" is a special value required to support leap seconds).
  • %tL, millisecond within the second formatted as three digits with leading zeros as necessary, i.e. 000 - 999.
  • %tN, nanosecond within the second, formatted as nine digits with leading zeros as necessary, i.e. 000000000 - 999999999.
  • %tp, locale-specific morning or afternoon marker in lower case, e.g."am" or "pm". Use of the conversion prefix 'T' forces this output to upper case.
  • %tz, RFC 822 style numeric time zone offset from GMT, e.g. -0800. This value will be adjusted as necessary for Daylight Saving Time.
  • %tZ, a string representing the abbreviation for the time zone. This value will be adjusted as necessary for Daylight Saving Time. For long, Long, and Date the time zone used is the default time zone for this instance of the Java virtual machine. The Formatter's locale will supersede the locale of the argument (if any).
  • %ts, seconds since the beginning of the epoch starting at 1 January 1970 00:00:00 UTC, i.e. Long.MIN_VALUE/1000 to Long.MAX_VALUE/1000.
  • %tQ, milliseconds since the beginning of the epoch starting at 1 January 1970 00:00:00 UTC, i.e. Long.MIN_VALUE to Long.MAX_VALUE.

The following conversion characters are used for formatting dates:

  • %tB, locale-specific full month name, e.g. "January", "February".
  • %tb, locale-specific abbreviated month name, e.g. "Jan", "Feb".
  • %th, same as 'b'.
  • %tA, locale-specific full name of the day of the week, e.g. "Sunday", "Monday"
  • %ta, locale-specific short name of the day of the week, e.g. "Sun", "Mon"
  • %tC, four-digit year divided by 100, formatted as two digits with leading zero as necessary, i.e. 00 - 99
  • %tY, year, formatted as at least four digits with leading zeros as necessary, e.g. 0092 equals 92 CE for the Gregorian calendar.
  • %ty, last two digits of the year, formatted with leading zeros as necessary, i.e. 00 - 99.
  • %tj, day of year, formatted as three digits with leading zeros as necessary, e.g. 001 - 366 for the Gregorian calendar.
  • %tm, month, formatted as two digits with leading zeros as necessary, i.e. 01 - 13.
  • %td, day of month, formatted as two digits with leading zeros as necessary, i.e. 01 - 31
  • %te, day of month, formatted as two digits, i.e. 1 - 31.
  • %tV, from strftime(3), week number of the year (Monday as the first day of the week) as a decimal number (01-53). If the week containing January 1 has four or more days in the new year, then it is week 1; otherwise it is the last week of the previous year, and the next week is week 1.

The following conversion characters are used for formatting common date/time compositions.

  • %tR, time formatted for the 24-hour clock as "%tH:%tM"
  • %tT, time formatted for the 24-hour clock as "%tH:%tM:%tS".
  • %tr, time formatted for the 12-hour clock as "%tI:%tM:%tS %Tp". The location of the morning or afternoon marker ('%Tp') may be locale-dependent.
  • %tD, date formatted as "%tm/%td/%ty".
  • %tF, ISO 8601 complete date formatted as "%tY-%tm-%td".
  • %tc, date and time formatted as "%ta %tb %td %tT %tZ %tY", e.g. "Sun Jul 20 16:17:00 EDT 1969".

Some example formatting :

${var%t<Europe/Paris>H}
${var%s:fr}

It can also format list of arguments, then variable will be a position in the list:

${#1%s}

If the field indicator (being a variable or an index) is missing or is the symbol '.', it formats the whole arguments, what ever it's type is:

${.%s}
${%s}

They are compiled, so they are much faster than usual printf formatting and can be used in tight loop very efficiently.

The implementation don't use other components of LogHub, so it can be used alone. It can be found at https://github.com/fbacchella/LogHub/blob/master/src/main/java/loghub/VarFormatter.java.

Clone this wiki locally