Log log Detailed analysis

Source: Internet
Author: User
Tags log log truncated log4j
Using Logging information

1, about log level log can be divided into five levels
Debug: Debug information for the program, lowest level
Info: For general information to display, such as login log out
Error: A critical error is primarily a bug in the program
WARN: For general warnings, such as session loss
FATAL: Crash, the entire program terminates running



2, the log output to the designated place1. Output to File
Log4j.rootlogger=debug,tofile

#输出到文件
Log4j.appender.tofile=org.apache.log4j.fileappender

#按指定的格式输出
Log4j.appender.tofile.layout=org.apache.log4j.patternlayout

#指定输出格式
log4j.appender.tofile.layout.conversionpattern=%d%p [%c%m]%m%n

#指定文件输出文件路径
Log4j.appender.tofile.file=log/b.txt



3. Output to Database
Lo4j.rootlogger=debug,tojdbc

#输出到数据库
Log4j.appender.tojdbc=org.apache.log4j.jdbc.jdbcappender

#指明输出的格式
Log4j.appender.tojdbc.layout=org.apache.log4j.patternlayout

#数据源在数据库输出格式
log4j.appender.tojdbc.layout.conversionpattern=%d%p [%c%m]%m%n

#指明驱动
Log4j.appender.tojdbc.driver=net.sourceforge.jtds.jdbc.driver

#数据库URL
Log4j.appender.tojdbc.url=jdbc:jtds:sqlserver://localhost:1433/pet

#数据库用户名
Log4j.appender.tojdbc.user=sa

#数据库密码
Log4j.appender.tojdbc.password=sa

#指定数据库sql语句
Log4j.appender.tojdbc.sql=insert into Loginfo (log4jinfo) VALUES ('%d%p [%c%m]%m%n ')



4. Output to console

The output is debug information
#log4j. Rootlogger=debug,tocon,

#配置Appendder的全路径ConsoleAppender: Output to console
Log4j.appender.tocon=org.apache.log4j.consoleappender

#说明输出的格式: simplelayout simple format, only information and level of output
Log4j.appender.tocon.layout=org.apache.log4j.simplelayout



5. Output to Web page
Log4j.rootlogger=debug,html

#输出到HTML
Log4j.appender.html=org.apache.log4j.dailyrollingfileappender

#按指定的格式输出
Log4j.appender.html.layout=org.apache.log4j.htmllayout

#指定输出格式
#log4j. appender.tofile.layout.conversionpattern=%d%p [%c%m]%m%n

#指定文件输出文件路径
Log4j.appender.html.file=html/b.html



6. Related Operation

Set Output level
1.private Logger log = Logger.getlogger (Log4jtest.class);
2.log.setlevel (Level.warn)


7, the function of the symbol in the log Information format


-X: Left-aligned when x information is output;
%p: Output log information priority, i.e. Debug,info,warn,error,fatal,
%d: the date or time of the output log point-in-time, the default format is ISO8601, or the format can be specified thereafter, for example:%d{yyy MMM dd hh:mm:ss,sss}, Output is similar: October 18, 2002 22:10:28,921
%r: The number of milliseconds to output from the application boot to the output of this log information
%c: The class that the output log information belongs to, usually the full name of the class in which it is located

%t: Output The name of the thread that generated the log event
%l: The location of the output log event, equivalent to the combination of%c.%m (%f:%l), including the class name, the thread that occurred, and the number of lines in the code. Example: Testlog4.main (testlog4.java:10)
%x: The NDC (nested diagnostics environment) associated with the current line threads relative, especially in multiple client multi-threaded applications such as Java Servlets.
Percent%: output a "%" character

%F: The name of the file where the output log message was generated
%l: line number in output code
%M: The message specified in the output code, the resulting log specific information
%n: Output a carriage return line break, Windows platform for "\ r \ n", Unix platform for "\ n" Output log message Wrapping


8. You can control the minimum width, maximum width, and text alignment by adding modifiers between% and pattern characters. such as:

1 C: Specifies the name of the output category, the minimum width is 20, and if the category name is less than 20, the default is right-aligned.
2)%-20c: Specifies the name of the output category, the minimum width is 20, if the category name is less than 20, the "-" number specifies left-aligned.
3)%.30c: Specifies the name of the output category, the maximum width is 30, if the category name is greater than 30, will be the left more characters truncated, but less than 30 words will not have spaces.
4). 30c: If the name of the category is less than 20, the space is filled and right-aligned, and if its name is longer than 30 characters, it is truncated from the character that is farther from the left.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.