Java Log4j configuration instructions

Source: Internet
Author: User

When log4j is called to output logs in XXX class, the following statement is called at first,

Public static logger = org. Apache. log4j. Logger. getlogger (class name. Class. getname ());

This method can be used to set the Log Level of the class in the configuration file. For example, add the following lines to the configuration file log4j. properties:

Log4j. rootcategory = error, stdout, file -- Overall log output level

Log4j. Logger. AAA. BBB. ccc. xxx = debug -- the log output level of a separate class. If not set, the overall setting is used.

In this way, the log output level of XXX class is different from that of other classes for debug.

Simple encapsulation of log classes, easier to use

package com.cloudclass.test;import org.apache.log4j.Logger;import org.apache.log4j.PropertyConfigurator;public class Log {public Logger loger;private static Log log;private Log(){String filePath = this.getClass().getResource("/").getPath();filePath = filePath.substring(1).replace("bin", "src");loger = Logger.getLogger(this.getClass());PropertyConfigurator.configure(filePath+"log4j.properties"); }static Log getLoger(){if(log!=null)return log;elsereturn new Log();} public static void main(String[] args) {      Log log = Log.getLoger();    String str = "test";    log.loger.info("info");    log.loger.info("info is " + str);    log.loger.debug("debug");    log.loger.debug("debug is " + str);    log.loger.error("error");    log.loger.error("error is " + str); }}

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.