Java log Combat and parsing __java

Source: Internet
Author: User
Tags log4j

Java log Combat and analysis

Log is one of the basic skills that programmers must master, and if you write software without a log, you can say you are not a real programmer.

Why do I have to keep a diary?

• Monitoring code

• Variable variation, system operation process.

• Production line environment, not very good debugging.

• Distributed environment, debugging more difficult, log is a very good helper.

• Statistical analysis

• Future Audits

in fact, 4% of the code is a log.

The Java Log framework consists mainly of log4j,logback and other unusual official logs and apachelogging.

The original author of Log4j and Logback is the same author Cekigülcü. Mainstream use is generally log4j, so the main point of this article is mainly explained Log4j.

Configuration file Log4j.properites file, generally put down classpath directory can be, no need to restart.

Log4j.rootlogger=debug, Stdout,r

Log4j.appender.stdout=org.apache.log4j.consoleappender

Log4j.appender.stdout.layout=org.apache.log4j.patternlayout

# to output the caller ' s file name and line number.

Log4j.appender.stdout.layout.conversionpattern=%-4r[%t]%5p%c%x-%m-%x{username}%n

Log4j.appender.r=org.apache.log4j.rollingfileappender

Log4j.appender.r.file=example.log

log4j.appender.r.maxfilesize=100kb

# Keep One backup file

Log4j.appender.r.maxbackupindex=1

Log4j.appender.r.layout=org.apache.log4j.patternlayout

Log4j.appender.r.layout.conversionpattern=%-4r[%t]%5p%c%x-%m-%x{username}%n

Log4j.rootlogger=debug, Stdout,r

Log4j.rootlogger=[level], Appender1, Appender2

Log4j.appender.stdout=org.apache.log4j.consoleappender

Log4j.appender.appender1=org.apache.log4j.consoleappender

Log4j.appender.stdout.layout=org.apache.log4j.patternlayout

Log4j.appender.appender1.layout=org.apache.log4j.patternlayout

Other similar

Basically three main parts of the configuration:

Logger

log type and log level (TRACE < DEBUG < INFO < WARN <error < FATAL)

 

appenders

where the output is, there can be multiple destinations (Console,file,gui components, remote socket servers,jms,nt Event loggers,remote Unix Syslog daemons. can also be asynchronous)

 

appenders General Console ( Consoleappender) for debugging, file has a daily update (Dailyrollingfileappender) and the largest number of files (Rollingfileappender The setting of scrolling, also has the alarm setting of the mail. Basic understanding of the above can be.

 

Layouts

%r [%t]%-5p%c-%m%n

176 [main] INFO org.foo.bar-located nearest gasstation.

Layouts Description

Conversion Character

Effect

Used

Category to output the logging event. The category conversion specifier can be optionally followed By precision, which is a decimal specifier in BR Ackets.

If A precision specifier is given, then only the corresponding number of right most components of the category name would be printed. By default, the category name is printed to full.

For example, for the category name "A.B.C" 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 are optionally followed by precision specifier, which 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 would be P Rinted. 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} would output "SomeClass".

WARNING Generating the caller class information is slow. Thus, use should being avoided unless execution speed is not a issue.

D

Used to output the date of the logging event. The date conversion specifier May is followed by a date format specifier enclosed between. 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 the format is assumed.

The date format specifier admits the same syntax as the time pattern string of the SimpleDateFormat. Although the standard JDK, the performance Ofsimpledateformat is quite poor.

For better results It's recommended to use the log4j date formatters. These can is specified using one of the strings "absolute", "DATE" and "ISO8601" for Specifyingabsolutetimedateformat, Dat Etimedateformat and respectively Iso8601dateformat. For example, %d{iso8601} or %d{absolute}.

These dedicated date formatters perform significantly better than.

F

Used to output the file name where the logging request is issued.

WARNING Generating caller location information is extremely slow and should was avoided unless execution speed are not a issue.

L

Used to output location information of the caller which generated event.

The location information depends on the JVM implementation but usually consists of the fully qualified Method followed by the callers source, the file name and line number between parentheses.

The location information can be very useful. However, its generation are extremely slow and should be avoided unless, execution speed.

L

Used to output the line number from where the logging request is issued.

WARNING Generating caller location information is extremely slow and should was avoided unless execution speed are not a issue.

m

Used to output the application supplied message associated with the logging event.

M

Used to output the "method name where" logging request was issued.

WARNING Generating caller location information is extremely slow and should was avoided unless execution speed are not a 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" \ r \ n. " 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 Ev Ent.

T

Used to output the ' 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 is followed by the key for the "map placed between, as in braces ER} where clientnumber is the key. The value in the MDC corresponding to the key would be output.

The MDC class for more details.

%

The sequence% outputs a single percent sign.

More Description:

Format modifier

Left Justify

Minimum width

Maximum width

Comment

%20c

False

20

None

Left pad and spaces if the category name is less than characters long.

%-20c

True

20

None

Right pad with spaces if the category name is less than characters long.

%.30c

NA

None

30

Truncate from the beginning if the category name is longer than characters.

%20.30c

False

20

30

Left pad and spaces if the category name is shorter than characters. However, if category name is longer than characters, then truncate from the beginning.

%-20.30c

True

20

30

Right pad with spaces if the category name is shorter than characters. However, if category name is longer than characters, then truncate from the beginning.

Maven Reference

<dependency>

<groupId>log4j</groupId>

<artifactId>log4j</artifactId>

<version>1.2.8</version>

</dependency>

The log characteristics that individuals think must be understood and mastered:

Log level

Inherited

JMX Management

Appenders's additivity

 

other needs to understand

Filter

NDC

MDC

Logging Tuning

Example:

logger.debug ("Entry number:" + i + "is" + string.valueof (entry[i));

if (logger.isdebugenabled () {

logger.debug ("Entry number:" + i + "is" + string.valueof (entry[i));

}

The judgment is only 1% times the log, so it is generally worth it.

However, the feature is automatically integrated using SLF4J.

after understanding the log4j, also must understand slf4j, we generally use this to carry on the integration internal different diary's concrete realization.

MAVEN uses

<dependency>

<groupid>org.slf4j</groupid>

<artifactid>slf4j-api</artifactid>

<version>1.7.21</version>

</dependency>

Logger Logger = Loggerfactory.getlogger (helloworld.class);

logger.info ("Hello World");

Common record log places:

• Method Entrance

• Method exit

• Abnormal

• The information you need to track

• Log the nearest local log

• Log first, then throw the exception, you can throw the exception up

• Remove redundant logs

Usage of the Log library

logs are generally used. Mastering This course is almost enough and it's up to you to see what's left.

The famous SOLR usage log situation

General large enterprises, will collect logs, using technology such as elk to view logs, or sink to Hadoop for data analysis and mining, and even use storm for real-time statistics.

For more information, refer to the video:

CSDN College: http://edu.csdn.net/course/detail/2890

NetEase Cloud Classroom: http://study.163.com/course/introduction/1003149011.htm

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.