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);