Log4j learning notes (2)

zhaozj2021-02-16  82

Log4j learning notes (2)

The front recorded some principles, today is a practice.

1. The study found that 4% of the code in a system is used to make Logging. 2, log4j's configuration file is used to set the level, regulator, and layout of the logger, which can pick up the setting of the set of key = value format or XML format. By configuration, you can create a log4J's operating environment. Log4J is running, do not do any assumptions for the environment, especially without the default storage. 3, there are several ways to configure log4j1) Call the BasicConfigurator.configure () method in the program; 2) Configure the file name by the command line parameter, parsing and configuring it through PropertyConfigurator.configure ; 3) Configure the file name and other information, by the environment variable, use the log4j default initialization process parsing and configured; 4) Configure the file name and other information, using the application server configuration, using a special servlets to complete the configuration. Look at the example below:

import com.foo.Bar;. // Import log4j classes import org.apache.log4j.Logger; import org.apache.log4j.BasicConfigurator; public class MyApp {// Define a static logger variable so that it references the // Logger instance named "MyApp" static Logger logger = Logger.getLogger (MyApp.class);. public static void main (String [] args) {// Set up a simple configuration that logs on the console BasicConfigurator.configure ();. logger Bar bar = new bar (); bar.doot (); logger.info ("exiting application.");}}

Package com.foo; import org.apache.log4j.logger; public class bar {static logger logger = logger.getlogger (bar.class); public void DOIT () {logger.debug ("DID IT AGAIN!");} }

BasicConfigurator.configure adds a consolerapnder to the root logger, the output format is set to "% -4R [% T]% -5p% C% X-% M% N" via PatternLayout, and the default level of the root recorder is Level. Debug.

The relationship between the recorder is as follows:

The output is as follows:

INFO MyApp - Entering Application.

36 [main] debug com.foo.bar - DID IT Again!

INFO MYAPP - EXITING Application.

The following code combines configuration information, which will result in the same result as the above program.

import com.foo.Bar; import org.apache.log4j.Logger; import org.apache.log4j.PropertyConfigurator; public class MyApp {static Logger logger = Logger.getLogger (MyApp.class.getName ()); public static void main (String [] args) {// BasicConfigurator replaced with protyconfigurator. PropertyConfigurator.configure (args [0]); Logger.info ("Entering Application)); bar bar = new bar (); bar.doot (); logger The content of the configuration file is as follows:

# Set root logger level to debug and its Only Appender to A1.

Log4j.rootlogger = Debug, A1

# A1 is set to be a consoleappender.log4j.appender.a1 = org.apache.log4j.consoleappender

# A1 uss patternLayout.log4j.Appender.a1.Layout = org.apache.log4j.patternLayoutLog4j.Appender.a1.Layout.conversionPattern =% - 4R [% T]% -5p% C% X-% M% N

With configuration files, you can easily modify the configuration. Below

Log4j.rootlogger = Debug, Stdout, R

Log4j.appender.stdout = org.apache.log4j.consoleappenderlog4j.Appender.stdout.Layout = org.apache.log4j.patternlayout

# Pattern to output The caller's file name and line number. # Log4j.appender.stdout.Layout.conversionPattern =% 5P [% t] (% F:% L) -% M% N # print The date in ISO 8601 FormatLog4j. Appender.stdout.Layout.conversionPattern =% D [% T]% -5p% C -% M% N

Log4j.Appender.r = org.apache.log4j.rollingfileappenderlog4j.Appender.r.file = example.log

Log4j.Appender.r.maxfilesize = 100kb # Keep One Backup filelog4j.Appender.r.maxbackupindex = 1

Log4j.Appender.r.Layout = org.apache.log4j.patternLayoutLog4j.Appender.r.Layout.conversionPattern =% P% T% C -% M% N

# Print Only Messages of Level Warn OR Above In The package com.foo.log4j.logger.com.foo = WARN

For Tomcat4, use environment variables to pass parameters, see the following examples.

UNIX settings

Export catalina_opts = "- DLOG4J.CONFIGURATION = FOOBAR.TXT" <=== PROPERTYCONFIGURATOR analysis

Export catalina_opts = "- DLOG4J.DEBUG -DLOG4J.CONFIGURATION = FOOBAR.XML"

<=== Analysis with Domconfigurator

Here is Windows settings

SET CATALINA_OPTS = -dlog4j.configuration = foobar.lcf -dlog4j.configuratorclass = com.foo.barconfigurator

<=== An Analysis with com.foo.barconfigurator

SET CATALINA_OPTS = -dlog4j.configuration = file: / c: /foobar.lcf

The configuration file location is not explicitly indicated, it is placed in a web-inf / class directory.

4, configure log4j with servlet

The following is a referring to the article of ice, copy it here, and make some necessary modifications. It should be noted that the following code needs to write and publish it, there is no such class in JAR.

I will write one later and put it up. I also wrote two, in

Notes (3).

Added code in the web.xml file under the Application directory

log4jlog4j-init com.apache.jakarta.log4j.log4jinit log4j /web-inf/log4j.properties 1

This code means that the com.apache.jakarta.log4j.log4jin is loaded when Tomcat is started. This class file named log4jinit.class.

Where the source code of log4jinit.class is as follows

package com.apache.jakarta.log4j; import org.apache.log4j.PropertyConfigurator; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class Log4jInit extends HttpServlet {public void init () {string prefix = getServletContext (). getRealPath ("/"); string file = getInitParameter ("log4j"); // if the log4j-init-file is not set, Then no point in Trying System .out.println ("................ log4j start"); if (file! = null) {propertyconfigurator.configure (prefix file);}} public void doget HTTPSERVLETREQUEST REQ, HTTPSERVLETRESPONSE RES {}} The program reads /web-inf/log4j.properties in the process of loading.

The configuration file is explained as follows:

# Set root logger level to debug and its only appender to a1

# log4j has five Logger

#Fatal 0

#Error 3

#Warn 4

#Info 6

#Debug 7

# Configure the root Logger, its syntax is:

# log4j.rootlogger = [level], appendername, appendername, ...

Log4j.rootlogger = info, a1, r

# This sentence is set to have all logs output

# If log4j.rootlogger = warn, it means that only Warn, Error, Fatal

# Output, Debug, INFO will be blocked.

# A1 is set to be a consoleapplender.

# log4j Appender has several layers such as console, file, GUI components, and even set interface servers, NT event logger, UNIX Syslog daemon, etc.

#ConsoleAppender output to the console

Log4j.Appender.a1 = org.apache.log4j.consoleAppender

# A1 The output layout used, where log4j provides four layouts. Org.apache.log4j.htmlLayout (layout in HTML table)

# org.apache.log4j.patternlayout (can be flexibly specified for layout mode),

# org.apache.log4j.simplelayout (level and information string with log information),

# org.apache.log4j.ttccLayout (including information, thread, category, etc.)

Log4j.Appender.a1.Layout = org.apache.log4j.patternLayout # flexible definition output format Specific View log4j javadoc org.apache.log4j.patternlayout #d time .... log4j.appender.a1.Layout.conversionPattern =% - D {YYYY-MM-DD HH: mm: SS} [% C] - [% P]% M% N #R Output to the file ROLLINGFILEAPPENDER extension, you can provide a backup function of a log. Log4j.Appender.r = org.apache.log4j.rollingfileaplender # log file name log4j.Appender.r.file = log4j.log # log file size log4j.Appender.r.maxFileSize = 100kb # Save a backup file log4j. Appender.r.maxbackupindex = 1log4j.appender.r.Layout = org.apache.log4j.tccLayout # log4j.Appender.r.Layout.conversionPattern =% - D {YYYY-MM-DD HH: mm: ss} [% C ] - [% P]% M% N

Configure root Logger, whose syntax is:

Log4j.rootlogger = [level], appendername, appendername, ...

Level is the priority of logging

Appendername is where the specified log information is output to which place. You can specify multiple output destinations at the same time.

Configure the log information output Destination Appender, its syntax is

Log4j.Appender.Appendername = flly.qualified.name.of.Appender.class

Log4j.appender.Appendername.Option1 = value1

...

Log4j.appender.Appendername.Option = VALUEN

The Appender provided by log4j has the following:

Org.apache.log4j.consoleappender (console),

Org.apache.log4j.fileappender (file),

Org.apache.log4j.dailyrollingFileAppender (a log file is generated),

Org.apache.log4j.rollingfileAppender generated a new file when the file is reached to specify the size,

Org.apache.log4j.writerappender (send log information to any specified place in stream format)

Configure the format (layout) of log information, whose syntax is:

Log4j.Appender.Appendername.Layout = Fully.qualified.Name.Of.Layout.class

Log4j.Appender.Appendername.Layout.Option1 = Value1

....

Log4j.Appender.Appendername.Layout.Option = VALUEN

Layout provided by LOG4J has the following:

Org.apache.log4j.htmlLayout (layout in HTML form),

Org.apache.log4j.patternLayout (can be flexibly specified), org.apache.log4j.simplelayout (including level and information string containing log information),

Org.apache.log4j.ttccLayout (including information, thread, category, etc.)

转载请注明原文地址:https://www.9cbs.com/read-15040.html

New Post(0)