前一段公司为客户开发了网格管理系统,由于对远程调试考虑不全面造成程序维护非常困难。
由于公司和客户距离较远遇到问题不可能直接到达现场解决。于是每次只能用远程协助维护程序。但由于网速太慢造成很小一个问题一天都解决不了。
如果程序出现异常,获得异常信息可以很快定位错误位置。所以如何很方便的得到异常信息应该实现考虑。
方法一:
修改tomcat配置,
将Tomcat的控制台输出重定向到文件中
在startup.bat中,把原来
call "%EXECUTABLE%" start %CMD_LINE_ARGS%
替换成
call "%EXECUTABLE%" run %CMD_LINE_ARGS% > D:\Tomcat\logs\console.log
这样e2.printStackTrace();就会把异常输出到D:\Tomcat\logs\console.log中。如有异常可以查看console.log文件
方法二:
用javamail自动发送给维护人员。
可以写一个工具类当发生异常时用mail自动发送给维护人员。
//mail发送类
public class MailSender {
private String from;
private String password;
private String to;
private String host;
private String subject;
private MailSender(){
Properties prop= new Properties();
try {
prop.load(MailSender.class.getClassLoader().getResourceAsStream("mail.properties"));
from=prop.getProperty("from");
to=prop.getProperty("to");
host=prop.getProperty("host");
password=prop.getProperty("password");
subject=prop.getProperty("subject");
} catch (IOException e) {
e.printStackTrace();
}
}
private static final MailSender instance=new MailSender();
public static MailSender getInstance(){
return instance;
}
public void sendEmail(String content) throws AddressException, MessagingException {
if(to==null||"".equals(to.trim())){
return;
}
// 在属性列表中搜索属性
Properties properties = System.getProperties();
// 调用 Hashtable 的方法 put设置smtp服务器的名字
properties.setProperty("mail.smtp.host", host);
// 设置该邮件服务器的证明是唯一的
properties.setProperty("mail.smtp.auth", "true");
// 设置邮件发送协议为smtp
properties.setProperty("mail.transport.protocol", "smtp");
PopupAuthenticator popAuthenticator = new PopupAuthenticator(from,
password);
// 创建session对象,并通过Properties获取邮件服务器、用户名、密码等以及通过Authenticator进行身份验证
Session session = Session.getInstance(properties, popAuthenticator);
// 创建发送消息
MimeMessage message = new MimeMessage(session);
// InternetAddress为信件标上地址,setFrom把地址与消息链接起来
message.setFrom(new InternetAddress(from));
/**
* 地址的3种预定义类型如下: 1.Message.RecipientType.TO(点对点发送)
* 2.Message.RecipientType.CC(抄送) 3.Message.RecipientType.BCC(密送)
*/
// InternetAddress为信件标上地址,setRecipient把地址与消息链接起来
message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
// 设置标题
message.setSubject(subject);
message.setContent(content, "text/plain;charset=gbk");// 如果没有附件,直接处理
Transport transport = session.getTransport("smtp");
transport.connect();
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
}
//异常处理类
public class OutExceptionInfo {
public static synchronized void out(Exception e) {
e.printStackTrace();
// String
// path=OutExceptionInfo.class.getClassLoader().getResource("").getPath();
/*
* String path = URLDecoder.decode(SystemConfig.class.getClassLoader()
* .getResource("").getPath(), "utf-8");
*/
// PrintWriter pw=new PrintWriter(new FileOutputStream(new
// File(path+"/exception.txt"),true));
ByteArrayOutputStream temp = new ByteArrayOutputStream();
PrintWriter pw = new PrintWriter(temp);
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
pw.println("===================" + sf.format(new Date())
+ "==================================");
e.printStackTrace(pw);
pw.flush();
String content = "";
try {
content = temp.toString("UTF-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
content = temp.toString();
}
try {
MailSender.getInstance().sendEmail(content);
} catch (AddressException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (MessagingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
pw.close();
}
}
使用时:
try{
..........
catch(Exception e){
OutExceptionInfo.out(e);
}
其他方法:也可以用AOP技术如spring的aop配置,还有struts2的异常配置等。