Log4j
This thing, everyone is familiar with, simple introduction, is a point, because I myself in the log logs to build, no experience, but this thing is really very important, in the future to adjust the bug does not have it basic impossible, if there is a friend what better log log using the tutorial, I hope you can tell me.
log4j configuration file
Contents: Resource, file name: log4j.properties
Create a new log4j configuration file, this file is placed in the resource root directory, it seems that, when the project started, will be automatically loaded, this I do not understand, because I did not study the log4j, just read a few articles online.
1 #定义LOG输出级别2 Log4j.rootlogger=info,console,file3 #定义日志输出目的地为控制台4 Log4j.appender.console=org.apache.log4j.consoleappender5 Log4j.appender.console.target=system.out6 #可以灵活地指定日志输出格式, the following line specifies the specific format7 log4j.appender.Console.layout = Org.apache.log4j.PatternLayout8 log4j.appender.console.layout.conversionpattern=[%c]-%m%n9 #文件大小到达指定尺寸的时候产生一个新的文件Ten log4j.appender.File = Org.apache.log4j.RollingFileAppender One #指定输出目录 A log4j.appender.File.File = Logs/ssm.log - #定义文件最大大小 - log4j.appender.File.MaxFileSize = 10MB the # Output so log, if switching to debug indicates output debug above level log - Log4j.appender.File.Threshold = All - log4j.appender.File.layout = Org.apache.log4j.PatternLayout -Log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-mm-dd hh\:mm\:ss}][%c]%m%n
log4j.properties
log4j Re-encapsulation tool
Contents: Com.magic.rent.util, file name: Log.java
I personally have obsessive-compulsive disorder, do not like in each class to create a logger object, so this thing encapsulated into a static method, this is mainly for different people I think.
1 PackageCom.magic.rent.util;2 3 Importorg.apache.commons.logging.LogFactory;4 5 Public classLog {6 7 Public Static voidinfo (Object object, string tittle, string text) {8Org.apache.commons.logging.Log logger =Logfactory.getlog (Object.getclass ());9String Infostart = "\ n ———————————————————————————————————————————————————————— [Info start]--\n";TenString infoend = "———————————————————————————————————————————————————————— [Info end]--\n"; OneStringBuffer StringBuffer =NewStringBuffer (infostart); AStringbuffer.append ("title:" + tittle + "\ n content:") -. append (text + "\ n")) - . Append (infoend); the Logger.info (stringbuffer.tostring ()); - } - - Public Static voiderror (Object object, string tittle, string text, Throwable t) { +Org.apache.commons.logging.Log logger =Logfactory.getlog (Object.getclass ()); -String Infostart = "\ n ———————————————————————————————————————————————————————— [Info start]--\n"; +String infoend = "———————————————————————————————————————————————————————— [Info end]--\n"; AStringBuffer StringBuffer =NewStringBuffer (infostart); atStringbuffer.append ("title:" + tittle + "\ n content:") -. append (text + "\ n")) - . Append (infoend); - Logger.error (stringbuffer.tostring (), t); - } - in Public Static voidDebug (Object object, string tittle, string text) { -Org.apache.commons.logging.Log logger =Logfactory.getlog (Object.getclass ()); toString Infostart = "\ n ———————————————————————————————————————————————————————— [Info start]--\n"; +String infoend = "———————————————————————————————————————————————————————— [Info end]--\n"; -StringBuffer StringBuffer =NewStringBuffer (infostart); theStringbuffer.append ("title:" + tittle + "\ n content:") *. append (text + "\ n")) $ . Append (infoend);Panax Notoginseng Logger.debug (stringbuffer.tostring ()); - } the +}
Log.java
The actual invocation of this writing is this code:
Log.info (This, "Filter link Address", "Servletpath:" + Servletpath);
And the playing out of the log effect, is like this, I feel very clear to look at ... Personal Feeling--
As you can see, the first line outputs the "class name" of the log.
The subsequent start is the body content, the caption corresponds to the string tittle this parameter, and the content corresponds to a string text.
Well, I'm trying to add a timestamp ... Of course, it is not difficult to add a time error, there is a need to handle it yourself. That's the basic format.
Javaweb Spring+springmvc+mybatis+springsecurity+ehcache+jcaptcha Full Web infrastructure (ii)