FAQ About Log4j

zhaozj2021-02-16  77

Translator statement:

1. This is a FAQ translation according to the latest Log4j (Jakarta-Log4j-1.2.8).

2. Translators try their best to keep the original format so that the reader find the original text

3. The pink title represents the original title, and there is no corresponding content

4. For some keywords, it is easy to read

5. The original words in the original text, the translators are referring to the Kingsoft Words, and the combination of the found is maintained in the original text.

6. For comparison, the translator tries to use the methods of translation while maintaining the original text.

Wang Jian

2004.6.16

· What is log4j?

· Is log4j Is a reliable log system?

· What is the prerequisite for log4j?

· Is log4j Is there a Javadoc documentation? (Is there javadoc documentation for log4j?)

· Is there any other log package? (What other logging packages Are there?)

· Is there an instance code using Log4j?

· Log4j has those features?

· Log4j is a thread safe?

· What looks like when log output looks like?

· What is a recorder?

· How can I change the behavior of the log runtime?

· What is the fastest way for logging (or not logging)?

· In addition to outputting the string array as one of the parameters, is there any other role in debugging? (What is the use of the debug method evECTING A STRING ARRAY As One of Its Parameters?)

· Why introduce the logger class, and how can I transfer from a string based on the previous implementation (IMPLEMENTITION)? (Why Was the logger class introduces and how do i migrate from the previous string based implementation?)

· Is there any suggestions in the name of the recorder?

· How can I get a high quality (Fully-Qualified) class name with static mode

· Can log outputs can be customized?

· What is the configure option for ForbaraPpender?

· Do you have multiple client requests to correspond to different log files?

· The recorder instance seems to be only created, why not do a method to remove the recorder instance?

· Can I output the log output to a non-moving output source (Appender) according to different levels?

· How can I get multiple processing processes for the same file?

· If I have a number of processing processes across multiple hosts (which may span multiple time zones), what happens to the same file, what happens?

· Why can't I find my attribute file in J2EE or WAR applications?

· If the attribute file changes, is there a way to let log4j load?

· When I use NTEVENTLOGAPPENDER class, what kind of response does Windows NT Event View is lost to the behavior of my event information? · When I use the NTEVENTLOGAPPENDER class, why can't I map my recorder name to the recorder displayed in the NT Event LOG

· Why should I give my Log4J extended donation (Donate)?

· What should I keep in mind when donating a code?

· Where can I find the latest log4j distribution?

What is log4j?

Log4j is a tool for helping programmers output log information to output destination

In a problematic application, use the log to locate an error make it very helpful. In operation, the non-modifying binary application uses LOG4J to make the log work. The log4j package is designed to save the log information in the form of a slip code, and does not cause high performance overhead. It follows the principle of recording speed (even without record behavior)

At the same time, log output may be so huge to not record. One of the unique features in Log4j is the concept of a grading recorder (Logger). Use the recorder to make the selected granular control output log may be possible.

Log4j is designed by two special eyes: speed and flexibility. It is difficult to find a balance between these two essential requirements. However, I believe that log4j reaches the best balance point

Is log4j Is a reliable log system?

Log4j is unreliable, it is a Best-Effort and Fail-STOP log system

Referring to Fail-Stop, we mean that log4j runs in a potential errors that cause the system crash, which will not throw out an unexpected exception. If some reasons, log4j throws an unusually captured exception, please send email to log4j-user@jakarta.apache.org mailing list

Also, when the output stream specified by the log4j is in a state in which the output stream is not opened, it is not written or full, it does not turn to the output to System.out and System.err. This avoids the failure of the logging, the user's terminal is full of error messages, thus causing another program that is running. However, log4j will output a single message to System.err indicates that the log cannot run normally.

What is the prerequisites of log4j?

· Log4j is compatible with JDK 1.1.x.

· Domconfigurator is based on the Dom Level 1 API. Domconfigurator.configure (Element) method will work with any XML parser that can be parsed into a DOM book (DOM Tree), and its variable requires one and JAXP-compatible XML analysis. For example, Xerces (Translation: This is also an open source project below Apache) or Sun's parser. When compiling Domconfigurator, ask the JAXP parser to be under the path of the ClassPath environment variable.

Org.apache.log4j.net.smtpappender class depends on the JavaMail API. It has been tested with the 1.2 version of the JavaMail API. JavaMail API requires JavaBeans Activation Framework Package

· Org.apache.log4j.net.jmsappender class requires the existence of JMS API and JNDI

· Log4j's Test Code Depending on the JUnit test framework with instance code using LOG4J?

See Examples / Path.

Log4j has those features?

· Log4j has been optimized in terms of speed

Log4j is based on recorder hierarchy (log4j is based on a named logger hierarchy.) Log4j is Fail-Stop but there is no dependency (log4j is fail-stop but not reliable.) Log4j is the LOG4J of thread security is not predetermined When a set of tool restrictions, log behavior can be placed in a profile that can be the attribute file or XML format, the log4j is designed to process the Java exception (Exceptions) log4j can oriented to one Document, console, output stream (Java.io. Writer), a set of Reemote Server Using TCP, a remote UNIX Syslog daemon (Unix Syslog daemon ), A remote JMS listening thread, NT event logger, or even email. Log4j uses five levels, named Debug, Info, Warn, and Error and Fatal log output format, by expansion of the Layout class, you can work with the target and write strategy of the log output and the Writing Strategy. The interface is changed. Log4j supports each recorder to have multiple output sources (Multiple Output Appenders Per Log, log4j supports internationalization.

Is log4j a thread safe?

Yes, log4j is thread safe

What is the log output looks like?

You can customize the log output in a variety of ways. Moreover, you can completely override the output format by implementing a layout (layout).

This is a conversion format using the PatternLayout layout of output examples, this layout "% r [% T]% -5p% c {2}% x -% m% N" conversion format

176 [main] info example.sort - Populating an Array of 2 Elements in Reverse ORDER.

225 [main] info example.sortalgo - Entered The Sort Method.

262 [main] debug sortalgo.outer i = 1 - Outer loop.

276 [main] debug sortalgo.swap i = 1 j = 0 - swapping IntArray [0] = 1 and IntArray [1] = 0

290 [main] debug sortalgo.outer i = 0 - Outer loop.

304 [main] info sortalgo.dump - dump of interger array:

317 [main] info sortalgo.dump - Element [0] = 0

331 [main] info sortalgo.dump - Element [1] = 1

343 [main] info example. .Sort - The next log statne.

346 [main] Error Sortalgo.dump - Tried to Dump An Uninitialized Array.

At Org.log4j.examples.Sortalgo.dump (Sortalgo.java: 58) at Org.log4j.examples.Sort.main (Sort.java: 64)

467 [main] info example. .Sort - exitation main method.

The first field is the number of milliseconds that run from the program to the output of the line log. The second field is the thread of the output log. The third is the level of log description. The fourth field is the composition of two parts of the logger's logger (Logger Making The Log Request). The fifth field (just in front of '-') is a nest Diagnostic Context (NDC)). Note Nested Diagnostic Environment (NDC) can be empty, just like the two descriptions starting. The description information is followed behind '-'

What is a logger?

The recorder is in the heart of log4j. The recorder defines a hierarchy and controls whether to print the program information for the programmer.

The recorder is assigned a level. Printing of a log description depends on his level and recorder

Read the log4j manual (log4j manual) Get more information

How can I change the behavior of log rules?

Log Behavior can be set in a configuration file, can resolve this file at runtime. Using a configuration file, programmers can define a logger and set its level

The PropertyConfigurator class defines a special profile format. You can also refer to the configuration file of the Examples / Sort.java instance.

The configuration file can be an XML file. Refer to log4j.dtd and org.log4j.xml.Domconfigurator class Get more information

Refer to the Layout and Output Source (Appender) components for implementing the Special Configuration Options.

Including the configuration file, the user can invalidate all the information attached to a set of levels, refer to the next.

What is the fastest way to log (or not recorded)?

For example, the recorder L, the content is as follows,

L. Debug ("Entry Number:" i "IS" String.Valueof (entry [i]));

It causes the construction of the information parameter overhead that transforming integer I values ​​and array Entry [i] is a string; the media connected to the string. These things happen regardless of whether logging information.

If you are worried, you can do this.

IF (L.IsdeBuGenabled ()) {

L. Debug ("Entry Number:" i "IS" String.Valueof (entry [i]));

}

Using this method, if you do not output a log when debugging, you will not lead to overhead of parameter construction. On the other hand, if the logger is required, regardless of whether the logger is debugging, it will lead to the overhead of the detection, which includes two places: one is debugenabled, one is debug (translation: is L. ISDebugeNabled () and L.debug ("") overhead of these two methods). This is irrelevant to daily overhead, because the cost of detecting a recorder does not record 1% of the log information.

Is there any suggestions in the name of the recorder?

There is

You can name the logger (Loggers) by location (LOCALITY). This proves that a single class name is to instantiate a logger equivalent to using a complete recorder name, which is a way to define a recorder directly.

This approach has the following benefits:

It can easily implement it easy to explain the new developers to explain it. It automatically maps your application module design. It can be very casualized to optimize the information given by the automatic print logger. However, this Not the only way to name the log name. An ordinary alternative is to name the logger through a functional area. For example, a database (Database) recorder, "Remote Method Call" recorder, "Security" recorder, "XML" recorder.

You can select a naming recorder in both functional and local subcategories (by "Database.com.foo.some.package.someclass" or "Database.com.foo.some. Other.package.someharClass.

When you select the name of your own logger, you are completely free. The log4j package only allows you to manage your name in accordance with a level. Anyway, defining this level is your responsibility

Note that local naming recorders are often naming (Note by Naming Loggers By Locality One Tends to Name Things By FunctionAlity), because in most cases, localization and functionality are more closely related.

How can I get a high quality (in a static block) to get a high quality (full-qualified) class name?

You may easily press Static mode (IN A Static Block, I didn't find the best translation, and then modify it later J) to get the high quality name of class X with the x.class.getname () method. Note that X is class name instead of an instance. X.class does not create a new instance of a new class X

This is a suggested use case template:

Package a.b.c;

Public class foo {

Static logger logger = logger.getlogger (foo.class);

... Other Code

}

Can log outputs can be customized?

Yes it is. From version 0.7.0, you can extend the Layout class to create your own log format. The Appenders also reaches parameters through the layout of your own choice (Layout).

Can multiple client requests to correspond to different log files?

Many developers are facing such problems, facing different client requests, and resizing in the same class (Class) category in addition to the log output. They put forward a unique mechanism for logs to different documents. In most cases, this is not a good way.

It is very simple to use the nested diagnostic environment (NDC). In particular, when processed a client request, use ndc.push () will obtain special information of the client, such as host name, ID, or any other distinguishing information. Since then, the log output will automatically include a nested diagnostic environment, even if the log is output to the same file, you can also distinguish between different clients.

Refer to NDC and PatternLayout class get more information. NumberCruncher example shows how to use NDC from multiple client distinguished log output information, even if they share the same log file

For applications, such as virtual host web server (Virtual Hosting Web-Servers), NDC solutions are inventive. To the 0.9.0 version of LOG4J, log4j began supporting multi-level trees. Therefore, depending on the current environment, different objectives are possible in the same recorder.

What is the configuration option for ForbaraPpender?

Log4j is configured using JavaBean.

Therefore, any assignment method (Setter Method) in the FoobaraPpender class corresponds to a configuration option. For example, the RollingFileAppender class's SetMaxBackups method corresponds to the MaxBackupIndex option. The first letter of the option can be uppercase, that is, (i.e.) MaxBackupUpIndex and MaxBackupIndex are the same, but MaxBackupIndex and MaxBackupIndex are different. The layout option can also be defined by the assignment method (SETER METHODS). Most of the other log4j components are also doing so.

The recorder instance seems to be only created (the translation: This is correct, that is, why can the logger can only create, can not be deleted), why not do a way to remove the recorder instance?

Defining a "Removed" recorder is not a simple matter, which can be referenced by the user. Future version may include a delete method (Remove method) in the Logger class

According to different levels, can you orient the log output to a different output source (appender)?

Yes, you can do this. Extend an output source from the Appenderskeleton class (most log4j output sources are extended appenderskeleton classes), setting the portlet options for this output source to filter all log events, these log events of the entry option (Threshold Option) The value is lower than the level already set.

For example, set an inlet option value of an output source to debug, which allows information of INFO, WARN, ERROR, and FATAL levels to be recorded along with the Debug level information. This is acceptable because there is no information about INFO, WARN, ERROR and FATAL around, and Debug information is nothing.

This rule is usually the best package you want, because this is the opposite of the solution that has been in his heart (as opposed to her mind-projected solution, the translation: This sentence feels very embarrassing, Original text meaning seems to be incorrect).

Refer to Examples / Sort4.lcf to find an instance of the entry (Threshold) configuration

If you have to match the filtering event using the exact level, you can let any output source inherit the LevelMatchFilter class to filter the log event.

How can I get multiple processing processes for the same file?

You can make each SocketAppender class have a process log. Acceptor's socket Server (or SimpleSocket Server) can accept all events and send them to a standalone log file.

If I have many processing processes that span multiple hosts (which may span multiple time zones) use the above method to record the same file, what happens?

When the log event is created, this timestamp is also created, that is, when Debug, Info, Warn, Error or Fatal method is called. This is not subject to them (the translation: here the representative logging) arrives at the time of the remote server. Since the timestamp is saved in the event in the UTC format, they can be displayed in the same time zone, which is the time zone where the server creates this log file. Since the clocks of different machines cannot be synchronized, this will result in a conflict of time interval on different servers.

Although this is a purposeful behavior, it has recently changed the BUG discovery between version 1.0.4 and version 1.1B1. 1.0.4 Previous versions can produce their own timestamps in the converter. In this case, the timestamp in the log file will be displayed in order, this timestamp is generated by the local clock when they arrive at the log server host. Why can't I find my attribute file in J2EE or WAR applications?

The simple answer is: log4j class and attribute files are not included in the class loader (ClassLoader).

The detailed answer is (and explains what is going on): J2EE or Servlet container uses Java's class load system. Sun is changing the strategy of the class load in Java2 versions, and is designed in the Java2 loader as a hierarchial parent-child relationship. But a subclass loader (the translation: the subclass loader means the child - class loader, not a subclass-loader, this is two different concepts, pay attention to reading, this is the semantic misunderstanding of Chinese ) Need to find a class or resource is that it will entrust the request to the parent class loader (the translation note: see the sub-load)

LOG4J is loaded with the default class.forname () mechanism, and the resource is also processed in this way. Get more details on the documentation for java.lang.classloader

Therefore, if you have a problem, try your own loading and resources. If you didn't find it, log4j will not find it. :)

If the attribute file changes, is there a way to let log4j load?

YES. Both The Domconfigurator and The PropertyConfigurator Support Automatic Reloading Through The ConfigureAndWatch Apis. See The API Documentation for More Details.

Yes it is. The Domconfigurator class and the PropertyConfigurator class supports automatic loading over the ConfigureAndWatch interface. Get more details on the API documentation

When I use the NTEVENTLOGAPPENDER class, what kind of response does Windows NT Event View is lost to your event information?

The NT event viewer depends on the message resource dlls to correctly display the event message. NTEVENTLOGAPPENDER.DLL contains these message resources, but the DLL must copy to% systemroot% / system32 directory to work correctly

When I use the NTEVENTLOGAPPENDER class, why can't I map my recorder name to the recorder displayed in the NT Event LOG

Unfortunately, the recorder name is hard-coded in the message resource DLL (refer to the problem with the NTEVENTLOGAPPENDER class), so there is no easy way to cover those dynamic things ... in fact, I think this is impossible, Because you have to modify the DLL resources for each application. In any case, most local applications have no proprietary features of the recorder ...

Why should I give my log4j extended donation (Donate)?

The Apache software license does not require more requirements for your extension with respect to the GNU public license (GPL). By expanding, we get a complete new code, call the LOG4J class with these code. According to your needs, you have power to freely expand LOG4J. Note that you can't distribute the code you extend to a big population.

We value the client code that does not modify the log4j, so the new version of log4j is backward compatible with previous versions. We are not very concerned about the Log4j internal API (We are a lot less scrupulous with the internal log4j api, the translation: I didn't understand this sentence, I think this sentence is incorrect, his original interest may not pay attention to log4j inside The interface calls are called, but the open external interface is not the case). So, if you design your extension and version n just consistent, then log4j's version N 1 came out, you will likely need to modify the extension to adapt to the new version. Therefore, you will be forced to spend valuable time to compatibility with the new version. This is the "stupid tax" (stupid-Tax) that is often mentioned. By donating code, you will save unnecessary maintenance work. If your extension is useful, there will be someone to write an extension to provide the same or similar functions. Your development will be wasted. Unless your log4j extension is a critical business, there is no reason not to donate your code item (translation: The project here refers to Apache's log4j project)

When I donate the code, what should I close (Keep In Mind)?

1. Write a test case for your code to donate

Nothing is more exciting than when debugging (i.e. log) code. Write test cases take a certain amount of time, but is critical to a widespread library, such as log4j. Writing a test case that makes you follower, the test case requires a lot of effort and a long time.

2. Adhere to the existing indentation style, even if you hate this method.

Changing the existing indentation style will make your code difficult to understand. Let you have hard work, but others will be very easy. Log4J follows the Java language code style (Code Conventions for the Javatm Programming Language)

3. Try to let the code support the JDK1.1 API

The important advantage of log4j is that it is fully compatible with JDK 1.1.x.

4. Keep the code is simple, small and fast.

This is related to the application and has nothing to do with the log.

5. At the beginning of the relevant document, you identify whether you are donors.

6. Responsibility for your code

Creative software and marathon are very similar, it takes time and endurance

7. Is I mentioned to adhere to indentation?

8. Did I mention whether to write a test case?

Where can I find the latest log4j distribution?

Log4j's project is placed at http://jakarta.apache.org/log4j/

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

New Post(0)