Log files are necessary to record key operations, such as some internal key operations, and need to know who it is. did what. How to do it. Whether it is through legal landing and so on.
Log file configuration and writing is very simple, in fact, they can also write a, that is, some of the operation of the content or need to write things to file the way to save, simple file operations can be completed log records. Sure, there's a ready-made one. That's not to say I'm stupid. Log4j (LOGFORJ) is very easy to use.
The SRC directory is established under the Log4j.properties file. Fixed, instead of the other name. Note that if you do not use the struts2 frame, then you are importing log4j jar packages into the project. If used, then Struts2 has already given the jar package containing the log4j, we don't need to import it. (I'm talking about the STRUTS2 support that is added under MyEclipse).
log4j.properties the contents of the document are explained:
#第一部分: Log4j.rootlogger, description of what level of log generated and appender names
#格式: Log4j.rootlogger= Log level (6), Appender list (separated by commas)
Log4j.rootlogger=info,console,file
#第二部分: Log4j.appender, indicating the destination of the log output
#格式: Appender class provided by Log4j.appender.Appender name =log4j
#格式: Log4j.appender.Appender name. Select name = option value
#第三部分: Layout, describing the format of the log generation
#格式: Log4j.appender.Appender name. Layout class provided by layout=log4j
#格式: Log4j.appender.Appender name. layout. Select name = option value
# Apply to Console
Log4j.appender.console=org.apache.log4j.consoleappender
Log4j.appender.console.target=system.out
Log4j.appender.console.layout=org.apache.log4j.patternlayout
Log4j.appender.console.layout.conversionpattern=[struts1.x]%d-%c-%-4r [%t]%-5p%c%x-%m%n
#log4j. Appender.console.layout.conversionpattern=[start]%d{date}[date]%n%p[priority]%n%x[ndc]%n%t[thread] n%c[ category]%n%m[message]%n%n
#应用于文件
Log4j.appender.file=org.apache.log4j.fileappender
Log4j.appender.file.file=c\:/log.html
Log4j.appender.file.layout=org.apache.log4j.htmllayout
#log4j. appender.file.layout.conversionpattern=[struts1.x]%d-%c-%-4r [%t]%-5p%c%x-%m%n
Here's a piece of real Code (story):
Contents of log4j.properties File:
Log4j.rootlogger=info,console,file
Log4j.appender.console=org.apache.log4j.consoleappender
Log4j.appender.console.target=system.out
Log4j.appender.console.layout=org.apache.log4j.patternlayout
Log4j.appender.console.layout.conversionpattern=[struts1.x]%d-%c-%-4r [%t]%-5p%c%x-%m%n
Log4j.appender.file=org.apache.log4j.fileappender
Log4j.appender.file.file=d\:/flowershop.html
Log4j.appender.file.layout=org.apache.log4j.htmllayout
Online Florist Record Admin login code, add the following code to the corresponding action:
public static Logger Log=logger.getlogger (Managerloading.class);
Managerloading is the current class.
Log.info (New Date (). toString () + "admin" +mana.gettruename () + "landed ... ");
In the login successful processing code to add this sentence (according to their own needs to write), you can record the log. Save the content must be time + Admin XXX landed ...
The last log file is saved in d\:/flowershop.html and can be viewed.