Log4j in eclipse cannot be output to the console

Source: Internet
Author: User

Log4j cannot be output to the console after a long time. The log4j. properties file is also stored everywhere.Please initialize the log4j system properly.

So I decidedCodeConfigure a simple stdlogger to implement the basic log function.

 
Import Org. apache. log4j. *; public class log {public static logger stdlogger = NULL; static {stdlogger = logger. getlogger ("stdout"); consoleappender CA = new consoleappender (New patternlayout (patternlayout. ttcc_conversion_pattern), "system. out "); stdlogger. addappender (CA );}}

It is worth noting that there must be a parameter in the new consoleappender. You must specify the patternlayout parameter. Otherwise, an error will be reported:

Error no output stream or file set for the appender named [null].

The reason is that the constructor without parameters does not perform any initialization operations.

This is the consoleappender source code:

45/*** constructs an unconfigured appender. 47 */48 Public consoleappender () {49} 50 51/** 52 * creates a configured appender. 53*54 * @ Param layout, may not be null. 55 */56 public consoleappender (layout) {57 This (layout, system_out); 58} 59 60/** 61 * creates a configured appender. 62 * @ Param layout, may not be null. 63 * @ Param Target target, either "system. err "or" system. out ". 64 */65 public consoleappender (layout, string target) {66 setlayout (layout); 67 settarget (target); 68 activateoptions (); 69}

Therefore, parameters must be input in the constructor.
In addition, to format the output like printf or system. Out. format, you can wrap the output function as follows:

Import Org. apache. log4j. *; public class log {public static logger stdlogger = NULL; static {stdlogger = logger. getlogger ("stdout"); consoleappender CA = new consoleappender (New patternlayout (patternlayout. default_conversion_pattern), "system. out "); stdlogger. addappender (CA); stdlogger. setlevel (level. debug);} public static void debug (string format, object... ARGs) {string STR = string. format (format, argS); stdlogger. debug (STR);} public static void Info (string format, object... ARGs) {string STR = string. format (format, argS); stdlogger.info (STR);} public static void warn (string format, object... ARGs) {string STR = string. format (format, argS); stdlogger. warn (STR);} public static void error (string format, object... ARGs) {string STR = string. format (format, argS); stdlogger. error (STR);} public static void fatal (string format, object... ARGs) {string STR = string. format (format, argS); stdlogger. fatal (STR );}}

InProgramYou can use it like this:

 log. debug ("thread % s get % d", thread. currentthread (). getname (), result); 
Related Article

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.