Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the main differences between EnhancedPatternLayout and PatternLayout?

Tags:

java

log4j

While checking the javadoc for PatternLayout, I noticed that it recommends using EnhancedPatternLayout instead. However, it seems to do pretty much the same thing.

What are the main differences, especially the ones I need to be aware of?

I'm also wondering why they made a separate class rather than enhancing the original class. Any syntax differences?

like image 905
aditsu quit because SE is EVIL Avatar asked Feb 28 '12 12:02

aditsu quit because SE is EVIL


People also ask

What is PatternLayout in Log4j2?

PatternLayout to format your logging information. The PatternLayout class extends the abstract org. apache. log4j. Layout class and overrides the format() method to structure the logging information according to a supplied pattern.

What is %P in log4j?

1.2 Log4j Conversion Pattern The percentage sign ( % ) is a prefix for any conversion characters. -5p is the format modifier for the conversion character p (i.e. priority). This justifies the priority name with a minimum of 5 characters.

What is ConversionPattern in log4j?

To recall, a conversion pattern specifies how log messages are formatted, using a combination of literals, conversion characters and format modifiers. Consider the following pattern: log4j.appender.ConsoleAppender.layout.ConversionPattern=[%-5p] %d %c - %m%n.

What are the format characters used in log4j?

The format characters used in log4j are, L- it is used to output the line number from where the logging request was processed or issued. m- It is used to output the application supplied message related to the logging event. p- It is used to output the priority of the logging event.

What is the purpose of the PatternLayout class?

The PatternLayout class extends the abstract org.apache.log4j.Layout class and overrides the format () method to structure the logging information according to a supplied pattern. PatternLayout is also a simple Layout object that provides the following- Bean Property which can be set using the configuration file:

What is the use of PatternLayout in Log4j?

The PatternLayout class extends the abstract org.apache.log4j.Layout class and overrides the format () method to structure the logging information according to a supplied pattern. PatternLayout is also a simple Layout object that provides the following- Bean Property which can be set using the configuration file: Sets the conversion pattern.

What is pattern layout?

The placement of pattern on the fabric, in an economical manner, that is without wasting fabric is known as pattern layout. The placement of pattern on the fabric, in an economical manner, that is without wasting fabric is known as pattern layout. All the patterns should be arranged prop-erly following grain of the fabric.

How to change the default time zone in enhancedpatternlayout?

Specifying something like GMT-5 will simply adjust the GMT time zone by the specified amount of hours. Also if you whant to dinamicaly obtein the default time zone, you can extend EnhancedPatternLayout and overwrite the method "setConversionPattern" like this:


2 Answers

Check the documentation, everything is explained. EnhancedPatternLayout is an enhanced version of PatternLayout. It should be used in preference to PatternLayout (except for compatibility reason with PatternLayout).

PatternLayout contains some issues which are not present in EnhancedPatternLayout especially with synchronisation.

like image 163
Çağdaş Bozman Avatar answered Oct 09 '22 22:10

Çağdaş Bozman


The main difference between PatternLayout and EnhancedPatternLayout is in the format() method. PatternLayout relies on a member field named sbuf which it modifies whereas EnhancedPatternLayout uses a private instance of the StringBuffer. This means that PatternLayout.format() calls are susceptible to data races during concurrent calls while concurrent EnhancedPatternLayout.format() calls are not.

like image 30
shams Avatar answered Oct 09 '22 23:10

shams