Java Writing log

Source: Internet
Author: User

When you do a recent Java EE: always appear after running Tomcat:

Log4j:warn No Appenders could is found for logger (samp. TEST1).
Log4j:warn Please initialize the log4j system properly.

Access to information: figuring out whether the problem is in the initialization of the log configuration file, which means that the application cannot find log4j.properties. Then we solve this problem!

Search a lot of information, solution a lot of, but really to solve the actual problem, that is also true, I this program is just to solve this warning, the simplest is to output the debug information to the console, it can be written as follows:

The following is the first initialization of log

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 it references the
 &N Bsp Logger instance named "MyApp".
   static Logger Logger = Logger.getlogger (myapp.class);

   public static void Main (string[] args) {

    //Set up a simple Configura tion that logs on the console.
     basicconfigurator.configure ();

     logger.info ("Entering application.");
     Bar bar = new Bar ();
     Bar.doit ();
     logger.info ("exiting application.");
  }
}
Then we can use it directly:

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!");
}
}

In the final analysis, the key is basicconfigurator.configure ();

Add this sentence to our web site, and we can see our debugging information on the console!

Another kind of debug file we want to write to us: that's it.

Let's write a class that initializes properties like this:

Package Com.foo;

Import Org.apache.log4j.PropertyConfigurator;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Import Java.io.PrintWriter;
Import java.io.IOException;

public class Log4jinit extends HttpServlet {

  public
  init () {
    String prefix =  Getservletcontext (). Getrealpath ("/");
    String file = Getinitparameter ("Log4j-init-file");
    If the Log4j-init-file is isn't set, then no point in trying
    if (file!= null) {
      propertyconfigurator.configure ( prefix+file);
    }

  Public
  void Doget (httpservletrequest req, httpservletresponse res) {
  }
}

write this in the configuration file:

<servlet>
<servlet-name>log4j-init</servlet-name>
<servlet-class>com.foo.Log4jInit</servlet-class>

<init-param>
<param-name>log4j-init-file</param-name>
<param-value>WEB-INF/classes/log4j.lcf</param-value>
</init-param>

<load-on-startup>1</load-on-startup>
</servlet>

This warning is far away from me!
The above two examples are only for the log added to the hibernate, which may be somewhat different from the eclipse version used.

For example, when you use spring will also have this problem, then we solve the time, we can follow the prawn introduced:

<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/config/log4j.properties</param-value>
</context-param>

······

<!--definition log4j listener-->
<listener>
<listener-class>
Org.springframework.web.util.Log4jConfigListener
</listener-class>
</listener>


Of course, some say in Contextloaderlistener change to Springcontextservle and so on, these questions or which sentence, the fact that!

More introduction See also:

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.