当前位置: 代码迷 >> Web前端 >> 取得本类(java文件)的路径【huiqinbo】this.getClass().getResource("/").getPath()
  详细解决方案

取得本类(java文件)的路径【huiqinbo】this.getClass().getResource("/").getPath()

热度:396   发布时间:2012-11-25 11:44:31.0
获得本类(java文件)的路径【huiqinbo】this.getClass().getResource("/").getPath()

package com.huiqinbo;????

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

???
/**??
30.* @author huiqinbo??
31.* Log演示程序??
32.*/???
public class Log {
?private Logger loger;
??? private static Log log;
??? private Log()????
??? {
??????? String filePath=this.getClass().getResource("/").getPath();
??????? filePath=filePath.substring(1).replace("bin", "src");
??????? PropertyConfigurator.configure(filePath+"log4j.properties");
??????? loger=Logger.getLogger(this.getClass());
??? }????
????????
??? static Log getLoger()????
??? {
??????? if(log!=null)????
??????????? return log;????
??????? else???
??????????? return new Log();????
??? }
???
???
??? public static void main(String args[])????
??? {????
??????? Log log=Log.getLoger();????
??????? try???
??????? {????
??????????? int c=2/0;????
??????? }catch(Exception e)????
??????? {
??????????? e.printStackTrace();
??????????? log.loger.error("今天气不错");
??????????? log.loger.error("error", e);
??????????? new Log().write();
????????????????
??????? }????
??? }
??? public void open(){
??? ?
??? }
??? public void close(){
??? ?
??? }
??? public void write(){
??? ?log.loger.error("今天气不错");
??? }
???
}

?

?

?

?

log4j.properties

?

view plaincopy to clipboardprint???
#\u5B9A\u4E49DEBUG\u4F18\u5148\u7EA7\uFF0CR\u4E3A\u65E5\u5FD7\u8F93\u51FA\u76EE\u7684\u7684????
log4j.rootLogger=DEBUG, R????
#org.apache.log4j.ConsoleAppender?
log4j.appender.R=org.apache.log4j.FileAppender????
#\u8BBE\u7F6E\u65E5\u5FD7\u6587\u4EF6\u540Dmy.log????
log4j.appender.R.file=my.log????
#\u6BCF\u6B21\u5728\u6587\u4EF6\u5C3E\u5199\u5165\u65B0\u7684\u65E5\u5FD7\u4FE1\u606F????
log4j.appender.R.Append=true???
#\u65E5\u5FD7\u8F93\u51FA\u4FE1\u606F\u683C\u5F0F\u7C7B\u578B????
log4j.appender.R.layout=org.apache.log4j.PatternLayout????
#\u65E5\u5FD7\u8F93\u51FA\u4FE1\u606F\u683C\u5F0F\u4E3A \u6362\u884C\u3001\u6362\u884C\u3001\u65E5\u671F\u3001\u4F18\u5148\u7EA7\u3001[\u7C7B\u540D]\u3001\u65E5\u5FD7\u4FE1\u606F\u3001\u6362\u884C????
log4j.appender.R.layout.ConversionPattern=%n%n%d%p[%c]-%m%n???
1#\u5B9A\u4E49DEBUG\u4F18\u5148\u7EA7\uFF0CR\u4E3A\u65E5\u5FD7\u8F93\u51FA\u76EE\u7684\u7684??
log4j.rootLogger=DEBUG, t??
#org.apache.log4j.ConsoleAppender???
log4j.appender.t=org.apache.log4j.FileAppender??
#\u8BBE\u7F6E\u65E5\u5FD7\u6587\u4EF6\u540Dmy.log??
log4j.appender.t.file=my1.log??
#\u6BCF\u6B21\u5728\u6587\u4EF6\u5C3E\u5199\u5165\u65B0\u7684\u65E5\u5FD7\u4FE1\u606F??
log4j.appender.t.Append=true?
#\u65E5\u5FD7\u8F93\u51FA\u4FE1\u606F\u683C\u5F0F\u7C7B\u578B??
log4j.appender.t.layout=org.apache.log4j.PatternLayout??
#\u65E5\u5FD7\u8F93\u51FA\u4FE1\u606F\u683C\u5F0F\u4E3A \u6362\u884C\u3001\u6362\u884C\u3001\u65E5\u671F\u3001\u4F18\u5148\u7EA7\u3001[\u7C7B\u540D]\u3001\u65E5\u5FD7\u4FE1\u606F\u3001\u6362\u884C??
log4j.appender.t.layout.ConversionPattern=%n%n%d%p[%c]-%m%n

  相关解决方案