var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-20738293-1']); _gaq.push(['_trackPageview']); (function() { var ga = document.createElement('script')"/>
jutleo
欢迎走进有风的地方~~
posts - 63,  comments - 279,  trackbacks - 0
关于Log4j的使用看代码吧!注释写的很详细,苦于没有运行日志信息的代码,写那些helloworld显得不合适,写出了五种遍历List的方法,没有专门的测试工具测试他们运行的效率,哪位要是能测试一下,烦请告诉具体情况!
package org.bulktree.log4j;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;

public class Log4jTest {

    
// 获取日志记录器
    static Logger logger = Logger.getLogger(Log4jTest.class);

    
// static Logger logger = Logger.getLogger(Log4jTest.class.getName());

    Log4jTest() 
{
        
// 配置日志环境

        
// 使用缺省Log4j环境
        BasicConfigurator.configure();
        
// 读取使用Java属性文件编写的配置文件
        
// PropertyConfigurator.configure("log4j.properties");
        
// 读取XML形式的配置文件
        
// DOMConfigurator.configure(String filename);
    }


    
public static void main(String[] args) {

        logger.debug(
"Log4jTest-->>debug");
        logger.info(
"Log4jTest-->>info");
        logger.warn(
"Log4jTest-->>warn");
        logger.error(
"Log4jTest-->>error");

        System.out.println(
"************test****************");

        List
<String> list = new ArrayList<String>();

        list.add(
"BULKTREE");
        logger.debug(
"debug-->>List Success! " + "list information: " + list);
        logger.info(
"info-->>List Success! " + "list information: " + list);
        logger.warn(
"warn-->>List Success! " + "list information: " + list);
        logger.error(
"error-->>List Success! " + "list information: " + list);

        list.add(
"LAOSHULIN");
        logger.debug(
"debug-->>List Success! " + "list information: " + list);
        logger.info(
"info-->>List Success! " + "list information: " + list);
        logger.warn(
"warn-->>List Success! " + "list information: " + list);
        logger.error(
"error-->>List Success! " + "list information: " + list);

        list.add(
"OAKERTREE");
        logger.debug(
"debug-->>List Success! " + "list information: " + list);
        logger.info(
"info-->>List Success! " + "list information: " + list);
        logger.warn(
"warn-->>List Success! " + "list information: " + list);
        logger.error(
"error-->>List Success! " + "list information: " + list);

        list.add(
"BULKTREE.LAOSHULIN.OAKERTREE");
        logger.debug(
"debug-->>List Success! " + "list information: " + list);
        logger.info(
"info-->>List Success! " + "list information: " + list);
        logger.warn(
"warn-->>List Success! " + "list information: " + list);
        logger.error(
"error-->>List Success! " + "list information: " + list);

        Log4jTest lt 
= new Log4jTest();
        lt.firstReadList(list);
        lt.secendReadList(list);
        lt.threeReadList(list);
        lt.fourReadList(list);
        lt.fiveReadList(list);
    }


    
public void firstReadList(List<String> list) {
        
for (Iterator<String> iter = list.iterator(); iter.hasNext();) {

            String str 
= iter.next();
            logger.debug(
"debug-->>Iterator have next element:" + str);
            logger.info(
"info-->>Iterator have next element:" + str);
            logger.warn(
"warn-->>Iterator have next element:" + str);
            logger.error(
"error-->>Iterator have next element:" + str);

            System.out.println(
"*********" + str);
        }

    }


    
public void secendReadList(List<String> list) {
        
for (int i = 0; i < list.size(); i++{

            String str 
= list.get(i);
            logger.debug(
"debug-->>Iterator have next element:" + str);
            logger.info(
"info-->>Iterator have next element:" + str);
            logger.warn(
"warn-->>Iterator have next element:" + str);
            logger.error(
"error-->>Iterator have next element:" + str);

            System.out.println(
"========" + str);
        }

    }


    
public void threeReadList(List<String> list) {
        
for (String str : list) {
            logger.debug(
"debug-->>Iterator have next element:" + str);
            logger.info(
"info-->>Iterator have next element:" + str);
            logger.warn(
"warn-->>Iterator have next element:" + str);
            logger.error(
"error-->>Iterator have next element:" + str);

            System.out.println(
"&&&&&&&&" + str);
        }

    }


    
public void fourReadList(List<String> list) {
        
for (int i = 0, a = list.size(); i < a; i++{

            String str 
= list.get(i);
            logger.debug(
"debug-->>Iterator have next element:" + str);
            logger.info(
"info-->>Iterator have next element:" + str);
            logger.warn(
"warn-->>Iterator have next element:" + str);
            logger.error(
"error-->>Iterator have next element:" + str);

            System.out.println(
"########" + str);
        }

    }


    
public void fiveReadList(List<String> list) {
        Iterator
<String> iter = list.iterator();
        
while (iter.hasNext()) {

            String str 
= iter.next();
            logger.debug(
"debug-->>Iterator have next element:" + str);
            logger.info(
"info-->>Iterator have next element:" + str);
            logger.warn(
"warn-->>Iterator have next element:" + str);
            logger.error(
"error-->>Iterator have next element:" + str);

            System.out.println(
"$$$$$$$$" + str);
        }

    }

}



# An example log4j configuration file that outputs to System.out.  The
# output information consists of relative time, log level, thread
# name, logger name, nested diagnostic context and the message in that
# order.

# For the general syntax of property based configuration files see the
# documenation of org.apache.log4j.PropertyConfigurator.

#配置根Logger,语法为:log4j.rootLogger=[level],appenderName,appenderName,.
#level是日志记录器的优先级,Log4j建议只使用四个级别,从高到低分别是ERROR,
#WARN,INFO,DEBUG,通过在这里定义的级别,您可以控制到应用程序中相应级别的日志信息的开关。
#比如在这里定义了INFO级别,则应用程序中所有DEBUG级别的日志信息将不被打印出来
#appenderName指的是制定日志信息输出到哪个地方
#A1标识输出控制台,A2标识输出到日志文件
log4j.rootLogger=DEBUG, A1,A2
#Log4j提供的appender有以下几种: 
#org.apache.log4j.ConsoleAppender(控制台) 
#org.apache.log4j.FileAppender(文件)
#org.apache.log4j.DailyRollingFileAppender(每天产生一个日志文件)
#org.apache.log4j.RollingFileAppender(文件大小到达指定尺寸的时候产生一个新的文件) 
#org.apache.log4j.WriterAppender(将日志信息以流格式发送到任意指定的地方
# A1 is set to be a ConsoleAppender which outputs to System.out. 
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A2=org.apache.log4j.RollingFileAppender
log4j.appender.A2.File=Log4jTest.log
#如果超过20KB则自动新建一个文件,文件名后面自动加序号
log4j.appender.A2.MaxFileSize=20KB

# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout

# The conversion pattern uses format specifiers. You might want to
# change the pattern an watch the output format change.
log4j.appender.A1.layout.ConversionPattern=%-4r %-5p [%t] %37c %3x - %m%n

# In this example, we are not really interested in INNER loop or SWAP
# messages. See the effects of uncommenting and changing the levels of
# the following loggers.
# log4j.logger.org.apache.log4j.examples.SortAlgo.INNER=WARN
# log4j.logger.org.apache.log4j.examples.SortAlgo.SWAP=WARN

# Keep one backup file
log4j.appender.A2.MaxBackupIndex=1
log4j.appender.A2.layout=org.apache.log4j.PatternLayout
log4j.appender.A2.layout.ConversionPattern=%p %t %c - %m%n
# Print only messages of level WARN or above in the package com.foo.
log4j.logger.org.bulktree=DEBUG
 
posted on 2008-08-13 13:59 凌晨风 阅读(2086) 评论(6)  编辑  收藏 所属分类: Java学习笔记

FeedBack:
# re: Log4j的使用
2008-08-14 11:37 | 异次元
不错,收藏了  回复  更多评论
  
# re: Log4j的使用
2008-08-14 17:39 | KingBack
不错, 收藏

想请教一下晨风: 我看过你那遍关于 struts2 中 select tag的用法之后, 还不是很明白, 自己试做也没能成功, 能否请你把有关 select tag的用法详细解释一下呢? Thanks!  回复  更多评论
  
# re: Log4j的使用
2008-08-14 20:36 | 凌晨风
http://struts.apache.org/2.1.2/docs/select.html这个是List tag的详细介绍。List tag只有list属性是必须的,
request范围内有个List集合(student)里装着所有学生的姓名,你应该这样遍历:<s:select list="student" name="selectTest" />
这样仅限于展示出数据。
你可以对照着HTML:
<select name="selectTest">
<option value="">请选择</option>
<option value="0">bulktree</option>
<option value="1">oakertree</option>
<option value="2">laoshulin</option>
</select>
  回复  更多评论
  
# re: Log4j的使用
2008-08-14 20:47 | 凌晨风
当你把数据展示出来后要是还需要下一步的传输,在下一个页面获得select中选中的值,Example:request.getParameter("selectTest");对于HTML值为0对应显示是"bulktree",而Struts2 List Tags,则是listKey对应ListValue,headerKey对应headerValue  回复  更多评论
  
# re: Log4j的使用
2008-08-15 09:33 | KingBack
Great! I had resolved the problem. Thanks a lot :)

Cause is not initialize List, after i had initialized it in execute method, i run it successfully.

Thanks again.  回复  更多评论
  
# re: Log4j的使用[未登录]
2009-03-11 15:59 | Raul
打个时间戳看看执行效率呗  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航:
 

<2008年8月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
31123456

常用链接

留言簿(11)

我参与的团队

随笔分类

随笔档案

文章分类

文章档案

新闻分类

新闻档案

收藏夹

围脖

最新随笔

搜索

  •  

最新评论

阅读排行榜

评论排行榜