在TOMCAT中如何让日志写入文件?

Avril_Lavigne 2005-06-13 06:54:40
在程序中使用ServletContext的log方法,发现日志只是出现在控制台,怎样让其生成文件出现在logs目录中?
...全文
230 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
java_augur 2005-06-14
  • 打赏
  • 举报
回复
ding--------------->日后参考
tomeandau 2005-06-13
  • 打赏
  • 举报
回复
我的二维数组,想把这个一维数组转换成二维的

Object[] p={tag,name,val,data,dsec,cat},其中:tag,name,val,data,dsec,cat都是String类型的变量,接受String类型的值,我想吧这个转换成二维,为了用在JTable里,JTable table=new JTable(Object[][],object[]),,就是为了把数组p[]转换成二维的,就可以了吧数据直接方在JTable里了。。那位可以把忙啊
yeyi001 2005-06-13
  • 打赏
  • 举报
回复
学习一下,楼上的好强
shuiruhan1981905 2005-06-13
  • 打赏
  • 举报
回复
import java.io.*;
public class Gr_Log{
static String log="";
/*================================================================
*
* 函 数 名:Log
*
* 参  数:
*
* String rows,String Tr_code
*
* 功能描述:
*
* 同步写入日志
*
* 返 回 值:
*
* 抛出异常:
*
* 作  者:
*
================================================================*/
public static synchronized void Log(String rows,String Tr_code) {
String tr_date = "";
GetDate getdate = new GetDate();
String hostdir = System.getProperty("user.dir");
String tmp = "";
//String tpPath = getClass().getResource("").toString();
//System.out.println(hostdir);
if(hostdir.indexOf("bin")== -1){
tmp = "\\bin\\log"+"\\Gr_Log"+getdate.getdate()+".txt";
}else{
tmp = "\\log"+"\\Gr_Log"+getdate.getdate()+".txt";
}
log =hostdir+tmp;
//System.out.println(log);
tr_date = getdate.getdatetime();
try{
RandomAccessFile rf1 = new RandomAccessFile(log,"rw");
//if(rf1.length() > 30000){
// rf1.close();
// File del = new File(log);
// del.delete();
// rf1 = new RandomAccessFile(log,"rw");
//}
rf1.seek(rf1.length());
rf1.writeBytes(new String("时间 : ".getBytes("gb2312"),"iso-8859-1")+tr_date+" ");
rf1.writeBytes(new String("类型 : ".getBytes("gb2312"),"iso-8859-1")+new String(Tr_code.getBytes("gb2312"),"iso-8859-1")+"\n");
rf1.writeBytes(new String(rows.getBytes("gb2312"),"iso-8859-1")+"\n");
rf1.writeBytes("\n");
//rf1.writeBytes("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "+"\n");
rf1.close();
}catch(Exception e){

System.out.println("errorlog.."+e.getMessage());
}
}
public void Read_rows(){
String hostdir = System.getProperty("user.dir");
GetDate getdate = new GetDate();
String tmp = "\\bin\\log"+"\\Gr_Log"+getdate.getdate()+".txt";
String steName=hostdir+tmp;
//String steName =hostdir+"\\Gr_Log.txt";
//String steName =hostdir+"\\Gr_Log.txt";
String record = new String();
RandomAccessFile rf2;
try{
rf2 = new RandomAccessFile(log,"r");
int i = 0;
while ((record = rf2.readLine()) != null) {
System.out.println("Value "+i+":"+record);
}
rf2.close();}catch(Exception e){return;}

}
public static void main(String args[]){
Gr_Log.Log("text","test");
}
}

上面是类,你编译一下就可以用了
下面是使用方法,在你想记录日至的地方加上:
String rows = OpNo+"|"+ysh+"|"+e.getMessage();
Gr_Log.Log(rows,"单证回收失败")

目前我的方法是写字tomcat/bin/log/
log是我建立的文件夹,里面的日至按照日期生成,便于检索,这些你都可以修改,看你想怎么用了

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧