Log4J

From KevinWiki

(Difference between revisions)
Jump to: navigation, search
(New page: Category:Java logging frameworks ==Configuration== -Sample config text file # Set root category priority to DEBUG and set its only appender to A1 log4j.rootCategory=DEBUG, A1 # A...)
Line 1: Line 1:
-
[[Category:Java logging frameworks]]
+
[[Category:Logging Frameworks (Java)]]
==Configuration==
==Configuration==
-Sample config text file
-Sample config text file

Revision as of 12:34, 6 October 2008

Configuration

-Sample config text file

# Set root category priority to DEBUG and set its only appender to A1
log4j.rootCategory=DEBUG, A1

# A1 is set to be a ConsoleAppender (writes to system console).
log4j.appender.A1=org.apache.log4j.ConsoleAppender

# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
#log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
#log4j.appender.A1.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
log4j.appender.A1.layout.ConversionPattern=%n%x%n%d{yyyy-MM-dd HH:mm:ss} [%-5p] %C.%M(%F:%L) %n%m%n%n


-Sample config xml file

 <?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
 
 <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
 
     <appender name="normal" class="org.apache.log4j.ConsoleAppender">
         <param name="Threshold" value="DEBUG"/>
         <layout class="org.apache.log4j.PatternLayout">
             <param name="ConversionPattern" 
			value="///=========%n%x%n%d{yyyy-MM-dd HH:mm:ss} [%-5p] %C.%M(%F:%L) %n%m%n=========///%n"/>
         </layout>
     </appender>
 
     <logger name="net.kevin" additivity="false">
         <level value="DEBUG"/>
         <appender-ref ref="normal"/>
     </logger>
 
     <root>
         <level value="INFO"/>
         <appender-ref ref="normal"/>
     </root>
 
     <!--
     1. Use debug to write debugging messages which should not be printed when the application is in production.
     2. Use info for messages similar to the "verbose" mode of many applications.
     3. Use warn for warning messages which are logged to some log but the application is able to carry on without a problem.
     4. Use error for application error messages which are also logged to some log but, still, the application can hobble along.
     Such as when some administrator-supplied configuration parameter is incorrect and you fall back to using
     some hard-coded default value.
     5. Use fatal for critical messages, after logging of which the application quits abnormally.
     -->
 
     <!--
     <logger name="org.apache" additivity="false">
     <level value="INFO"/>
     <appender-ref ref="normal"/>
     </logger>
 
     <root>
     <level value="WARN"/>
     <appender-ref ref="normal"/>
     </root>
     -->
 </log4j:configuration>


-Conversion characters:

  •  %m: Outputs your message.
  •  %p: Outputs the priority of the logging event.
  •  %r: Outputs the number of milliseconds elapsed since the start of the application until the creation of the logging event.
  •  %c: Outputs the category of the logging event. Example: For the category name "a.b.c", the pattern %c{2} will output "b.c". {2} means "output last two components of the dot-separated category name". If no {n} is there, full Category name is output by default.
  •  %t: Outputs the name of the thread that generated the logging event.
  •  %x: Outputs the nested diagnostic context (NDC) associated with the thread that generated the logging event. Useful when multiple clients are handled by separate threads such as in Java servlets.
  •  %n: Outputs the platform-dependent newline character(s). Preferable to specifying "\n" or "\r\n" etc.
  •  %%: Outputs a single percent sign.
  • WARNING: The patterns below will slow down the execution of your program somewhat. Avoid unless execution speed is not an issue.
  •  %d: Outputs the date of the logging event. The date conversion specifier may be followed by a date format specifier enclosed between braces. 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 Java's SimpleDateFormat which is slow. For faster performance, use %d{ISO8601}, %d{ABSOLUTE}, %d{RELATIVE} (millisecs since program start, fastest) or %d{DATE} which use log4j's ISO8601DateFormat, AbsoluteTimeDateFormat, RelativeTimeDateFormat and DateTimeDateFormat date formatters respectively.
  •  %l: Outputs source code location information. Shortcut for %C.%M(%F:%L).
  •  %C: Outputs the fully-qualified class name of the caller issuing the logging request. Example: For the class name "org.apache.xyz.SomeClass", the pattern %C{1} will output "SomeClass". {1} means "output last one component of the fully-qualified class name". If no {n} is there, full class name is output by default.
  •  %M: Outputs the method name where the logging request was issued.
  •  %F: Outputs the file name where the logging request was issued.
  •  %L: Outputs the line number from where the logging request was issued.


References

http://www.vipan.com/htdocs/log4jhelp.html

Personal tools