开发者

Log4j properties and log4j.xml [closed]

开发者 https://www.devze.com 2023-02-13 11:28 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete开发者_JS百科, overly broad, or rhetorical andcannot be reasonably answered in its current form. F
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete开发者_JS百科, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 10 years ago.

how to get all actions and exceptions that are running in the project to a file with the help of log4j.


Since this is quite a generic question I suggest looking over the documentation for log4j and the tutorials.

http://logging.apache.org/log4j/1.2/manual.html


1 Use a Logger mechanism to log all your exceptions and error to a file stored in hard disk drive or a server location (example : C:\\MyProject\logs\Mylog.txt)

  1. The Logger mechanism which im talking about is usage of Log4j which will help in logging all exceptions reported in a application in a separate path.

  2. Log4j.jar has to be placed in the lib folder along with other related jar files.

Useful Link : http://www.mkyong.com/struts/struts-log4j-integration-example/


You can place the following log4j.xml in your classpath:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
    <appender name="CA" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%-4r [%t] %-5p %c %x - %m%n" />
        </layout>
    </appender>
    <root>
        <level value="debug" />
        <appender-ref ref="CA" />
    </root>
</log4j:configuration>

And you need to have the proper logging statements in your app, for example:

public class HelloWorld {

    static Logger logger = Logger.getLogger(HelloWorld.class);

    public static void main(String[] args) {
        logger.debug("Sample debug message");
        logger.info("Sample info message");
        logger.warn("Sample warn message");
        logger.error("Sample error message");
        logger.fatal("Sample fatal message");
    }
}


I doubt your asking this but just in case (its unclear with your question):

If you are looking to log any exception that your application (your code) throws you will have to use something like aspectj. To do this other wise you will have to:

try {
// some questionable code
}
catch(Exception e) {
   log.error(e);
   throw new RuntimeException(e);
}


Log4j has three main components: loggers, appenders and layouts. These three types of components work together to enable developers to log messages according to message type and level, and to control at runtime how these messages are formatted and where they are reported.

See this example

0

精彩评论

暂无评论...
验证码 换一张
取 消