• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Log4j
 

Log4j

on

  • 217 views

 

Statistics

Views

Total Views
217
Views on SlideShare
217
Embed Views
0

Actions

Likes
0
Downloads
4
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via SlideShare as Microsoft Word

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Log4j Log4j Document Transcript

    • Log4j Documentation This documentation explains how to set up log4j with email, files and stdout. It compares XML to properties configuration files, shows how to change LogLevels for a running application. Furthermore, we explain best practices on logging and exception handling. • Create the file 'src/main/resources/log4j.properties’. • 'pom.xml' file contains: <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.14</version> </dependency> • Now we will discuss about some statements in DwfLogger.java file: Log4j(DwfLogger.java ) will first check for a file log4j.xml(dwflogger.xml) and then for a log4j.properties file in the root directory of the classes folder (= src folder before compilation).  URL loggerUrl = ClassLoader.getSystemResource("dwflogger.xml"); The above statement given in DwfLogger.java file and it goes to the dwflogger.xml file.  PropertyConfigurator.configure("log4j.properties"); The above statement goes to log4j.properties file used to parse the URL to configure log4j unless the URL ends with the ".xml" extension.  The PropertyConfigurator will be used to parse the URL to configure log 4j unless the URL ends with the ".xml" extension.  Logger logger = DwfLogger.getLogger(DwfLogger.class);
    •  getLogger(java.lang.String name) Return a new logger instance named as the first parameter using the default factory.  getLogger public Logger getLogger(java.lang.String name) Return a new logger instance named as the first parameter using the default factory. If a logger of that name already exists, then it will be returned. Otherwise, a new logger will be instantiated and then linked with its existing ancestors as well as children. Specified by: getLogger in interface LoggerRepository Parameters: name - The name of the logger to retrieve.  GetLogger public Logger getLogger(java.lang.String name,LoggerFactory factory) Return a new logger instance named as the first parameter using factory. If a logger of that name already exists, then it will be returned. Otherwise, a new logger will be instantiated by the factory parameter and linked with its existing ancestors as well as children. Specified by: getLogger in interface LoggerRepository Parameters: name - The name of the logger to retrieve. factory - The factory that will make the new logger instance.  Logger level
    • logger.trace("This is the Message that is logged as Trace Level"); logger.debug("This is the Message that is logged as Debug Level"); logger.info("This is the Message that is logged as Info Level"); logger.warn("This is the Message that is logged as Warn Level"); logger.error("This is the Message that is logged as Error Level"); logger.fatal ("This is the Message that is logged as Fatal Level"); The following Levels are available. But you can define custom levels as well. Examples are provided with the log4j download. Level Description all All levels including custom levels trace developing only, can be used to follow the program execution. debug developing only, for debugging purpose info Production optionally, Course grained (rarely written informations), I use it to print that a configuration is initialized, a long running import job is starting and ending. warn Production, simple application error or unexpected behaviour. Application can continue. I warn for example in case of bad login attemps, unexpected data during import jobs. error Production, application error/exception but application can continue. Part of the application is probably not working. fatal Production, fatal application error, application cannot continue, for example database is down. No Do not log at all. • dwflogger.xml : Three main components you need to configure to obtain the same result are the logger, appender and layout.
    •  The logger object is the one that is used to log messages.  The appender is the one that specifies the output destination like console or a file and  The Layout is the one that specify the format in which the log messages should be logged. In <root> tag, here first ‘appender-ref’ is “ConsoleAppender”.so in “log4j:configuration” tag “appender name” is ConsoleAppender and class is "org.apache.log4j.ConsoleAppender".  ConsoleAppender : Logs to console.  FileAppender : Logs to a file The invocation of the BasicConfigurator.configure method creates a rather simple log4j setup. This method is hardwired to add to the root logger a ConsoleAppender. The output will be formatted using a PatternLayout set to the pattern "%-4r [%t] %-5p %c %x - %m %n". Note that by default, the root logger is assigned to Level.DEBUG.  Layout of the log file The layout specifies how a log message looks like. First you define the layout in log4j.properties file log4j.appender.rootAppender.layout=org.apache.log4j.PatternLayout The pattern layout requires another parameter, i.e. the pattern. log4j.appender.rootAppender.layout.ConversionPattern=[%d{dd MMM yyyy HH:mm:ss}] [%-6p] [%C{2}] -> [%M (%L)] [%t] - %m%n There you can see that we have DateLayout, HTMLLayout, PatternLayout, SimpleLayout, XMLLayout as options. SimpleLayout has no properties to be set. It is simple. We used PatternLayout in our example and we set a property named ConversionPattern. This property allows us to define the log output as folows. [%d{dd MMM yyyy HH:mm:ss}] Date in Format [%-6p] %6 defines a right justified print with 6 characters, p prints the priority of the log message [%C{2}] -> [%M (%L)] - %m%n conversion patterns A flexible layout configurable with pattern string.
    • The goal of this class is to format a LoggingEvent and return the results as a String. The results depend on the conversion pattern. The conversion pattern is closely related to the conversion pattern of the printf function in C. 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 and a conversion character. The conversion character specifies the type of data, e.g. category, priority, date, thread name. The format modifiers control such things as field width, padding, left and right justification. The following is a simple example. Let the conversion pattern be "%-5p [%t]: %m%n" and assume that the log4j environment was set to use a PatternLayout. Then the statements Category root = Category.getRoot(); root.debug("Message 1"); root.warn("Message 2"); would yield the output DEBUG [main]: Message 1 WARN [main]: Message 2 Note that there is no explicit separator between text and conversion specifiers. The pattern parser knows when it has reached the end of a conversion specifier when it reads a conversion character. In the example above the conversion specifier %-5p means the priority of the logging event should be left justified to a width of five characters. The recognized conversion characters are Conversion Character Effect c Used to output the category of the logging event. The category conversion specifier can be optionally followed by precision specifier, that is a decimal constant in brackets. If a precision specifier is given, then only the corresponding number of right most components of the category name will be printed. By default the category name is printed in full. For example, for the category name "a.b.c" the pattern %c{2} will output "b.c".
    • C Used to output the fully qualified class name of the caller issuing the logging request. This conversion specifier can be optionally followed by precision specifier, that is a decimal constant in brackets. If a precision specifier is given, then only the corresponding number of right most components of the class name will be printed. By default the class name is output in fully qualified form. For example, for the class name "org.apache.xyz.SomeClass", the pattern %C{1} will output "SomeClass". WARNING Generating the caller class information is slow. Thus, it's use should be avoided unless execution speed is not an issue. d Used to output the date of the logging event. The date conversion specifier may be followed by a date format specifier enclosed between braces. For example, %d{HH:mm:ss,SSS} or %d{dd MMM yyyy HH:mm:ss,SSS}. If no date format specifier is given then ISO8601 format is assumed. The date format specifier admits the same syntax as the time pattern string of the SimpleDateFormat. Although part of the standard JDK, the performance of SimpleDateFormat is quite poor. For better results it is recommended to use the log4j date formatters. These can be specified using one of the strings "ABSOLUTE", "DATE" and "ISO8601" for specifying AbsoluteTimeDateFormat, DateTimeDateFormat and respectively ISO8601DateFormat. For example, %d{ISO8601} or %d{ABSOLUTE}. These dedicated date formatters perform significantly better than SimpleDateFormat. F Used to output the file name where the logging request was issued. WARNING Generating caller location information is extremely slow. It's use should be avoided unless execution speed is not an issue.
    • l Used to output location information of the caller which generated the logging event. The location information depends on the JVM implementation but usually consists of the fully qualified name of the calling method followed by the callers source the file name and line number between parentheses. The location information can be very useful. However, it's generation is extremely slow. It's use should be avoided unless execution speed is not an issue. L Used to output the line number from where the logging request was issued. WARNING Generating caller location information is extremely slow. It's use should be avoided unless execution speed is not an issue. m Used to output the application supplied message associated with the logging event. M Used to output the method name where the logging request was issued. WARNING Generating caller location information is extremely slow. It's use should be avoided unless execution speed is not an issue. n Outputs the platform dependent line separator character or characters. This conversion character offers practically the same performance as using non-portable line separator strings such as "n", or "rn". Thus, it is the preferred way of specifying a line separator. p Used to output the priority of the logging event. r Used to output the number of milliseconds elapsed from the construction of the layout until the creation of the logging event. t Used to output the name of the thread that generated the logging event. x Used to output the NDC (nested diagnostic context) associated with the thread that generated the logging
    • event. X Used to output the MDC (mapped diagnostic context) associated with the thread that generated the logging event. The X conversion character must be followed by the key for the map placed between braces, as in %X{clientNumber} where clientNumber is the key. The value in the MDC corresponding to the key will be output. % The sequence %% outputs a single percent sign. By default the relevant information is output as is. However, with the aid of format modifiers it is possible to change the minimum field width, the maximum field width and justification. The optional format modifier is placed between the percent sign and the conversion character. The first optional format modifier is the left justification flag which is just the minus (-) character. Then comes the optional minimum field width modifier. This is a decimal constant that represents the minimum number of characters to output. If the data item requires fewer characters, it is padded on either the left or the right until the minimum width is reached. The default is to pad on the left (right justify) but you can specify right padding with the left justification flag. The padding character is space. If the data item is larger than the minimum field width, the field is expanded to accommodate the data. The value is never truncated. This behavior can be changed using the maximum field width modifier which is designated by a period followed by a decimal constant. If the data item is longer than the maximum field, then the extra characters are removed from the beginning of the data item and not from the end. For example, it the maximum field width is eight and the data item is ten characters long, then the first two characters of the data item are dropped. This behavior deviates from the printf function in C where truncation is done from the end. Below are various format modifier examples for the category conversion specifier. Format modifier left justify minimum width maximum width comment %20c false 20 none Left pad with spaces if the category name is less than 20 characters long. %-20c true 20 none Right pad with spaces if the category name is less than 20 characters long. %.30c NA none 30 Truncate from the beginning if the category name is longer than 30 characters.
    • %20.30c false 20 30 Left pad with spaces if the category name is shorter than 20 characters. However, if category name is longer than 30 characters, then truncate from the beginning. %-20.30c true 20 30 Right pad with spaces if the category name is shorter than 20 characters. However, if category name is longer than 30 characters, then truncate from the beginning. Below are some examples of conversion patterns. %r [%t] %-5p %c %x - %m%n This is essentially the TTCC layout. %-6r [%15.15t] %-5p %30.30c %x - %m%n Similar to the TTCC layout except that the relative time is right padded if less than 6 digits, thread name is right padded if less than 15 characters and truncated if longer and the category name is left padded if shorter than 30 characters and truncated if longer. Now in both “dwflogger.xml” and “log4j.properties” files, give the same ConversionPattern value in log4j.properties as given below  dwflogger.xml : <appender name="ConsoleAppender" class="org.apache.log4j.ConsoleAppender"> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="[%d{dd MMM yyyy HH:mm:ss}] [%-6p] [%C{2}] -> [%M (%L)] [%t] - %m%n"/> </layout> </appender> <appender name="FileAppender" class="org.apache.log4j.FileAppender"> <param name="file" value="/logs/dwfLogs.log" /> <param name="append" value="true" /> <param name="encoding" value="UTF-8" /> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="[%d{dd MMM yyyy HH:mm:ss}] [%-6p] [%C{2}] -> [%M (%L)] [%t] - %m%n"/> </layout>
    • </appender> <root> <priority value ="fatal" /> <appender-ref ref="ConsoleAppender"/> <appender-ref ref="FileAppender"/> </root>  log4j.properties : log4j.appender.rootAppender.layout=org.apache.log4j.PatternLayout log4j.appender.rootAppender.layou t.ConversionPattern=[%d{dd MMM yyyy HH:mm:ss}] [%-6p] [%C{2}] -> [%M (%L)] [%t] - %m%n log4j.appender.dwfsAppender.layo ut=org.apache.log4j.PatternLayout log4j.appender.dwfsAppender.layo ut.ConversionPattern=[%d{dd MMM yyyy HH:mm:ss}] [%-6p] [%C{2}] -> [%M (%L)] [%t] - %m%n  In dwflogger.xml file <root> tag , <priority value ="fatal" /> ----------------------> Here the Priority value is taken as ‘fatal’.  Already we discuss about <appender-ref ref="ConsoleAppender"/>  And <appender-ref ref="FileAppender"/> FileAppender : FileAppender has the File and Append options both of which are ambigous until the other is also set. File : <param name="file" value="/logs/dwfLogs.log" /> (In logs folder log file will be create) Append : <param name="append" value="true" /> (Here true means log file information is appending) Encoding : <param name="encoding" value="UTF-8" /> (Here encoding is UTF-8 ) • log4j.properties file:
    •  log4j.rootLogger=DEBUG,rootAppender RootLogger : The rootLogger is the one that resides on the top of the logger hierarchy. Here we set its level to DEBUG and added the root appender (RA) to it. The root appender can have arbitrary name, here its name is RA. Once the appender is created and its layout is set you need to specify which loggers can use this appender. If you set this appender to the rootLogger then all the loggers will log message to this appender. Since the rootLogger is on top of the hierarchy all the loggers will inherit its logger level and its appenders. Here we mention rootAppender so we are taking “log4j.appender.rootAppender” log4j.appender.rootAppender=org.apache.log4j.RollingFileAppender log4j.appender.rootAppender.File=/logs/application.log log4j.appender.rootAppender.MaxFileSize=10MB Same as the above is dwfsAppender so we are taking “log4j.appender.dwfsAppender” as shown below log4j.appender.dwfsAppender=org.apache.log4j.RollingFileAppender log4j.appender.dwfsAppender.File=/logs/dwfs.log log4j.appender.dwfsAppender.MaxFileSize=10MB log4j.appender.dwfsAppender.MaxBackupIndex=1 In the above two appenders (rootAppender and dwfsAppender) ,  RollingFileAppender : Logs to a file, starts a new file once the max size is reached. (An alternative is the DailyRollingFileAppender which creates on file per day). log4j.appender.rootAppender=org.apache.log4j.RollingFileAppender log4j.appender.dwfsAppender=org.apache.log4j.RollingFileAppender
    • So by using RollingFileAppender we created two log files namely,” application.log and dwfs.log”.If u want to rename the file name.then rename these two files. log4j.appender.rootAppender.File=/logs/application.log log4j.appender.dwfsAppender.File=/logs/dwfs.log  MaxFileSize : Maximum size of the file. log4j.appender.rootAppender.MaxFileSize=10MB (Maximum size of file is 10MB)  MaxBackupIndex : Keeping number of backup files log4j.appender.dwfsAppender.MaxBackupIndex=1 (keep one Backup file)