文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>使用log4j

使用log4j

时间:2006-12-31  来源:liulibo

获取logger
Logger.getRootLogger()  获取根logger
Logger.getLogger(String name)获取子logger
Logger.getLogger(Class clazz)或
Logger.getLogger(clazz.getName())

设置日志级别(.setLevel(int,Exception))
Level.ALL打开所有日志
Level.DEBUG 用于调试
Level.INFO 用于运行过程
Level.WARN 用于潜在的错误
Level.ERROR 用于错误事件
Level.FATAL 用于严重错误时间
Level.OFF 关闭所有日志

输出端Appender(.addAppender(Appender).setAdditivity(boolean additive)是否覆盖)
org.apache.log4j.ConsoleAppender  输出到控制台
 targer:
  ConsoleAppender.SYSTEM_OUT(Default)
  ConsoleAppender.SYSTEM_ERR
 public ConsoleAppender(Layout)
 public ConsoleAppender(Layout,String targer)
org.apache.log4j.FileAppender   输出到文件
 public FileAppender(Layout,String fileName)
 public FileAppender(Layout,String fileName,boolean append)是否覆盖
org.apache.log4j.DailyRollingFileAppender 输出到文件,每天一个新文件
org.apache.log4j.RollingFileAppender  输出到文件,自动新增改名
 public RollingFileAppender(Layout,String fileName)
 void setMaxBackupIndex(int index) 设置日志文件最大备份数
 void setMaximumFileSize(long size) 设置日志文件最大尺寸
org.apache.log4j.WriterAppender  流格式输出到任意地方
org.apache.log4j.JDBCAppender  输出到数据库

日志格式化(Layout)
%c   类全名
%d   时间
%f   类名
%l   位置
%m   信息
%n   换行
%p   级别
%r   耗时
%t   线程名

public PatternLayout() 使用默认设置DEFAULT_CONVERSION_PATTERN 只打印信息
public PatternLayout(String)使用自定义的pattern构造一个PatternLayout
void setConversionPattern(String)  设置日志格式
HTMLLayout
SimpleLayout


5.
1.BasicConfigurator.configure()
 PatternLayout p = new PatternLayout("%p [%t] %c (%F:%L) - %m%n");  
 ConsoleAppender a = new ConsoleAppender(p,ConsoleAppender.SYSTEM_OUT); 
 root.addAppender(a);         
 rootLogger.setLevel(Level.DEBUG);       
2.PropertyConfigurator.configure("/help/example.properties")
  String resource = "/help/example.properties";
  URL configFileResource = Log4J.class.getResource(resource);
  PropertyConfigurator.configure(configFileResource);
3.DOMConfigurator.configure("/help/example.xml")
xml declaration and dtd
|
log4j:configuration
|
+-- appender (name, class)
| |
| +-- param (name, value)
| +-- layout (class)
| |
| +-- param (name, value)
+-- logger (name, additivity)
| |
| +-- level (class, value)
| | |
| | +-- param (name, value)
| +-- appender-ref (ref)
+-- root
|
+-- param (name, class)
+-- level
| |
| +-- param (name, value)
+-- appender-ref (ref)


0.log4j.properties模板
log4j.rootLogger=info,CONSOLE,RFILE,FILE,DB      设置级别和三个输出端
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= %4p [%t] (%F:%L) - %m%n 

log4j.appender.FILE=org.apache.log4j.FileAppender     
log4j.appender.FILE.File=/help/my.properties       目标文件
log4j.appender.FILE.Append=false        是否追加
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout     布局模式
log4j.appender.FILE.layout.ConversionPattern=%d{yyyy-MM-dd hh:mm:ss}:%p %c:%L - %m%n   格式化布局

log4j.appender.RFILE=org.apache.log4j.RollingFileAppender     
log4j.appender.RFILE.File=/help/my.properties       目标文件
log4j.appender.RFILE.MaxFileSize=1KB        最大长度
log4j.appender.RFILE.MaxBackupIndex=3       最多备份
log4j.appender.RFILE.layout=org.apache.log4j.PatternLayout     布局模式
log4j.appender.RFILE.layout.ConversionPattern=%d{yyyy-MM-dd hh:mm:ss}:%p %c:%L - %m%n   格式化布局

log4j.appender.DB=org.apache.log4j.jdbc.JDBCAppender     
log4j.appender.DB.URL=jdbc:oracle:thin:@127.0.0.1:1521:mumu     URL
log4j.appender.DB.driver=oracle.jdbc.driver.OracleDriver     驱动
log4j.appender.DB.user=liulibo         用户名
log4j.appender.DB.password=liulibo        密码
log4j.appender.DB.layout=org.apache.log4j.PatternLayout     布局模式
log4j.appender.DB.layout.ConversionPattern=insert into log4j(createdate,thread,level_,class,message) values(\'%d\',\'%t\',\'%-5p\',\'%c\',\'%m\')

create table log4j(createdate varchar2(32),thread varchar2(32),level_ varchar2(32),class varchar2(32),message varchar2(32));
 
0.XML模板
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration>
       
 <appender name="ConsoleAppender" class="org.apache.log4j.ConsoleAppender">
  <layout class="org.apache.log4j.PatternLayout">
   <param name="ConversionPattern" value="%d{yyyy-MM-dd hh:mm:ss}:%p %t %c - %m%n"/>
  </layout>
 </appender>
 
 <appender name="RollingFileAppender" class="org.apache.log4j.RollingFileAppender">
     <param name="Append" value="false"/>
     <param name="MaxFileSize" value="1KB"/>
     <param name="File" value="dom/my.log"/>
     <param name="MaxBackupIndex" value="3"/>
     <layout class="org.apache.log4j.PatternLayout">
       <param name="ConversionPattern" value="%d{yyyy-MM-dd hh:mm:ss}:%p %t %c - %m%n"/>
     </layout>
 </appender>
 
 <appender name="JDBCAppender" class="org.apache.log4j.jdbc.JDBCAppender">
     <param name="URL" value="jdbc:oracle:thin:@127.0.0.1:1521:mumu"/>
     <param name="user" value="liulibo"/>
     <param name="password" value="liulibo"/>
     <param name="driver" value="oracle.jdbc.driver.OracleDriver"/>
     <layout class="org.apache.log4j.PatternLayout">
       <param name="ConversionPattern" value="insert into log4j(createdate,thread,level_,class,message) values(\'%d\',\'%t\',\'%-5p\',\'%c\',\'%m\')"/>
     </layout>
 </appender>
 
 <root>
  <priority value ="debug" />
  <appender-ref ref="ConsoleAppender"/>
  <appender-ref ref="RollingFileAppender"/>
  <appender-ref ref="JDBCAppender"/>
 </root>

</log4j:configuration>

应用
 web.xml
  <context-param>
   <param-name>props</param-name>
   <param-value>/WEB-INF/log4j.properties</param-value>
  </context-param>
 
  <listener>
   <listener-class>cart.listener.SCServletContextListener</listener-class>
  </listener>
 初始化方法中添加
 private void initLog4j(ServletContext context){
   String prefix = context.getRealPath("/");
   System.out.println("prefix:"+prefix);
   String props = context.getInitParameter("props");
  if(props != null) {
   PropertyConfigurator.configure(prefix+props);
  }
  Logger logger = Logger.getLogger(SCServletContextListener.class); 
   }

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载