org.apache.commons.logging的用法(上)

类:

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

 

1)private static Log testLogger = LogFactory.getLog("testLogger");

使用自定义的logger

 

2)private static Log testLogger = LogFactory.getLog(FullTextVideoSearchDaoImpl2Test.class);、

使用系统自带的rootlogger,如果作为api级别的代码,建议使用这个方法

 

注意使用了org.apache.commons.logging,而不是直接使用log4j,其实这样用也很方便,不必写驱动log4j的程序

 

如果自定义的log级别合适,自定义的log会显示在系统log中,另外如果找不到自定义的log,那么返回系统log

 

 

配置文件(仍然使用log4j.properties)

#Logger


#系统log

log4j.rootLogger=DEBUG, A1 

#自定义log

log4j.logger.testLogger =DEBUG, A2

 

# Appender

log4j.appender.A1=org.apache.log4j.ConsoleAppender

 

log4j.appender.A2=org.apache.log4j.FileAppender

log4j.appender.A2.File=testlog.txt

 

##PatternLayout

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

log4j.appender.A1.layout.ConversionPattern=%d %-5p [%t] %C{2} (%F:%L) - %m%n

 

log4j.appender.A2.layout=org.apache.log4j.PatternLayout

log4j.appender.A2.layout.ConversionPattern=[%d] %t %c %-5p - %m%n

 

一目了然,不再赘述了,配置文件方面的咚咚见本人以前的文章

请问那里可以下载这个包org.apache.commons.logging???????

02-09

大侠:"请问那里可以下载这个包org.apache.commons.logging???????"rn或这段代码如何才可以正常编译:rn我想取GUIDrn/**rn * @author Administratorrn *rn * TODO To change the template for this generated type comment go torn * Window - Preferences - Java - Code Style - Code Templatesrn */rnimport java.net.InetAddress;rnimport java.net.UnknownHostException;rnimport java.security.MessageDigest;rnimport java.security.NoSuchAlgorithmException;rnimport java.security.SecureRandom;rnimport java.util.Random;rnrnrnpublic class RandomGUID extends Object rn protected final org.apache.commons.logging.Log logger = org.apache.commons.logging.LogFactory.getLog(getClass());rn//上面两行出错,错误如下!!!!!rn/**C:\Documents and Settings\Administrator\桌面\U\王仕神13161066\RandomGUID.java:16: *package org.apache.commons.logging does not existrn* protected final org.apache.commons.logging.Log logger = *org.apache.commons.logging.LogFactory.getLog(getClass());rn*/rn public String valueBeforeMD5 = "";rn public String valueAfterMD5 = "";rn private static Random myRand;rn private static SecureRandom mySecureRand;rnrn private static String s_id;rn private static final int PAD_BELOW = 0x10;rn private static final int TWO_BYTES = 0xFF;rnrn /*rn * Static block to take care of one time secureRandom seed.rn * It takes a few seconds to initialize SecureRandom. You mightrn * want to consider removing this static block or replacingrn * it with a "time since first loaded" seed to reduce this time.rn * This block will run only once per JVM instance.rn */rnrn static rn mySecureRand = new SecureRandom();rn long secureInitializer = mySecureRand.nextLong();rn myRand = new Random(secureInitializer);rn try rn s_id = InetAddress.getLocalHost().toString();rn catch (UnknownHostException e) rn e.printStackTrace();rn rnrn rnrnrn /*rn * Default constructor. With no specification of security option,rn * this constructor defaults to lower security, high performance.rn */rn public RandomGUID() rn getRandomGUID(false);rn rnrn /*rn * Constructor with security option. Setting secure truern * enables each random number generated to be cryptographicallyrn * strong. Secure false defaults to the standard Random function seededrn * with a single cryptographically strong random number.rn */rn public RandomGUID(boolean secure) rn getRandomGUID(secure);rn rnrn /*rn * Method to generate the random GUIDrn */rn private void getRandomGUID(boolean secure) rn MessageDigest md5 = null;rn StringBuffer sbValueBeforeMD5 = new StringBuffer(128);rnrn try rn md5 = MessageDigest.getInstance("MD5");rn catch (NoSuchAlgorithmException e) rn logger.error("Error: " + e);rn rnrn try rn long time = System.currentTimeMillis();rn long rand = 0;rnrn if (secure) rn rand = mySecureRand.nextLong();rn else rn rand = myRand.nextLong();rn rn sbValueBeforeMD5.append(s_id);rn sbValueBeforeMD5.append(":");rn sbValueBeforeMD5.append(Long.toString(time));rn sbValueBeforeMD5.append(":");rn sbValueBeforeMD5.append(Long.toString(rand));rnrn valueBeforeMD5 = sbValueBeforeMD5.toString();rn md5.update(valueBeforeMD5.getBytes());rnrn byte[] array = md5.digest();rn StringBuffer sb = new StringBuffer(32);rn for (int j = 0; j < array.length; ++j) rn int b = array[j] & TWO_BYTES;rn if (b < PAD_BELOW)rn sb.append('0');rn sb.append(Integer.toHexString(b));rn rnrn valueAfterMD5 = sb.toString();rnrn catch (Exception e) rn logger.error("Error:" + e);rn rn rnrn /*rn * Convert to the standard format for GUIDrn * (Useful for SQL Server UniqueIdentifiers, etc.)rn * Example: C2FEEEAC-CFCD-11D1-8B05-00600806D9B6rn */rn public String toString() rn String raw = valueAfterMD5.toUpperCase();rn StringBuffer sb = new StringBuffer(64);rn sb.append(raw.substring(0, 8));rn sb.append("-");rn sb.append(raw.substring(8, 12));rn sb.append("-");rn sb.append(raw.substring(12, 16));rn sb.append("-");rn sb.append(raw.substring(16, 20));rn sb.append("-");rn sb.append(raw.substring(20));rnrn return sb.toString();rn rnrnrn // Demonstraton and self test of classrn public static void main(String args[]) rn for (int i=0; i< 100; i++) rn RandomGUID myGUID = new RandomGUID();rn System.out.println("Seeding String=" + myGUID.valueBeforeMD5);rn System.out.println("rawGUID=" + myGUID.valueAfterMD5);rn System.out.println("RandomGUID=" + myGUID.toString());rn rn rnrnrnrnrn

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试