Simple log4j configuration and log4j Configuration
Preface:
To view debugging information about a project, it is not convenient to use System. out. println () to print the information directly on the console. Therefore, log4j can be configured to output the information to a file.
Steps:
1. Import jar package: log4j-1.2.8.jar
2. Compile the log4j configuration file: log4j. properties
### Set log levels-for more verbose logging change 'info' to 'debug' ### the output information above info level is specified and can be output to stdout on the console, and log4j in file. rootLogger = info, stdout, file ### direct log messages to stdout ### console log4j. appender. stdout = org. apache. log4j. leleappenderlog4j. appender. stdout. target = System. outlog4j. appender. stdout. layout = org. apache. log4j. patternLayoutlog4j. appender. stdout. layout. conversionPattern = % d {yyyy/MM/dd HH: mm: ss, SSS} % 5 p % c {1 }: % L-% m % n ### direct messages to file mylog. log #### output the file to only one file log4j. appender. logFile = org. apache. log4j. fileAppenderlog4j. appender. logFile. file = E:/logs/mylog.txt log4j. appender. logFile. encoding = UTF-8log4j.appender.logFile.layout = org. apache. log4j. patternLayoutlog4j. appender. logFile. layout. conversionPattern = % d {yyyy/MM/dd HH: mm: ss, SSS} % 5 p % c {1 }: % L-% m % n ### the file overd some size will create a new log file #### you can set the file size for the rolling file (automatically create a new log files ), and the number of files (after the number of files is exceeded, the new content will be stored from the first file) log4j. appender. file = org. apache. log4j. rollingFileAppenderlog4j. appender. file. append = truelog4j. appender. file. file = E:/logs/log.txt log4j. appender. file. encoding = UTF-8log4j.appender.file.MaxFileSize = 10MBlog4j. appender. file. maxBackupIndex = 10log4j. appender. file. layout = org. apache. log4j. patternLayoutlog4j. appender. file. layout. conversionPattern = % d {yyyy/MM/dd HH: mm: ss, SSS} % 5 p % c {1 }: % L-% m % n ### error information #### file only outputs error information to this file
Log4j. appender. errorfile = org. apache. log4j. fileAppender log4j. appender. errorfile. file = E:/logs/errlog.txt log4j. appender. errorfile. threshold = ERRORlog4j. appender. errorfile. append = false log4j. appender. errorfile. layout = org. apache. log4j. patternLayout log4j. appender. errorfile. layout. conversionPattern = % d {yyyy/MM/dd HH \: mm \: ss, SSS} % 5 p % c {1} \: % L-% m % n
3. Configure log4j in web. xml
If you directly place the log4j. properties file in the src path, you do not need to configure related information in the web. xml file. The project will automatically load
On the other hand, you need to configure (use the spring listener to load log4j. properties at this time, the location of log4j. properties can be put elsewhere ):
<! -- In ssh, you can use spring listeners to load --> <context-param> <param-name> log4jConfigLocation </param-name> <param-value> classpath: config/log4j. properties </param-value> </context-param> <! -- Use the spring listener to read the log4j configuration file when the application starts --> <listener-class> org. springframework. web. util. log4jConfigListener </listener-class> </listener>
4. Use log4j in the program
Public class Test {// Add this public static Logger = logger to each class. getLogger (Test. class); public static void main (String [] args) {logger.info ("little mosquito qq: 513996980 "); logger.info ("John aha method to look at gynecology Xiaowang Aha"); logger. error ("utilities"); int a = 10; try {int B = a/0;} catch (Exception e) {// print the exception information to the log file. // logger. error (e. getStackTrace () [0]); // logger. error (e. getMessage (); logger. error (getTrace (e) ;}// encapsulate the public static String getTrace (Throwable t) {StringWriter stringWriter = new StringWriter (); printWriter writer = new PrintWriter (stringWriter); t. printStackTrace (writer); StringBuffer buffer = stringWriter. getBuffer (); return buffer. toString ();}}
After running the command, you can see that the console and your log files contain printed information .....
Chat group: 527038646