Learning little drip (2)

zhaozj2021-02-16  51

3.4 Configuring the interface

Commons-logging configuration can be set in the system properties, but this is a bad habit, system properties affect all classes under the same JVM, and is not good to control, so configuring the best position of Commons-logging in Commons-logging .properties file. When configuring Commons-Logging using the properties file, you must put it under the ClassPath of the software system.

The following two attribute items can be set to the following two attribute items in Commons-logging.properties: Org.apache.commons.logging.log

The default LogFactory of Commons-Logging is instantified as an instance of the application programming interface log in accordance with the value of this item. If this item is not set, logfactory searches for classes to implement the log interface in the orderpath of the software system:

Log4j

JSDK 1.4

JCL Simplelog

This rule we call it a search strategy. Org.apache.commons.logging.logfactory This item covers the default LogFactory implementation, with

To meet the specific needs of the application, such as redefining the search strategy. 4. Program list:

Note: The following two PropERIES files are placed in the class of the classs, and the log package will automatically find them, then compile the running program, you can see the results of the record log.

Commons-logging.properties

# Set the record log Use log4j

Org.apache.commons.logging.log = org.apache.commons.logging.Impl.log4jlogger

Org.apache.commons.logging.logfactory = org.apache.commons.logging.impl.log4jfactory

# Set the record log Using JDK1.4 logging package

# org.apache.commons.logging.log = org.apache.commons.logging.Impl.jdk14logger

# Set the record log to use Logkit

# org.apache.commons.logging.log = org.apache.commons.logging.impl.logkitlogger

# Set the record log Using Simplelog

# org.apache.commons.logging.log = org.apache.commons.logging.Impl. Simplelog

2. Log4j.properties

! log4j Analysis of this attribute file

Log4j.debug = false

! The following two properties don't know what to use

Log4j.disableOverride = TRUE

Log4j.disable = info

! Set the priority of all class logs

Log4j.rootcategory = debug, dest1, dest2

! Set this package record log as a fake, dist1, dist2 will not record the log of org.jarrywen.sample.logging.Therpackagelog.loggingSample, only DIST3 will record

! Conversely, record, this will repeat the record

Log4j.additivity.org.jarrywen.sample.logging.TherpackageLog.loggingSample = false

! Special specify the level and target device for a particular package log

Log4j.category.org.jarrywen.sample.logging.TherpackageLog.loggingSample = error, DEST3

! This target device is used to debug

Log4j.Appender.Dest1 = org.apache.log4j.consoleappender # log4j.Appender.Dest1.Layout = org.apache.log4j.simpleLayout

Log4j.Appender.Dest1.Layout = org.apache.log4j.patternlayout

Log4j.Appender.Dest1.Layout.conversionPattern =% D% p% C - <% m> (% F.% m:% L)% T% N

! This target device is used to trace

Log4j.Appender.Dest2 = org.apache.log4j.rollingfileappender

! The level of the document log log (the level of INFO is not recorded)

Log4j.Appender.Dest2.threshold = WARN

! File Save Path

Log4j.Appender.Dest2.file = c: /log4jlog.htm

! Whether to add a message to the file

Log4j.Appender.Dest2.Append = TRUE

! Set the maximum value of the file

Log4j.Appender.Dest2.maxfilesize = 100kb

! Set the maximum number of backup files

Log4j.Appender.Dest2.maxbackupindex = 10

! Use an HTML format to record the log

Log4j.Appender.Dest2.Layout = org.apache.log4j.htmllayout

! Whether to print the message line of the message

Log4j.Appender.Dest2.Layout.LocationInfo = true

! Set the title of the HTML file of the log

Log4j.Appender.Dest2.Layout.title = my app title

! This target device is used to specify the class or package

Log4j.Appender.Dest3 = org.apache.log4j.dailyrollingFileAppender

# log4j.Appender.Dest3.threshold = Error

Log4j.Appender.Dest3.file = c: /specpackagelog.htm

Log4j.Appender.Dest3.Append = false

Log4j.Appender.Dest3.Layout = org.apache.log4j.htmllayout

Log4j.Appender.Dest3.Layout.LocationInfo = true

Log4j.Appender.Dest3.Layout.title = my app title

3. Loggingsample.java

Package Org.jarrywen.sample.logging;

Import org.apache.commons.logging.log;

Import org.apache.commons.logging.logfactory;

Public class loggingsample {

Private log log = logfactory.getlog (getclass (). getname ());

Public static void main (String [] args) {

Loggingsample loggingsample = new loggingsample ();

// Trace, Debug, Info, Warn, Error, Fatal

// Take a look at a log record

Loggingsample.trace ();

Loggingsample.debug ();

Loggingsample.info ();

Loggingsample.warn (); loggingsample.error ();

Loggingsample.fatal ();

Org.jarrywen.sample.logging.jarrywagelog.loggingsample loggingSample1 = new org.jarrywen.sample.logging.jarrywen.sample.logging.loggingSample ();

Loggingsample1.trace ();

Loggingsample1.debug ();

Loggingsample1.info ();

Loggingsample1.warn ();

Loggingsample1.Error ();

Loggingsample1.fatal ();

}

Public loggingsample () {

}

Public void trace () {

// if (log.IstraceNabled ())

Log.Trace ("TRACE --- System Start Trace Record Log");

}

Public void debug () {

// if (log.IndebugeNabled ())

Log.debug ("Debug --- System Start Debug Record Log");

}

Public void info () {

// if (log.isinfoenabled ())

Log.info ("INFO --- System Start INFO Record Log");

}

Public void warn () {

// if (log.iswarnenabled ())

Log.warn ("Warn --- System Start WARN Record Log");

}

Public void error () {

// if (log.iserrorenabled ()) {

Log.Error ("Error - System Start Error Record Log", New Throwable ("Test Log.Error");

//}

}

Public void fat () {

// if (log.isfatalenabled ())

Log.fatal ("Fatal --- System Start Fatal Record Log", New Throwable ("Test Log.FATAL");

}

}

4. Org.jarrywen.sample.logging .loggingsample.java

Package org.jarrywen.sample.logging.otherpackagelog;

Import org.apache.commons.logging.log;

Import org.apache.commons.logging.logfactory;

Public class loggingsample {

Private log log = logfactory.getlog (getclass (). getname ());

Public static void main (String [] args) {

Loggingsample loggingsample = new loggingsample ();

// Trace, Debug, Info, Warn, Error, Fatal

// Take a look at a log record

Loggingsample.trace ();

Loggingsample.debug ();

Loggingsample.info ();

Loggingsample.warn ();

Loggingsample. error ();

Loggingsample.fatal ();

}

Public loggingsample () {

}

Public void trace () {

// if (log.isticraceenabled ()) log.trace ("TRACE --- System Start Trace Record Log);

}

Public void debug () {

// if (log.IndebugeNabled ())

Log.debug ("Debug --- System Start Debug Record Log");

}

Public void info () {

// if (log.isinfoenabled ())

Log.info ("INFO --- System Start INFO Record Log");

}

Public void warn () {

// if (log.iswarnenabled ())

Log.warn ("Warn --- System Start WARN Record Log");

}

Public void error () {

// if (log.iserrorenabled ()) {

Log.Error ("Error - System Start Error Record Log", New Throwable ("Test Log.Error");

//}

}

Public void fat () {

// if (log.isfatalenabled ())

Log.fatal ("Fatal --- System Start Fatal Record Log", New Throwable ("Test Log.FATAL");

}

}

Learning little drip (2)

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

New Post(0)