锘??xml version="1.0" encoding="utf-8" standalone="yes"?>亚洲欧美在线x视频,亚洲成a人片在线观看中文动漫 ,亚洲人成在线免费观看http://www.tkk7.com/RR00/category/2663.html涓嶈鍩嬪ご鑻﹀共錛岃瀛︿範錛屽涔狅紝鍐嶅涔犮傘傘傘傘? <br> powered by <font color='orange'>R.Zeus</font>zh-cnTue, 27 Feb 2007 10:37:59 GMTTue, 27 Feb 2007 10:37:59 GMT60Set the Logging Level Over a Tomcat Cluster Dynamicallyhttp://www.tkk7.com/RR00/articles/69693.htmlR.ZeusR.ZeusThu, 14 Sep 2006 09:03:00 GMThttp://www.tkk7.com/RR00/articles/69693.htmlhttp://www.tkk7.com/RR00/comments/69693.htmlhttp://www.tkk7.com/RR00/articles/69693.html#Feedback0http://www.tkk7.com/RR00/comments/commentRss/69693.htmlhttp://www.tkk7.com/RR00/services/trackbacks/69693.html
Set the Logging Level Over a Tomcat Cluster Dynamically
Rather than modifying your Log4J configuration and then redeploying the correct code version with the modified logging level, you can dynamically set the Log4J logging level over each node in a Tomcat cluster with JMX.聽


henever you have to fix a problem in a production environment, one of the most common issues is retrieving some useful debug information to tell you what happened. Of course, to avoid needless notifications and enormous log files, most production systems are configured to log only errors. To avoid having to modify your Log4J configuration and then redeploy the correct version of the code with the modified logging level, why not just implement a mechanism for dynamically changing the logging level?聽

Log4J registers a number of MBeans, one of which provides access to the root logger. You can dynamically modify the logging level of the root logger by implementing the following servlet to set the appropriate MBean property through JMX:

public class SetLog4jLevelServlet extends HttpServlet {
    private static final Log log = LogFactory.getLog(SetLog4jLevelServlet.class);

    protected void doGet(HttpServletRequest request, 
            HttpServletResponse response) 
            throws ServletException, IOException {
        List list = MBeanServerFactory.findMBeanServer(null);
        MBeanServer server  = (MBeanServer)list.iterator().next();

        try {
            String loggingLevel = request.getParameter("level");
            Attribute attribute = new Attribute("priority", loggingLevel);
            ObjectName objectName = new ObjectName("log4j:logger=root");
            server.setAttribute(objectName, attribute);
        } catch (Exception e) {
            log.error("log4j:logger=root", e);
        }
    }
}

The first two lines of the doGet method look up the MBeanServer. Log4J registers the root logger with the name log4j:logger=root. This MBean provides a property to set the logging level of the root logger. You set the priority of the root logger by passing the ObjectName of the MBean to operate on, along with an instance of javax.management.Attribute to the MBean Server. In this case, you want to set the "priority" attribute. The value will be the new logging level you have specified

A Log4J MBean for Each Log4J Configuration

Although this technique seems simple, if your Tomcat cluster hosts a number of Web applications鈥攅ach with its own Log4J configuration that allows it to log to a separate log file鈥攖hen you will run into your first problem. Given multiple Log4J configurations, only the first one loaded by the JVM will have a root logger MBean associated with it. The reason for this is Log4J hard-codes the JMX Domain that it uses when registering its MBeans as log4j, and the JMX server doesn't allow duplicate names.

The way round this is to leave a Tomcat server Log4J configuration (jars under server/lib and property files under server/classes) registered under the default Log4J name log4j:logger=root. Then register the root logger for each Web application in a startup servlet, using the Web application name to identify the domain:


public class RegisterLog4JMBeanServlet extends HttpServlet {

    private static final Log log = LogFactory.getLog(RegisterLog4JMBeanServlet.class);

    public void init(ServletConfig servletConfig) 
            throws ServletException {
        ServletContext context = servletConfig.getServletContext();
        String webappName = context.getServletContextName();
        Hashtable<String, String> nameParameters = new Hashtable<String, String>();
        nameParameters.put("logger", "root");

        Logger rootLogger = LogManager.getRootLogger();
        LoggerDynamicMBean mbean = new LoggerDynamicMBean(rootLogger);
        List list = MBeanServerFactory.findMBeanServer(null);
        MBeanServer server  = (MBeanServer)list.iterator().next();

        ObjectName objectName = null;
        try {
            objectName = new ObjectName(webappName, nameParameters);
            server.registerMBean(mbean, objectName);
        } catch (Exception e) {
            log.error("Problems registering Log4J MBean", e);
        }
    }
}

You will end up with an MBean named <web-app-name>: logger=root for each Web application. The webappName is retrieved from the <display-name> element in your application's web.xml file, and the application's root logger is retrieved by calling LogManager.getRootLogger(). Then you can use the LoggerDynamicMBean class from Log4J to create your MBean from the root logger definition.

To create a distinct ObjectName for the new MBean, you should use the Web application name as the JMX domain (this is the text before the colon) and then register the Root Logger MBean with the new ObjectName. This will guard against conflicts with other Log4J MBeans.

Now that you have registered your Root Logger under a different domain, you must modify the servlet that sets the logging level. So in SetLog4JlevelServlet, change this:


ObjectName objectName = new ObjectName("log4j:logger=root");

To this:


ServletContext context = servletConfig.getServletContext();
String webappName = context.getServletContextName();
Hashtable<String, String> nameParameters = new Hashtable<String, String>();
nameParameters.put("logger", "root");
ObjectName objectName = new ObjectName(webappName, nameParameters);

When setting up your Log4J configuration, give your Log4J appenders unique names. Otherwise, they will not get registered to the JMX server and you will get lots of the following errors in your log files at startup:


ERROR main org.apache.log4j.jmx.LoggerDynamicMBean ? - Could not add appenderMBean for [<appender_name>].
javax.management.InstanceAlreadyExistsException: log4j:appender=<appender_name>

Again, the reason for this is because the JMX domain name his hard-coded to log4j, so if you have repeated appender names then only the first of these will be registered.

At this point, configuring the MX4J HTTP adaptor in your Tomcat server might be useful. This will give you a visual representation of the MBeans you are creating and manipulating, and show you what else is exposed via MBeans within Tomcat. To do this, you must put the mx4j-tools.jar file (download it at mx4j.sourceforge.net) in your common/lib directory. Then configure your server.xml file to set up the connector as follows:


    <Connector port="8010" 
           handler.list="mx" 
           mx.enabled="true" 
           mx.httpHost="localhost"
           mx.httpPort="8013"
           protocol="AJP/1.3"/>

When you start your server, you will be able to access the MX4J HTTP connector through your browser at http://localhost:8013, assuming you have no other Tomcat connector running on port 8010.

Set the Logging Level for All the Nodes in Your Cluster

Once you have made the previously discussed changes, you will be able to set the Log4J logging level dynamically for any application running in Tomcat. The next challenge is to make sure you can set the logging level for every node in the cluster. You could either call this servlet manually on each node in the cluster or get the servlet to send a message to every other node in the cluster so the logging levels are always the same across the cluster

To send a message to the other nodes in a Tomcat cluster, you need to call the send(ClusterMessage) method on the org.apache.catalina.cluster.SimpleTcpCluster class. When implementing a ClusterMessage, you must populate the details of the cluster node that was responsible for sending the request. (Listing 1 provides an example that retrieves the information through the Tomcat MBeans.) The first step is to provide an implementation of the ClusterMessage interface to pass the new logging level and to determine for which application you want to set the logging level.

It may seem strange, but you must use JMX over RMI to send the message to the cluster because the interface implemented in Listing 1 is defined within the Tomcat server class loader (i.e., it exists in a jar file in the server directory). You will instantiate that class in a Web application, which uses a totally separate class loader (it exists in the WEB-INF directory of your Web application). This means that the cluster message you instantiate in your Web application and send to the SimpleTcpCluster class will be different from the cluster message definition that is placed in the Tomcat server class loader. If you try to send your message directly through JMX, your cluster message instance will not match the cluster message definition accessible to the SimpleTcpCluster class and the call will fail. (See the Tomcat documentation on class loaders for a definition of how the class loaders are configured for the Tomcat server.)

The workaround is to use JMX over RMI so the cluster message is serialized in the Web application class loader, instantiated in the Tomcat server class loader (and that is the important point), and then de-serialized. The following method opens a connection to the local JVM's JMX server over RMI, looks up the MBean that wraps the SimpleTcpCluster, and then invokes the send(ClusterMessage) operation on the MBean:


public static void sendLogMessageEvent(String applicationName, String logLevel) {
    String port = System.getProperty("com.sun.management.jmxremote.port");
    String urlForJMX = JMX_SERVICE_PREFIX + HOST + ":" + port + JMX_SERVICE_SUFFIX;

    try {
        JMXServiceURL url = new JMXServiceURL(urlForJMX);
        JMXConnector connector = JMXConnectorFactory.connect(url, null);
        MBeanServerConnection server = connector.getMBeanServerConnection();
        ObjectName cluster = new ObjectName("Catalina:type=Cluster,host=localhost");
        log.debug("Cluster cluster: " + cluster.toString());

        LogLevelMessage message = new LogLevelMessage();
        message.setLoggingLevel(logLevel);
        message.setApplication(applicationName);

        Object[] params = new Object[] {message};
        String[] types = new String[] {"org.apache.catalina.cluster.ClusterMessage"};
        server.invoke(cluster, "send", params, types);
    } catch (Exception e) {
        log.error(e.getMessage(), e);
    }
}

You should call this method in your SetLog4JLevelServlet to ensure that a message is sent to the other members of the cluster when you set the logging level.

You will need to configure your Tomcat server to allow a JMX connection to be opened up over RMI. Simply add the following line to your catalina.bat file in the bin directory:


set JAVA_OPTS=%JAVA_OPTS% -Dcom.sun.management.jmxremote.port=8012 
-Dcom.sun.management.jmxremote.authenticate=false 
-Dcom.sun.management.jmxremote.ssl=false

For a quick guide on applying security to this configuration, see the Tomcat documentation. For a more in-depth look at configuring JMX Remote, see the Sun documentation.

Receiving the Message in the Other Nodes

Once you are able to send these messages to the other nodes in the cluster, you will need to implement a Tomcat ClusterListener to receive the events. This implementation will set the logging level for the correct MBean determined by the application name that is passed as part of the message:

public class ClusterLoggingListener extends ClusterListener {

    private static final Log log = LogFactory.getLog(ClusterLoggingListener.class);
    
    public void messageReceived(ClusterMessage message) {
        if(message instanceof LogLevelMessage) {
            LogLevelMessage logMessage = (LogLevelMessage) message;
            List list = MBeanServerFactory.findMBeanServer(null);
            MBeanServer server  = (MBeanServer)list.iterator().next();
            Hashtable<String, String> nameParameters = new Hashtable<String, String>();
            nameParameters.put("logger", "root");
            try {
                ObjectName logBean = new ObjectName(logMessage.getApplication(), nameParameters);
                Attribute attribute = new Attribute("priority", logMessage.getLoggingLevel());
                server.setAttribute(logBean, attribute);
            } catch (Exception e) {
                log.error("Problem setting the logging level for application " + 
                        logMessage.getApplication() + " to level " + 
                        logMessage.getLoggingLevel(),e);
            }
        }
    }
    public boolean accept(ClusterMessage message) {
        return message instanceof LogLevelMessage;
    }
}

You must deploy this class in the server directory and enter it in the Tomcat server.xml file as a ClusterListener within the <Cluster> element:


<ClusterListener className="dynamiclogging.tomcat.ClusterLoggingListener"/>

You should now be able to set the logging level for any application, over all nodes, in the cluster by calling SetLog4JLevelServlet on any node in the cluster.

Deploying the Example Code

To deploy the example code for setting the logging level in your cluster, take the following steps:
  1. Load the Eclipse project and build it using the dist target. This will create set-log-levels.war and set-log-levels-server.jar in the dist directory.
  2. Put the set-log-levels-server.jar in the Tomcat server\lib directory and deploy the set-log-levels.war file across the cluster.
  3. Use the SetLog4jLevelServlet (e.g., /setLogLevel?level=debug) to set the logging level and use PerformLogTestServlet (/testLogLevel) to test the logging level changes.



R.Zeus 2006-09-14 17:03 鍙戣〃璇勮
]]>
log4j in tomcathttp://www.tkk7.com/RR00/articles/69638.htmlR.ZeusR.ZeusThu, 14 Sep 2006 06:41:00 GMThttp://www.tkk7.com/RR00/articles/69638.htmlhttp://www.tkk7.com/RR00/comments/69638.htmlhttp://www.tkk7.com/RR00/articles/69638.html#Feedback0http://www.tkk7.com/RR00/comments/commentRss/69638.htmlhttp://www.tkk7.com/RR00/services/trackbacks/69638.htmllog4j.xml 鍙互鏀懼湪2涓湴鏂癸細
1銆変嬌鐢?strong>springframework鍦╳eb.xml涓厤緗?br />//璁劇疆webAppRootKey鐨勫埆鍚嶏紝
//濡傛灉涓嶈緗紝鍦╨og4j.xml涓緗負<param name="File" value="${webapp.root}/WEB-INF/log/frame.log " />
//鍚﹀垯錛屽彲浠ヨ緗負<param name="File" value="${demo.root}/WEB-INF/log/frame.log " />
//濡傛灉涓嶄嬌鐢?font color="#0000ff">${webapp.root
}錛屽氨浼氭寚鍚?{TOMCAT_HOM},涔熷氨鏄痶omcat鐨勫畨瑁呯洰褰?font color="#ff0000">銆?/font>
聽<context-param>
聽聽<param-name>webAppRootKey</param-name>
聽聽<param-value>demo.root</param-value>
聽</context-param>

//璁劇疆閰嶇疆鏂囦歡聽聽聽
<context-param>
聽聽聽聽聽聽聽 <param-name>log4jConfigLocation</param-name>
聽聽聽聽聽聽聽 <param-value>/WEB-INF/log4j.xml</param-value>
聽聽聽 </context-param>

//璁劇疆鐩戝惉鍣?br />聽 <listener>
聽聽<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
聽</listener>
榪欐牱log4j.xml 灝卞彲浠EB-INF閲岄潰鐨勪換浣曡礬寰勩?br />
Source:

initLogging->
{
聽聽聽聽聽聽WebUtils.setWebAppRootSystemProperty(servletContext);
聽聽聽聽聽聽{
聽聽聽聽聽聽String root = servletContext.getRealPath("/");
聽聽聽聽聽聽String param = servletContext.getInitParameter(WEB_APP_ROOT_KEY_PARAM);
聽聽聽聽聽聽聽String key = (param != null ? param : DEFAULT_WEB_APP_ROOT_KEY);
聽聽聽聽聽聽聽聽System.setProperty(key, root);
聽聽聽聽聽聽//use system property聽to聽deal with
${webapp.root} or聽its聽alias.
聽聽聽聽聽聽//once聽I聽writed careless聽${webapp.root}聽to {webapp.root}聽,and聽look up聽the error for聽much time.
聽聽聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽String location = servletContext.getInitParameter(CONFIG_LOCATION_PARAM);
聽聽聽聽聽聽Log4jConfigurer.initLogging(location);
聽聽聽聽聽聽{

聽聽聽聽聽聽聽聽聽//distinguish the log4j.xml聽and聽log4j.properties
聽聽聽聽聽聽聽聽聽if (resolvedLocation.toLowerCase().endsWith(XML_FILE_EXTENSION)) {
聽聽聽聽聽聽聽聽聽聽聽聽DOMConfigurator.configure(url);
聽聽聽聽聽聽聽聽}
聽聽聽聽聽else {
聽聽聽聽聽聽聽聽聽PropertyConfigurator.configure(url);
聽聽聽聽聽}
聽聽聽聽聽聽}
}
use DOMConfigurator.configure("log4j.xml"); to init config ,then LogFactory can work. the "log4j.xml "
may be any name .DOMConfigurator may use cache mechanism聽as聽in LogFactory class .cache machanism is
another way to implement "single" design pattern.
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2銆塛EB-INF/classes涓嬮潰

閲囧彇榪欑鏂瑰紡錛屽畠鐨勬枃浠禷ppender鐨勭浉瀵硅礬寰勬繪槸鎸囧悜${TOMCAT_HOM},涔熷氨鏄痶omcat鐨勫畨瑁呯洰褰?font color="#ff0000">銆?lt;param name="File" value="demo.log" /> 浼氭妸demo.log 淇濆瓨鍒?/font>${TOMCAT_HOM}/demo.log 錛?br />閲囩敤緇濆璺緞鍒欏彲浠ヤ換鎰忚緗?br />
涓轟簡浣跨敤鐩稿璺緞錛屾垜浠妸project鏀懼埌${TOMCAT_HOMe}/webapps涓媎eploy銆?br />濡俻roject鍚嶄負demo錛宭og瑕佷繚瀛樺湪demo/log/demo.log鏂囦歡閲岋紝鍒欏彲浠ヨ涓猴細
<param name="File" value="webapps\\demo\\log\\framefile.log" />

---------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------
娉ㄦ剰鈥淺" 瑕佺敤杞箟瀛楃"\\",濡傛灉娌″姞,tomcat鍚姩灞忎腑鐨勮礬寰勪細鍙樹負涔辯爜.

tomcat涓技涔庨噰鍙栦簡鏌愮瀹夊叏絳栫暐
,<param name="Append" value="false" />涓嶈搗浣滅敤銆?br />
鏈嶅姟鍣ㄩ噸璧峰悗灝變細鏂板緩涓涓猯og鏂囦歡.



R.Zeus 2006-09-14 14:41 鍙戣〃璇勮
]]>
log4j.propertieshttp://www.tkk7.com/RR00/articles/9845.htmlR.ZeusR.ZeusThu, 11 Aug 2005 14:30:00 GMThttp://www.tkk7.com/RR00/articles/9845.htmlhttp://www.tkk7.com/RR00/comments/9845.htmlhttp://www.tkk7.com/RR00/articles/9845.html#Feedback0http://www.tkk7.com/RR00/comments/commentRss/9845.htmlhttp://www.tkk7.com/RR00/services/trackbacks/9845.htmlLog4j-----how to use in commons-logging-1.0.4.jar

Logger - 鏃ュ織鍐欏嚭鍣紝渚涚▼搴忓憳杈撳嚭鏃ュ織淇℃伅

Appender - 鏃ュ織鐩殑鍦幫紝鎶婃牸寮忓寲濂界殑鏃ュ織淇℃伅杈撳嚭鍒版寚瀹氱殑鍦版柟鍘?
ConsoleAppender - 鐩殑鍦頒負鎺у埗鍙扮殑Appender
FileAppender - 鐩殑鍦頒負鏂囦歡鐨凙ppender
RollingFileAppender - 鐩殑鍦頒負澶у皬鍙楅檺鐨勬枃浠剁殑Appender

Layout - 鏃ュ織鏍煎紡鍖栧櫒錛岀敤鏉ユ妸紼嬪簭鍛樼殑logging request鏍煎紡鍖栨垚瀛楃涓?
PatternLayout - 鐢ㄦ寚瀹氱殑pattern鏍煎紡鍖杔ogging request鐨凩ayout


### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

### direct messages to file hibernate.log ###
#log4j.appender.file=org.apache.log4j.FileAppender
#log4j.appender.file.File=hibernate.log
#log4j.appender.file.layout=org.apache.log4j.PatternLayout
#log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

### set log levels - for more verbose logging change 'info' to 'debug' ###

log4j.rootLogger=warn, stdout    #鎵鏈夐厤緗戶鎵挎閰嶇疆(鍦ㄦ鍩虹澧炲姞),鍙笉瑕?BR>#鍙畾涔夎嚜宸辯殑綰у埆:
log4j.logger.test=info
#log4j.logger蹇呴渶;鍏跺悗鍙姞package name or class name;

浣跨敤:
package test;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @author herald
 *
 * TODO 瑕佹洿鏀規鐢熸垚鐨勭被鍨嬫敞閲婄殑妯℃澘錛岃杞嚦 紿楀彛 錛?棣栭夐」 錛?Java 錛?浠g爜鏍峰紡 錛?浠g爜妯℃澘
 */
public class Test {

 public static void main(String[] args) {
  Log log = LogFactory.getLog("test");         鍏跺鏋滃瓨鍦?test",涓嶉噸鏂板緩绔媗og  
  log.info("aa");
  log.error("bbbbbb");
 }
}


婧愮爜:
CategoryKey key = new CategoryKey(name);
  // Synchronize to prevent write conflicts. Read conflicts (in
  // getChainedLevel method) are possible only if variable
  // assignments are non-atomic.
  Logger logger;

  synchronized (ht) {
   Object o = ht.get(key);
   if (o == null) {
    logger = factory.makeNewLoggerInstance(name);
    logger.setHierarchy(this);
    ht.put(key, logger);
    updateParents(logger);
    return logger;
   } else if (o instanceof Logger) {
    return (Logger) o;
   } else if (o instanceof ProvisionNode) {
    //System.out.println("("+name+") ht.get(this) returned
    // ProvisionNode");
    logger = factory.makeNewLoggerInstance(name);
    logger.setHierarchy(this);
    ht.put(key, logger);
    updateChildren((ProvisionNode) o, logger);
    updateParents(logger);
    return logger;
   } else {
    // It should be impossible to arrive here
    return null; // but let's keep the compiler happy.
   }
  }
 }



R.Zeus 2005-08-11 22:30 鍙戣〃璇勮
]]>
Log4j-----how to use in commons-logging-1.0.4.jar http://www.tkk7.com/RR00/articles/9844.htmlR.ZeusR.ZeusThu, 11 Aug 2005 14:22:00 GMThttp://www.tkk7.com/RR00/articles/9844.htmlhttp://www.tkk7.com/RR00/comments/9844.htmlhttp://www.tkk7.com/RR00/articles/9844.html#Feedback0http://www.tkk7.com/RR00/comments/commentRss/9844.htmlhttp://www.tkk7.com/RR00/services/trackbacks/9844.html
commons-logging-1.0.4.jar 浣跨敤

Configuring the Commons Logging Package

Choosing a LogFactory Implementation

From an application perspective, the first requirement is to retrieve an object reference to the LogFactory instance that will be used to create Log instances for this application. This is normally accomplished by calling the static getFactory() method. This method implements the following discovery algorithm to select the name of the LogFactory implementation class this application wants to use:

  • Check for a system property named org.apache.commons.logging.LogFactory.
  • Use the JDK 1.3 JAR Services Discovery mechanism (see http://java.sun.com/j2se/1.3/docs/guide/jar/jar.html for more information) to look for a resource named META-INF/services/org.apache.commons.logging.LogFactory whose first line is assumed to contain the desired class name.
  • Look for a properties file named commons-logging.properties visible in the application class path, with a property named org.apache.commons.logging.LogFactory defining the desired implementation class name.
  • Fall back to a default implementation, which is described further below.

If a commons-logging.properties file is found, all of the properties defined there are also used to set configuration attributes on the instantiated LogFactory instance.

浣跨敤蹇呴』:
commons-logging-1.0.4.jar
log4j-1.2.9.jar
log4j.properties



R.Zeus 2005-08-11 22:22 鍙戣〃璇勮
]]>
Log4J 浣跨敤璇存槑http://www.tkk7.com/RR00/articles/9827.htmlR.ZeusR.ZeusThu, 11 Aug 2005 08:13:00 GMThttp://www.tkk7.com/RR00/articles/9827.htmlhttp://www.tkk7.com/RR00/comments/9827.htmlhttp://www.tkk7.com/RR00/articles/9827.html#Feedback0http://www.tkk7.com/RR00/comments/commentRss/9827.htmlhttp://www.tkk7.com/RR00/services/trackbacks/9827.html Logger錛氭棩蹇楀啓鍑哄櫒

Logger瀵硅薄鏄敤鏉ュ彇浠?TT>System.out鎴栬?TT>System.err鐨勬棩蹇楀啓鍑哄櫒錛岀敤鏉ヤ緵紼嬪簭鍛樿緭鍑烘棩蹇椾俊鎭?/P>
Logger鐨勮緭鍑烘柟娉?/B>

Logger綾誨璞℃彁渚涗竴緋誨垪鏂規硶渚涚▼搴忓憳杈撳嚭鏃ュ織淇℃伅銆?/P>

        

        ------ Log4j APIs : class Logger ------
        
        
        
        // Printing methods :
        
        public void debug(Object msg);
        public void debug(Object msg, Throwable t);
        
        public void info(Object msg);
        public void info(Object msg, Throwable t);
        
        public void warn(Object msg);
        public void warn(Object msg, Throwable t);
        
        public void error(Object msg);
        public void error(Object msg, Throwable t);
        
        public void fatal(Object msg);
        public void fatal(Object msg, Throwable t);
        
        // Generic printing method :
        
        public void log(Level l, Object msg);
      


Logger鐨勫懡鍚嶈鍒?/B>

Logger鐢變竴涓猄tring綾葷殑鍚嶅瓧璇嗗埆錛宭ogger鐨勫悕瀛楁槸澶у皬鍐欐晱鎰熺殑錛屼笖鍚嶅瓧涔嬮棿鍏鋒湁緇ф壙鐨勫叧緋伙紝瀛愬悕鏈夌埗鍚嶄綔涓哄墠緙錛岀敤鐐瑰彿.鍒嗛殧銆傚錛?TT>x.y鏄?TT>x.y.z鐨勭埗浜層?/P>

鏍筶ogger (root logger)鏄墍鏈塴ogger鐨勭鍏堬紝瀹冨叿鏈夊涓嬪睘鎬э細1) 瀹冩繪槸瀛樺湪鐨勶紱2) 瀹冧笉鍙互閫氳繃鍚嶅瓧鑾峰緱銆?/P>

閫氳繃璋冪敤public static Logger Logger.getRootLogger()鑾峰緱root logger錛涢氳繃璋冪敤public static Logger Logger.getLogger(String name)鎴栬?TT>public static Logger Logger.getLogger(Class clazz)鑾峰緱錛堟垨鑰呭垱寤猴級涓涓?TT>named logger銆傚悗鑰呯浉褰撲簬璋冪敤Logger.getLogger(clazz.getName())銆?/P>

鍦ㄦ煇瀵硅薄涓紝鐢ㄨ瀵硅薄鎵灞炵殑綾諱負鍙傛暟錛岃皟鐢?TT>Logger.getLogger(Class clazz)浠ヨ幏寰條ogger琚涓烘槸鐩墠鎵鐭ョ殑鏈鐞嗘櫤鐨勫懡鍚峫ogger鐨勬柟娉曘?/P>


Log level

姣忎釜logger閮借鍒嗛厤浜嗕竴涓棩蹇楃駭鍒?(log level)錛岀敤鏉ユ帶鍒舵棩蹇椾俊鎭殑杈撳嚭銆傛湭琚垎閰峫evel鐨刲ogger灝嗙戶鎵垮畠鏈榪戠殑鐖秎ogger鐨刲evel銆?/P>

姣忔潯杈撳嚭鍒發ogger鐨勬棩蹇楄姹?logging request)涔熼兘鏈変竴涓猯evel錛屽鏋滆request鐨刲evel澶т簬絳変簬璇ogger鐨刲evel錛屽垯璇equest灝嗚澶勭悊錛堢О涓篹nabled錛夛紱鍚﹀垯璇equest灝嗚蹇界暐銆傛晠鍙緱鐭ワ細

  1. logger鐨刲evel瓚婁綆錛岃〃紺鴻logger瓚婅緇?
  2. logging request鐨刲evel瓚婇珮錛岃〃紺鴻logging request瓚婁紭鍏堣緭鍑?

Level綾諱腑棰勫畾涔変簡浜斾釜level錛屽畠浠殑澶у皬鍏崇郴濡備笅錛?/P>

        Level.ALL < Level.DEBUG < Level.INFO < Level.WARN < Level.ERROR < Level.FATAL < Level.OFF
      


紺轟緥浠g爜

浠ヤ笅浠g爜灝嗙敤鑷繁鎵灞炵殑綾諱負鍙傛暟錛屽垱寤轟竴涓猯ogger錛屽惎鐢ㄩ粯璁ら厤緗紝璁劇疆鍏秎evel騫跺悜鍏惰緭鍑鴻嫢騫瞝ogging request銆?/P>

import org.apache.log4j.Logger;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Level;

public class Log4jTest {
    public static void main(String argv[]) {
    
        
        // Create a logger by the name of class Log4jTest.
        
        Logger logger = Logger.getLogger(Log4jTest.class);
        
        // Use the default configuration.
        
        BasicConfigurator.configure();
        
        // Set the logger level to Level.INFO
        
        logger.setLevel(Level.INFO);
        
        // This request will be disabled since Level.DEBUG < Level.INFO.
                  
        logger.debug("This is debug.");
        
        // These requests will be enabled.
                  
        logger.info("This is an info.");
        logger.warn("This is a warning.");
        logger.error("This is an error.");
        logger.fatal("This is a fatal error.");
        
        return;
    }
}
      


鍏充簬logger鐨勪袱鐐硅鏄?/B>

  1. 鐢ㄥ悓鍚嶅弬鏁拌皟鐢?TT>Logger.getLogger(String name)灝嗚繑鍥炲悓涓涓猯ogger鐨勫紩鐢ㄣ傛晠鍙互鍦ㄤ竴涓湴鏂歸厤緗甽ogger錛屽湪鍙﹀涓涓湴鏂硅幏寰楅厤緗ソ鐨刲ogger錛岃屾棤欏葷浉浜掗棿浼犻抣ogger鐨勫紩鐢ㄣ?
  2. logger鐨勫垱寤哄彲浠ユ寜鐓т換鎰忕殑欏哄簭錛屽嵆錛岀埗logger鍙互鍚庝簬瀛恖ogger琚垱寤恒俵og4j灝嗚嚜鍔ㄧ淮鎶ogger鐨勭戶鎵挎爲銆?



Appender錛氭棩蹇楃洰鐨勫湴

姣忎釜logger閮藉彲浠ユ嫢鏈変竴涓垨鑰呭涓猘ppender錛屾瘡涓猘ppender琛ㄧず涓涓棩蹇楃殑杈撳嚭鐩殑鍦幫紝姣斿console鎴栬呮煇涓枃浠躲傚彲浠ヤ嬌鐢?TT>Logger.addAppender(Appender app)涓簂ogger澧炲姞涓涓猘ppender錛涘彲浠ヤ嬌鐢?TT>Logger.removeAppender(Appender app)涓簂ogger縐婚櫎涓涓猘ppender銆?/P>

榛樿鎯呭喌涓嬶紝logger鐨刟dditive鏍囧織琚緗負true錛岃〃紺哄瓙logger灝嗙戶鎵跨埗logger鐨勬墍鏈塧ppenders銆傝閫夐」鍙互琚噸鏂拌緗紝琛ㄧず瀛恖ogger灝嗕笉鍐嶇戶鎵跨埗logger鐨刟ppenders銆?/P>

root logger鎷ユ湁鐩爣涓?TT>system.out鐨?TT>consoleAppender錛屾晠榛樿鎯呭喌涓嬶紝鎵鏈夌殑logger閮藉皢緇ф壙璇ppender銆?/P>

      

      ------ Log4j APIs : class Logger ------
      
      
      
      // 涓簂ogger瀵硅薄澧炲姞鎴栬呯Щ闄や竴涓狝ppender瀵硅薄 :.
      
      public void appAppender(Appender app);
      public void removeAppender(Appender app);
      
      // 鑾峰緱鍜岃緗產dditive鏍囧織錛氭槸鍚︾戶鎵跨埗logger鐨刟ppenders :.
      // 娉ㄦ剰錛?/B>鍦ㄨ緗產dditive鏍囧織涓篺alse鏃訛紝蹇呴』淇濊瘉宸茬粡涓鴻logger璁劇疆浜嗘柊鐨刟ppender錛?:.
      // 鍚﹀垯log4j灝嗘姤閿欙細log4j:WARN No appenders could be found for logger (x.y.z). :.
      
      public boolean getAdditivity();
      public void setAdditivity(boolean additive);
    

ConsoleAppender

鍙互浣跨敤ConsoleAppender瀵硅薄鎶婃棩蹇楄緭鍑哄埌鎺у埗鍙般傛瘡涓?TT>ConsoleAppender閮芥湁涓涓?TT>target錛岃〃紺哄畠鐨勮緭鍑虹洰鐨勫湴銆傚畠鍙互鏄?TT>System.out錛屾爣鍑嗚緭鍑鴻澶囷紙緙撳啿鏄劇ず灞忥級錛涙垨鑰呮槸System.err錛屾爣鍑嗛敊璇澶囷紙涓嶇紦鍐叉樉紺哄睆錛夈?TT>ConsoleAppender鐨勪嬌鐢ㄦ柟娉曞弬鑰冨涓婣PI :.

        

        ------ Log4j APIs : class ConsoleAppender extends WriterAppender ------
        
      
        
        // 鏋勯犳柟娉曪紝浣跨敤涓涓狶ayout瀵硅薄鏋勯犱竴涓狢onsoleAppender瀵硅薄 :.
        // 榛樿鎯呭喌涓嬶紝ConsoleAppender鐨則arget鏄疭ystem.out :.
        
        public ConsoleAppender(Layout layout);
        
        // 鏋勯犳柟娉曪紝浣跨敤涓涓狶ayout瀵硅薄鍜屼竴涓猼arget瀛楃涓叉瀯閫燙onsoleAppender瀵硅薄 :.
        // target鐨勫彲鑳藉彇鍊間負ConsoleAppender.SYSTEM_OUT鍜孋onsoleAppender.SYSTEM_ERR :.
        
        public ConsoleAppender(Layout layout, String target);
      


FileAppender

鍙互浣跨敤FileAppender瀵硅薄鎶婃棩蹇楄緭鍑哄埌涓涓寚瀹氱殑鏃ュ織鏂囦歡涓幓銆備嬌鐢ㄦ柟娉曞彲浠ュ弬鑰冨涓嬬殑API :.

        

        ------ Log4j APIs : class FileAppender extends WriterAppender ------
        
      
        
        // 鏋勯犳柟娉曪紝浣跨敤涓涓狶ayout瀵硅薄鍜屾棩蹇楁枃浠跺悕鏋勯犱竴涓狥ileAppender瀵硅薄 :.
        
        public FileAppender(Layout layout, String filename)
            throws IOException;
        public FileAppender(Layout layout, String filename, boolean append)
            throws IOException;
      


RollingFileAppender

鍙互浣跨敤FileAppender鐨勫瓙綾?TT>RollingFileAppender瀵硅薄錛屾妸鏃ュ織杈撳嚭鍒頒竴涓寚瀹氱殑鏃ュ織鏂囦歡涓備笉鍚岀殑鏄鏃ュ織鏂囦歡鐨勫ぇ灝忓彈鍒伴檺鍒訛紝褰撴棩蹇楀唴瀹硅秴鍑烘渶澶х殑灝哄鏃訛紝璇ユ枃浠跺皢鍚戜笂婊氬姩錛堟渶鑰佺殑鏃ュ織琚摝闄わ級銆傝繕鍙互鍦ㄨ綾誨璞′腑鎸囧畾涓烘棩蹇楁枃浠跺仛澶氬皯涓浠姐傚叿浣撲嬌鐢ㄦ柟娉曞弬鑰冨涓婣PI :.

        

        ------ Log4j APIs : class RollingFileAppender extends FileAppender ------
        
      
        
        // 鏋勯犳柟娉曪紝浣跨敤涓涓狶ayout瀵硅薄鍜屾棩蹇楁枃浠跺悕鏋勯犱竴涓猂ollingFileAppender瀵硅薄 :.
        
        public RollingFileAppender(Layout layout, String filename)
            throws IOException;
        public RollingFileAppender(Layout layout, String filename, boolean append)
            throws IOException;
        
        // 鑾峰緱鍜岃緗棩蹇楀浠芥枃浠剁殑涓暟 :.
        
        public int getMaxBackupIndex();
        public void setMaxBackupIndex(int index);
        
        // 鑾峰緱鍜岃緗粴鍔ㄦ棩蹇楁枃浠剁殑鏈澶у昂瀵?:.
        
        public long getMaximumFileSize();
        public void setMaximumFileSize(long size);
      



Layout錛氭棩蹇楁牸寮忓寲鍣?/B>

姣忎釜appender閮藉拰涓涓猯ayout鐩歌仈緋伙紱layout鐨勪換鍔℃槸鏍煎紡鍖栫敤鎴風殑logging request錛宎ppender鐨勪換鍔℃槸鎶妉ayout鏍煎紡鍖栧ソ鐨勮緭鍑哄唴瀹歸佸線鎸囧畾鐨勭洰鐨勫湴銆?/P>
PatternLayout

PatternLayout鏄疞ayout鐨勪竴涓瓙綾伙紝鐢ㄦ潵浣跨敤綾諱技C璇█鐨?TT>printf鍑芥暟涓嬌鐢ㄧ殑鏍煎紡鎺у埗瀛楃涓叉潵鎺у埗鏃ュ織鐨勮緭鍑烘牸寮忋備嬌鐢ㄦ柟娉曞弬鑰冨涓婣PI :.

        

        ------ Log4j APIs : class PatternLayout extends Layout ------
        
      
        
        // 鏃犲弬鏁版瀯閫犳柟娉曪紝浣跨敤DEFAULT_CONVERSION_PATTERN鏋勯犱竴涓狿atternLayout :.
        // 娉ㄦ剰錛?/B>DEFAULT_CONVERSION_PATTERN涓?%m%n"錛屽彧鎵撳嵃娑堟伅淇℃伅 :.
        
        public PatternLayout();
        
        // 鏋勯犳柟娉曪紝浣跨敤鑷畾涔夌殑pattern鏋勯犱竴涓狿atternLayout :.
        
        public PatternLayout(String pattern);
        
        // 鑾峰緱鍜岃緗甈atternLayout瀵硅薄鐨勬棩蹇梡attern :.
        
        public String getConversionPattern();
        public void setConversionPattern(String pattern);
      


patterns in PatternLayout

鏈畬寰呯畫



Configuration錛氶厤緗?/B>

瀵筶og4j鐜鐨勯厤緗氨鏄root logger鐨勯厤緗紝鍖呮嫭鎶妑oot logger璁劇疆涓哄摢涓駭鍒?level)錛涗負瀹冨鍔犲摢浜沘ppender錛岀瓑絳夈傝繖浜涘彲浠ラ氳繃璁劇疆緋葷粺灞炴х殑鏂規硶鏉ラ殣寮忓湴瀹屾垚錛屼篃鍙互鍦ㄧ▼搴忛噷璋冪敤XXXConfigurator.configure()鏂規硶鏉ユ樉寮忓湴瀹屾垚銆?/P>
榛樿鐨刲og4j鍒濆鍖栬繃紼?/B>

Logger綾葷殑闈欐佸垵濮嬪寲鍧?static initialization block)涓log4j鐨勭幆澧冨仛榛樿鐨勫垵濮嬪寲銆?B>娉ㄦ剰錛?/B>濡傛灉紼嬪簭鍛樺凡緇忛氳繃璁劇疆緋葷粺灞炴х殑鏂規硶鏉ラ厤緗簡log4j鐜錛屽垯涓嶉渶瑕佸啀鏄懼紡鍦拌皟鐢?TT>XXXConfigurator.configure()鏂規硶鏉ラ厤緗甽og4j鐜浜嗐?/P>

Logger鐨勯潤鎬佸垵濮嬪寲鍧楀湪瀹屾垚鍒濆鍖栬繃紼嬫椂灝嗘鏌ヤ竴緋誨垪log4j瀹氫箟鐨勭郴緇熷睘鎬с傚畠鎵鍋氱殑浜嬫儏濡備笅錛?

  1. 媯鏌ョ郴緇熷睘鎬?TT>log4j.defaultInitOverride錛屽鏋滆灞炴ц璁劇疆涓篺alse錛屽垯鎵ц鍒濆鍖栵紱鍚﹀垯錛堝彧瑕佷笉鏄痜alse錛屾棤璁烘槸浠涔堝鹼紝鐢氳嚦娌℃湁鍊鹼紝閮芥槸鍚﹀垯錛夛紝璺寵繃鍒濆鍖栥?
  2. 鎶婄郴緇熷睘鎬?TT>log4j.configuration鐨勫艱祴緇欏彉閲弐esource銆傚鏋滆緋葷粺鍙橀噺娌℃湁琚畾涔夛紝鍒欐妸resource璧嬪間負"log4j.properties"銆?B>娉ㄦ剰錛?/B>鍦╝pache鐨刲og4j鏂囨。涓緩璁嬌鐢ㄥ畾涔?TT>log4j.configuration緋葷粺灞炴х殑鏂規硶鏉ヨ緗粯璁ょ殑鍒濆鍖栨枃浠舵槸涓涓ソ鏂規硶銆?
  3. 璇曞浘鎶妑esource鍙橀噺杞寲鎴愪負涓涓?TT>URL瀵硅薄url銆傚鏋滀竴鑸殑杞寲鏂規硶琛屼笉閫氾紝灝辮皟鐢?TT>org.apache.log4j.helpers.Loader.getResource(resource, Logger.class)鏂規硶鏉ュ畬鎴愯漿鍖栥?
  4. 濡傛灉url浠?.html"緇撳熬錛屽垯璋冪敤鏂規硶DOMConfigurator.configure(url)鏉ュ畬鎴愬垵濮嬪寲錛涘惁鍒欙紝鍒欒皟鐢ㄦ柟娉?TT>PropertyConfigurator.configure(url)鏉ュ畬鎴愬垵濮嬪寲銆傚鏋?TT>url鎸囧畾鐨勮祫婧愪笉鑳借鑾峰緱錛屽垯璺沖嚭鍒濆鍖栬繃紼嬨?


BasicConfigurator.configure()

BasicConfigurator.configure()鏂規硶浣跨敤鏈綆鐨勬柟娉曢厤緗甽og4j鐜銆?B>娉細鎵璋撻厤緗甽og4j鐜錛屽氨鏄寚閰嶇疆root logger錛屽洜涓烘墍鏈夊叾瀹冪殑logger閮芥槸root logger鐨勫悗浠o紝鎵浠ュ畠浠紙榛樿鎯呭喌涓嬶級閮藉皢緇ф壙root logger鐨勬ц川銆?/P>

BasicConfigurator.configure()瀹屾垚鐨勪換鍔℃槸錛?

  1. 鐢ㄩ粯璁attern鍒涘緩PatternLayout瀵硅薄p錛?BR>PatternLayout p = new PatternLayout("%-4r[%t]%-5p%c%x - %m%n");
  2. 鐢╬鍒涘緩ConsoleAppender瀵硅薄a錛岀洰鏍囨槸system.out錛屾爣鍑嗚緭鍑鴻澶囷細
    ConsoleAppender a = new ConsoleAppender(p,ConsoleAppender.SYSTEM_OUT);
  3. 涓簉oot logger澧炲姞涓涓狢onsoleAppender p錛?BR>rootLogger.addAppender(p);
  4. 鎶妑oot logger鐨刲og level璁劇疆涓篋EBUG綰у埆錛?BR>rootLogger.setLevel(Level.DEBUG);


xml鏍煎紡鐨刲og4j閰嶇疆鏂囦歡姒傝堪

xml鏍煎紡鐨刲og4j閰嶇疆鏂囦歡闇瑕佷嬌鐢?TT>org.apache.log4j.html.DOMConfigurator.configure()鏂規硶鏉ヨ鍏ャ傚xml鏂囦歡鐨勮娉曞畾涔夊彲浠ュ湪log4j鐨勫彂甯冨寘涓壘鍒幫細org/apache/log4j/xml/log4j.dtd銆?/P>
log4j鐨剎ml閰嶇疆鏂囦歡鐨勬爲鐘剁粨鏋?/B>

log4j鐨剎ml閰嶇疆鏂囦歡鐨勬爲鐘剁粨鏋勫涓嬫墍紺猴紝娉ㄦ剰涓嬪浘鍙樉紺轟簡甯哥敤鐨勯儴鍒嗐?:.

          xml declaration and dtd
            |
          log4j:configuration
            |
            +-- appender (name, class)
            |     |
            |     +-- param (name, value)
            |     +-- layout (class)
            |           |
            |           +-- param (name, value)
            +-- logger (name, additivity)
            |     |
            |     +-- level (class, value)
            |     |     |
            |     |     +-- param (name, value)
            |     +-- appender-ref (ref)
            +-- root
                  |
                  +-- param (name, class)
                  +-- level
                  |     |
                  |     +-- param (name, value)
                  +-- appender-ref (ref)  
        


xml declaration and dtd

xml閰嶇疆鏂囦歡鐨勫ご閮ㄥ寘鎷袱涓儴鍒嗭細xml澹版槑鍜宒td澹版槑銆傚ご閮ㄧ殑鏍煎紡濡備笅錛?:.

          <?xml version="1.0" encoding="UTF-8" ?>
          <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
        


log4j:configuration (root element)

  1. xmlns:log4j [#FIXED attribute] : 瀹氫箟log4j鐨勫悕瀛楃┖闂達紝鍙栧畾鍊?TT>"http://jakarta.apache.org/log4j/"
  2. appender [* child] : 涓涓猘ppender瀛愬厓绱犲畾涔変竴涓棩蹇楄緭鍑虹洰鐨勫湴
  3. logger [* child] : 涓涓猯ogger瀛愬厓绱犲畾涔変竴涓棩蹇楀啓鍑哄櫒
  4. root [? child] : root瀛愬厓绱犲畾涔変簡root logger


appender

appender鍏冪礌瀹氫箟涓涓棩蹇楄緭鍑虹洰鐨勫湴銆?/P>

  1. name [#REQUIRED attribute] : 瀹氫箟appender鐨勫悕瀛楋紝浠ヤ究琚悗鏂囧紩鐢?
  2. class [#REQUIRED attribute] : 瀹氫箟appender瀵硅薄鎵灞炵殑綾葷殑鍏ㄥ悕
  3. param [* child] : 鍒涘緩appender瀵硅薄鏃朵紶閫掔粰綾繪瀯閫犳柟娉曠殑鍙傛暟
  4. layout [? child] : 璇ppender浣跨敤鐨刲ayout瀵硅薄


layout

layout鍏冪礌瀹氫箟涓庢煇涓涓猘ppender鐩歌仈緋葷殑鏃ュ織鏍煎紡鍖栧櫒銆?/P>

  1. class [#REQUIRED attribute] : 瀹氫箟layout瀵硅薄鎵灞炵殑綾葷殑鍏ㄥ悕
  2. param [* child] : 鍒涘緩layout瀵硅薄鏃朵紶閫掔粰綾繪瀯閫犳柟娉曠殑鍙傛暟


logger

logger鍏冪礌瀹氫箟涓涓棩蹇楄緭鍑哄櫒銆?/P>

  1. name [#REQUIRED attribute] : 瀹氫箟logger鐨勫悕瀛楋紝浠ヤ究琚悗鏂囧紩鐢?
  2. additivity [#ENUM attribute] : 鍙栧間負"true"錛堥粯璁わ級鎴栬?false"錛屾槸鍚︾戶鎵跨埗logger鐨勫睘鎬?
  3. level [? child] : 瀹氫箟璇ogger鐨勬棩蹇楃駭鍒?
  4. appender-ref [* child] : 瀹氫箟璇ogger鐨勮緭鍑虹洰鐨勫湴


root

root鍏冪礌瀹氫箟鏍規棩蹇楄緭鍑哄櫒root logger銆?/P>

  1. param [* child] : 鍒涘緩root logger瀵硅薄鏃朵紶閫掔粰綾繪瀯閫犳柟娉曠殑鍙傛暟
  2. level [? child] : 瀹氫箟root logger鐨勬棩蹇楃駭鍒?
  3. appender-ref [* child] : 瀹氫箟root logger鐨勮緭鍑虹洰鐨勫湴


level

level鍏冪礌瀹氫箟logger瀵硅薄鐨勬棩蹇楃駭鍒?/P>

  1. class [#IMPLIED attribute] : 瀹氫箟level瀵硅薄鎵灞炵殑綾伙紝榛樿鎯呭喌涓嬫槸"org.apache.log4j.Level綾?
  2. value [#REQUIRED attribute] : 涓簂evel瀵硅薄璧嬪箋傚彲鑳界殑鍙栧間粠灝忓埌澶т緷嬈′負"all"銆?debug"銆?info"銆?warn"銆?error"銆?fatal"鍜?off"銆傚綋鍊間負"off"鏃惰〃紺烘病鏈変換浣曟棩蹇椾俊鎭杈撳嚭
  3. param [* child] : 鍒涘緩level瀵硅薄鏃朵紶閫掔粰綾繪瀯閫犳柟娉曠殑鍙傛暟


appender-ref

appender-ref鍏冪礌寮曠敤涓涓猘ppender鍏冪礌鐨勫悕瀛楋紝涓簂ogger瀵硅薄澧炲姞涓涓猘ppender銆?/P>

  1. ref [#REQUIRED attribute] : 涓涓猘ppender鍏冪礌鐨勫悕瀛楃殑寮曠敤
  2. appender-ref鍏冪礌娌℃湁瀛愬厓绱?


param

param鍏冪礌鍦ㄥ垱寤哄璞℃椂涓虹被鐨勬瀯閫犳柟娉曟彁渚涘弬鏁般傚畠鍙互鎴愪負appender銆?TT>layout銆?TT>filter銆?TT>errorHandler銆?TT>level銆?TT>categoryFactory鍜?TT>root絳夊厓绱犵殑瀛愬厓绱犮?/P>

  1. name and value [#REQUIRED attributes] : 鎻愪緵鍙傛暟鐨勪竴緇勫悕鍊煎
  2. param鍏冪礌娌℃湁瀛愬厓绱?



鍦▁ml鏂囦歡涓厤緗產ppender鍜宭ayout

鍒涘緩涓嶅悓鐨凙ppender瀵硅薄鎴栬呬笉鍚岀殑Layout瀵硅薄瑕佽皟鐢ㄤ笉鍚岀殑鏋勯犳柟娉曘傚彲浠ヤ嬌鐢╬aram瀛愬厓绱犳潵璁懼畾涓嶅悓鐨勫弬鏁板箋?/P>
鍒涘緩ConsoleAppender瀵硅薄

ConsoleAppender鐨勬瀯閫犳柟娉曚笉鎺ュ彈鍏跺畠鐨勫弬鏁般?:.

          ... ... ... ...
          <appender name="console.log" class="org.apache.log4j.ConsoleAppender">
            <layout ... >
              ... ...
            </layout>
          </appender>
          ... ... ... ...
        


鍒涘緩FileAppender瀵硅薄

鍙互涓篎ileAppender綾葷殑鏋勯犳柟娉曚紶閫掍袱涓弬鏁幫細File琛ㄧず鏃ュ織鏂囦歡鍚嶏紱Append琛ㄧず濡傛枃浠跺凡瀛樺湪錛屾槸鍚︽妸鏃ュ織榪藉姞鍒版枃浠跺熬閮紝鍙兘鍙栧間負"true"鍜?false"錛堥粯璁わ級銆?:.

          ... ... ... ...
          <appender name="file.log" class="org.apache.log4j.FileAppender">
            <param name="File" value="/tmp/log.txt" />
            <param name="Append" value="false" />
            <layout ... >
              ... ...
            </layout>
          </appender>
          ... ... ... ...
        


鍒涘緩RollingFileAppender瀵硅薄

闄や簡File鍜?TT>Append浠ュ錛岃繕鍙互涓篟ollingFileAppender綾葷殑鏋勯犳柟娉曚紶閫掍袱涓弬鏁幫細MaxBackupIndex澶囦喚鏃ュ織鏂囦歡鐨勪釜鏁幫紙榛樿鏄?涓級錛?TT>MaxFileSize琛ㄧず鏃ュ織鏂囦歡鍏佽鐨勬渶澶у瓧鑺傛暟錛堥粯璁ゆ槸10M錛夈?:.

          ... ... ... ...
          <appender name="rollingFile.log" class="org.apache.log4j.RollingFileAppender">
            <param name="File" value="/tmp/rollingLog.txt" />
            <param name="Append" value="false" />
            <param name="MaxBackupIndex" value="2" />
            <param name="MaxFileSize" value="1024" />
            <layout ... >
              ... ...
            </layout>
          </appender>
          ... ... ... ...
        


鍒涘緩PatternLayout瀵硅薄

鍙互涓篜atternLayout綾葷殑鏋勯犳柟娉曚紶閫掑弬鏁癈onversionPattern銆?:.

          ... ... ... ...
          <layout class="org.apache.log4j.PatternLayout>
            <param name="Conversion" value="%d [%t] %p - %m%n" />
          </layout>
          ... ... ... ...
        



鎴戣嚜宸辯殑涓涓嬌鐢▁ml鏂囦歡閰嶇疆log4j鐜鐨勫緢綆鍗曠殑渚嬪瓙

涓篧SOTA欏圭洰寮鍙慾ava web start鐨勮儢瀹㈡埛绔椂錛屼嬌鐢ㄤ簡濡備笅鐨剎ml鏂囦歡閰嶇疆log4j鐜錛堟枃浠跺悕涓簑sota-rc.log4j.html錛夛細:.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
  
  <!-- ================================================================= -->
  <!--                     a rolling file appender                       -->
  <!-- ================================================================= -->
  
  <appender name="wsota-rc.file.log" class="org.apache.log4j.RollingFileAppender">
    <param name="File" value="/tmp/wsota-rc.log" />
    <param name="Append" value="false" />
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern" value="%d [%t] %p - %m%n" />
    </layout>
  </appender>
  
  <!-- ================================================================= -->
  <!--                       a console appender                          -->
  <!--     debug can be turned off by setting level of root to "off"     -->
  <!-- ================================================================= -->
  
  <appender name="wsota-rc.console.log" class="org.apache.log4j.ConsoleAppender">
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern" value="%d [%t] %p - %m%n" />
    </layout>
  </appender>
  
  <!--  use this to turn on debug to a rolling file. -->
  
  <root>
    <level value="debug" />
    <appender-ref ref="wsota-rc.file.log" />
  </root>
  
  <!--  use this to turn on debug to console. -->
  <!--
  <root>
    <level value="off" />
    <appender-ref ref="wsota-rc.console.log" />
  </root>
  -->
  
  
  <!--  use this to turn off debug. -->
  <!--
  <root>
    <level value="off" />
    <appender-ref ref="wsota-rc.console.log" />
  </root>
  -->
  
</log4j:configuration>
      

鍦ㄨ儢瀹㈡埛紼嬪簭涓嬌鐢ㄤ簡濡備笅浠g爜鏉ヤ嬌鐢ㄥ閮▁ml鏂囦歡閰嶇疆log4j鐜錛屾敞鎰忚浠g爜孌典綅浜庣▼搴忕殑main class鐨勯潤鎬佸垵濮嬪寲鍧椾腑錛屽惈鏈変互涓嬩唬鐮佺殑綾誨拰xml閰嶇疆鏂囦歡鍦ㄥ悓涓涓洰褰曚笅錛?.

  import org.apache.log4j.html.DOMConfigurator;
  
  public class SapFrame extends JFrame {
      static {
          DOMConfigurator.configure(SapFrame.class.getResource("wsota-rc.log4j.html"));
      }
      ... ... ... ...
  }
      



Log4j鐨勭紪鐮佷範鎯?/B>

  1. 璁╂瘡涓被閮芥嫢鏈変竴涓猵rivate static鐨凩ogger瀵硅薄錛岀敤鏉ヨ緭鍑鴻綾諱腑鐨勫叏閮ㄦ棩蹇椾俊鎭?
  2. 浣跨敤xml鏂囦歡鏉ュ畬鎴愬log4j鐜鐨勯厤緗傚湪欏圭洰鐨刴ain class涓殑闈欐佸垵濮嬪寲鍧楅噷鏀緇og4j鐜鐨勯厤緗唬鐮併傛敞鎰忥細鍦ㄤ竴涓」鐩腑錛宭og4j鐜鍙渶瑕佽閰嶇疆涓嬈★紝鑰屼笉鏄湪姣忎釜浣跨敤浜唋ogger鐨勭被閲岄兘闇瑕佽皟鐢ㄤ竴嬈?
  3. 鐢?TT>MyClass.class浣滀負鍙傛暟鍒涘緩璇ョ被鐨勯潤鎬丩ogger瀵硅薄
  4. 琛ュ厖涓?..


R.Zeus 2005-08-11 16:13 鍙戣〃璇勮
]]>
主站蜘蛛池模板: 亚洲久本草在线中文字幕| 日韩一区二区免费视频| 国产成人无码综合亚洲日韩| jizz免费一区二区三区| 亚洲Aⅴ无码一区二区二三区软件| 亚洲av永久无码精品网址| 日韩伦理片电影在线免费观看| 亚洲精品天堂成人片AV在线播放| 成年午夜视频免费观看视频| 亚洲一级黄色大片| 免费黄网在线观看| 美女被暴羞羞免费视频| 亚洲日韩中文字幕日韩在线| gogo免费在线观看| 亚洲AV午夜成人片| 五月婷婷在线免费观看| 久久久久久亚洲精品影院| 免费永久看黄在线观看app| av成人免费电影| 亚洲人成电影亚洲人成9999网 | 亚洲特级aaaaaa毛片| 中文字幕无码免费久久99| 亚洲乱码中文字幕在线| 亚洲精品NV久久久久久久久久| 2022国内精品免费福利视频 | 在线综合亚洲中文精品| 午夜一级毛片免费视频| 黄色免费在线网址| 亚洲国产精品久久久久婷婷软件 | 夜色阁亚洲一区二区三区| 你是我的城池营垒免费观看完整版| 欧洲亚洲国产清在高| 1000部拍拍拍18勿入免费凤凰福利| 日韩亚洲不卡在线视频中文字幕在线观看| 日韩在线视频免费看| 最好免费观看高清在线| 亚洲无mate20pro麻豆| 久久久久亚洲av成人无码电影| 99re6在线视频精品免费下载| 亚洲国产精品美女久久久久| 久久精品国产亚洲网站|