Logback in Servlet

Source: Internet
Author: User

Before reading log4j2, I want to use it in the project. This guy is not very easy to use in servlet later. Although it is quite new, the beta version is always not suitable for online products. I have seen some people say that logback is awesome. Today, I decided to give it a try. If you are familiar with the Web, you only need to check Step 2 and Step 5. The rest are basic web configuration items.

Step 1: Create a new web project in myeclipse.

Step 2: Then directly put the logback-core-1.0.9.jar, logback-classic-1.0.9.jar, slf4j-api-1.7.2.jar and logback-access-1.0.9.jar dragged to the project under the WEB-INF/lib directory.

The reason for importing the logback-access-1.0.9.jar package is "The logback-access module integrates with servlet containers, such as Tomcat and jetty, to provide HTTP-access log functionality ."

Step 3: Add a form to the JSP page. Action points to the servlet to be tested and a button is added to trigger the servlet on the page.

Step 4: Configure in Web. xml

Step 5: Create a New testservlet and enter the logback-relatedCode

 Import Java. Io. ioexception;  Import  Javax. servlet. servletexception;  Import  Javax. servlet. http. httpservlet;  Import  Javax. servlet. http. httpservletrequest;  Import  Javax. servlet. http. httpservletresponse;  Import  Org.Slf4j. Logger;  Import  Org.Slf4j. Loggerfactory; Public   Class Testservlet Extends  Httpservlet {  Private   Static   Final   Long Serialversionuid = 1l ; @ Override  Protected   Void  Service (httpservletrequest arg0, httpservletresponse arg1)  Throws  Servletexception, ioexception {logger Logger = Loggerfactory. getlogger (testservlet. Class  . Getname ());Logger. Error( "Hello world ." );}} 

We can see that logback uses slf4j, so you don't have to worry about switching other logging frameworks.

Step 6: Run as-myeclipse server application

Select Tomcat 7.0

Step 7: Re-trigger the button in the browser to make the servlet called.

View the output result on the console.

Now, the test of using logback in servlet has been completed.

After reading the logback architecture, I feel that it is more concise and clearer than log4j2, and there are many similarities, such as the hierarchy inheritance relationship between logger.

The configuration of logback is similar to that of log4j2. It is clearer than log4j2, And there is automatic retrieval.

Log4j2, which has been around for so long, seems a waste of time. You should check logback at the beginning.

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.