Chinaunix首页 | 论坛 | 博客
  • 博客访问: 30465414
  • 博文数量: 708
  • 博客积分: 12163
  • 博客等级: 上将
  • 技术积分: 8240
  • 用 户 组: 普通用户
  • 注册时间: 2007-12-04 20:59
文章分类

全部博文(708)

分类: Java

2008-03-17 11:45:36

在struts里应用log4j写日志,一般是放在classes目录下的名字是log4j.propertyies或log4j.xml。log4j.xml功能比较强一点。例子的大概内容是这样的
                                                        
                                  
         
                                                                         
                                                                  
 

 
  
                                     value="%d %-5p [%t] %C (%F:%L) - %m%n"/>
  

 



 
  
  
        
          
  
                                     value="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
  

  
        
          
        
      

 

 
  
  
        
         
  
                                     value="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
  
                                                                      
  
         
             
      
                                                                    
 
                                                              
                        
                                             
  
                                                                   
                                         
                                             
  
                                                                    
                                                                         
                                         
                               
                                                                                
  
                                                                       
                                                    

其中append就一个日志的输出点,可以是文件,console或者其他(如mail).
如果是自己写程序时用,没有那么麻烦,建个log4j.properties
内容如下:
#levels: debug, info, warn, error, fatal         
log4j.rootCategory=DEBUG, stdout, logfile

log4j.appender.stdout=org.apache.log4j.ConsoleAppender                        
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout                   
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - <%m>%n

log4j.appender.logfile=org.apache.log4j.RollingFileAppender
# ---------------- Attention here ----------------------------
log4j.appender.logfile.File=koala.log                                    
log4j.appender.logfile.MaxFileSize=512KB                         
# Keep three backup files                                                     
log4j.appender.logfile.MaxBackupIndex=3                          
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
# Pattern to output : date priority [category] - line_separator
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - <%m>%n
仔细看看,然后写一下就知道什么意思了。

然后写每个类时加一句话:
Log log = LogFactory.getLog(所在类名.class),然后程序里就
if (log.isInfo) {
     log.info("信息");
}
写一下就知道了。
阅读(1853) | 评论(0) | 转发(0) |
0

上一篇:Log4J 使用心得

下一篇:关于接口

给主人留下些什么吧!~~