海边拾贝shellfish.blog.chinaunix.net
simonsun
全部博文(2)
2010年(1)
2008年(1)
分类: Java
2008-08-15 10:17:09
import java.io.IOException; import java.io.InputStream; import java.util.Properties; import org.apache.log4j.BasicConfigurator; import org.apache.log4j.LogManager; import org.apache.log4j.Logger; import org.apache.log4j.PropertyConfigurator; /** * Logs messages into File and DB. * * Debug < Info < Warn < Error * * */ public class LoggerManager { public static final String PROFILE = "Log4jFile.properties"; /** * Holds singleton instance */ private static LoggerManager impl; static { impl = new LoggerManager(); } private static Logger log4j; /** * prevents instantiation */ private LoggerManager() { log4j = LogManager.getLogger(LoggerManager.class); try { Properties pro = new Properties(); InputStream is = getClass().getResourceAsStream(PROFILE); pro.load(is); PropertyConfigurator.configure(pro); } catch (IOException e) { BasicConfigurator.configure(); log4j.info("should change properties file..."); log4j.error(e.toString()); } } /** * Singleton Pattern -- use getLog() instead */ private static LoggerManager getInstance() { return impl; } /** * Logging: Debug < Info < Warn < Error. * * Singleton Pattern * Usage: * * * static final Logger log = LoggerManager.getLog(); * * log.debug("debug, note: ..."); * log.info("info msg"); * log.warn("Warning message"); * log.error("Error, dead"); * * * @return log4j For logging call debug(msg), info(msg), warn(msg), and * error(msg) */ public static Logger getLog() { return log4j; } public static boolean shutdownDBLog() { return false; } /** * Replace single quotes (') with double quotes ("). * @param queryString * @return queryString w/o single quotes (') */ public static String unquote(String queryString) { return queryString.replace('\'', '"'); } }
* Debug < Info < Warn < Error * * */
* Singleton Pattern * Usage: * *
* static final Logger log = LoggerManager.getLog(); * * log.debug("debug, note: ..."); * log.info("info msg"); * log.warn("Warning message"); * log.error("Error, dead"); *
附:log4jFile.properties
log4j.addivity.org.apache=truelog4j.appender.CONSOLE=org.apache.log4j.ConsoleAppenderlog4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout log4j.appender.CONSOLE.layout.ConversionPattern=%d{yyyy-MM-dd HH\:mm\:ss} %l%n%p\: %m%n%nlog4j.appender.Chainsaw=org.apache.log4j.net.SocketAppenderlog4j.appender.Chainsaw.layout=org.apache.log4j.PatternLayoutlog4j.appender.Chainsaw.layout.ConversionPattern=%d{DATE} [%-5p] %c {%F\:%L} - %m%nlog4j.appender.Chainsaw.locationInfo=truelog4j.appender.Chainsaw.port=4445log4j.appender.Chainsaw.remoteHost=127.0.0.1log4j.appender.FILE=org.apache.log4j.DailyRollingFileAppenderlog4j.appender.FILE.Append=truelog4j.appender.FILE.DatePattern=yyyyMMdd'.log'log4j.appender.FILE.File=logfilelog4j.appender.FILE.layout=org.apache.log4j.PatternLayout log4j.appender.FILE.layout.ConversionPattern=%d{yyyy-MM-dd HH\:mm\:ss} %l%n%p\: %m%nlog4j.rootLogger=ERROR, CONSOLE,FILE
上一篇:让自己老婆成为世界上最幸福的女人
下一篇:没有了
登录 注册