In case you were wondering, layouts have nothing to do withlarge estates in Florida. Layouts are logback componentsresponsible for transforming an incoming event into a String. Theformat() method in the Layoutinterface takes an object that represents an event (of any type)and returns a String. A synopsis of the Layoutinterface is shown below.
This interface is rather simple and yet is sufficient for manyformatting needs. The Texan developer from Texas, whom you mightknow from Joseph Heller's Catch-22, might exclaim: itjust takes five methods to implement a layout!!?
Date Format Creator 1.3
Note that MySampleLayout extends LayoutBase. This class manages state common toall layout instances, such as whether the layout is started orstopped, header, footer and content type data. It allows thedeveloper to concentrate on the formatting expected from his/herLayout. Note that the LayoutBase classis generic. In its class declaration, MySampleLayoutextends LayoutBase.
The doLayout(ILoggingEvent event) method, i.e. theonly method in MySampleLayout, begins byinstantiating a StringBuffer. It proceeds by addingvarious fields of the event parameter. The Texan from Texas wascareful to print the formatted form of the message. This issignificant if one or more parameters were passed along withthe logging request.
The conversion pattern of PatternLayout is closely related to the conversion pattern of the printf() function in the C programming language. A conversion pattern is composed of literal text and format control expressions called conversion specifiers. You are free to insert any literal text within the conversion pattern. Each conversion specifier starts with a percent sign '%' and is followed by optional format modifiers, a conversion word and optional parameters between braces. The conversion word controls the data field to convert, e.g. logger name, level, date or thread name. The format modifiers control field width, padding, and left or right justification.
Used to output the date of the logging event. The date conversion word admits a pattern string as a parameter. The pattern syntax is compatible with the format accepted by java.text.SimpleDateFormat (in logback 1.2.x) and java.time.format.DateTimeFormatter (in logback 1.3.x).
The second parameter specifies a timezone. For example, the pattern '%dateHH:mm:ss.SSS, Australia/Perth' would print the time in the time zone of Perth, Australia, the most isolated city on Earth. Note that in the absence of the timezone parameter, the default timezone of the host Java platform is used. If the specified timezone identifier is unknown or misspelled, the GMT timezone is assumed as dictated by the TimeZone.getTimeZone(String) method specification.
Since 1.3.6/1.4.6 The third parameter specifies the locale. For example, writing '%dateHH:mm:ss.SSS, Australia/Perth, en-AU' would print the date in the time zone of Perth, Australia, using the english australian locale. If the locale parameter is absent, then the system's default locale is used.
common error Given that the comma ',' character is interpreted as the parameter separator, the pattern HH:mm:ss,SSS will be interpreted as the pattern HM:mm:ss and the timezone SSS. If you wish to include a comma in your date pattern, then simply enclose the pattern between single or double quotes. For example, %date"HH:mm:ss,SSS" or %date'HH:mm:ss,SSS'.
The location information depends on the JVMimplementation but usually consists of the fully qualifiedname of the calling method followed by the caller's source,the file name and line number between parentheses.
If you do not specify %throwable or another throwable-related conversion word in the conversion pattern, PatternLayout will automatically add it as the last conversion word, on account of the importance of stack trace information. The $nopex conversion word can be substituted for %throwable, if you do not wish stack trace information to be displayed. See also the %nopex conversion word.
At the end of each stack frame of the exception, a string consisting of the jar file containing the relevant class followed by the "Implementation-Version" as found in that jar's manifest will be added. This innovative technique was originally suggested by James Strachan. If the information is uncertain, then the class packaging data will be preceded by a tilde, i.e. the '' character.
Logback goes to great lengths to ensure that the class packaging information it displays is correct, even in arbitrarily complex class loader hierarchies. However, when it is unable to guarantee the absolute correctness of the information, then it will prefix the data with a tilde, i.e. the '' character. Thus, it is theoretically possible for the printed class packaging information to differ from the real class packaging information. So, in the above example, given that packaging data for the Wombat class is preceded by a tilde, it is possible that the correct packaging data is in reality [wombat.jar:1.7].
The %rootException converter admits the same optional parameters as the %xException converter described above, including depth and evaluators. It outputs also packaging information. In short, %rootException is very similar to %xException, only the order of exception output is reversed.
In most cases literals naturally contain spaces or other delimiting characters so that they are not confused with conversion words. For example, the pattern "%level [%thread] - %message%n" contains the string literals " [" and "] - ". However, if a character which can be part of a java identifier immediately follows a conversion word, logback's pattern parser will be fooled into thinking that the literal is part of the conversion word. For example, the pattern "%date%nHello" will be interpreted as two conversion words %date and %nHello and since %nHello is not a known conversion word, logback will output %PARSER_ERROR[nHello] for %nHello. If you wish the string literal "Hello" to immediately separate %n and Hello, pass an empty argument list to %n. For example, "%date%nHello" will be interpreted as %date followed by %n followed by the literal "Hello".
The first optional format modifier is the leftjustification flag which is just the minus (-)character. Then comes the optional minimum field widthmodifier. This is a decimal constant that represents the minimumnumber of characters to output. If the data item contains fewercharacters, it is padded on either the left or the right until theminimum width is reached. The default is to pad on the left (rightjustify) but you can specify right padding with the leftjustification flag. The padding character is space. If the dataitem is larger than the minimum field width, the field is expandedto accommodate the data. The value is never truncated.
The table below list examples for format modifiertruncation. Please note that the square brackets, i.e the pair of "[]"characters, are not part of the output. They are used to delimitthe width of output.
Instead of printing TRACE, DEBUG, WARN, INFO or ERROR for the level, you may want to print just T, D, W, I and E. You could write a custom converter for this purpose, or simply make use of format modifiers (just discussed) to shorten the level value to a single character. The appropriate conversion specifier would be "%.-1level".
Let us review an example involving a EventEvaluator. The next configuration file outputs the logging events to the console, displaying date, thread, level, message and caller data. Given that extracting the caller data of a logging event is on the expensive side, we will do so only when the logging request originates from a specific logger, and when the message contains a certain string. Thus, we make sure that only specific logging requests will have their caller information generated and displayed. In other cases, where the caller data is superfluous, we will not penalize application performance.
Notice how the second log statement has no stack trace. We effectively suppressed the stack trace for the TextException. The text between square brackets at the end of each stack trace line is packaging information discussed earlier.
First, you must extend the ClassicConverterclass. ClassicConverter objects are responsible forextracting information out of ILoggingEvent instancesand producing a String. For example,LoggerConverter, the converter underlying the%logger conversion word, extracts the name of the logger fromILoggingEvent and returns it as a String. It mightabbreviate the logger name in the process.
The content of table columns are specified with the help of aconversion pattern. See PatternLayout fordocumentation on conversion patterns. As such, you have fullcontrol over the contents and format of the table. You can selectand display any combination of convertersPatternLayout knows about.
XMLLayout (part of logback-classic) generates output in a log4j.dtd compliant format to interoperate with tools such as Chainsaw and Vigilog capable of processing files generated by log4j's XMLLayout.
As the original XMLLayout in log4j version 1.2.15, XMLLayout in logback-classic takes two boolean properties, locationInfo and properties. Setting locationInfo to true enables the inclusion of location info (caller data) in each event. Setting properties to true enables the inclusion of MDC information. Both options are set to false by default.
PatternLayout in logback-access can be configuredin much the same way as its classic counterpart. However itfeatures additional conversion specifiers suited for loggingparticular bits of information available only in HTTP servletrequests and HTTP servlet responses.
Outputs the date of the logging event. The date conversion specifier may be followed by a set of braces containing a date and time pattern strings used by java.text.SimpleDateFormat. ISO8601 is also a valid value.
For example, %tHH:mm:ss,SSS or%tdd MMM yyyy ;HH:mm:ss,SSS.If no date format specifier is given then theCommon Log Format date format is assumed, that is: %tdd/MMM/yyyy:HH:mm:ss Z 2ff7e9595c
Комментарии