반응형

------------------------------------------

tail_log_ajax.jsp

------------------------------------------

 

<%@ page contentType="text/html;charset=euc-kr" %>
<%@ page import="java.io.*" %>
<%!
public String toKorean(String param) {
 try{
  //param=new String(param.getBytes("ISO-8859-1"),"EUC-KR");
 }catch(Exception e){
 }
 return param;
}
%>

<%
response.setHeader("Pragma","No-cache"); //HTTP 1.0
response.setDateHeader ("Expires", 0);
response.setHeader ("Cache-Control", "no-cache");
%>

<%
//로그파일명
String log_filename = request.getParameter("log_filename") == null ? "": request.getParameter("log_filename");
long end_point = (request.getParameter("end_point")==null)? 0:Long.parseLong(request.getParameter("end_point")+"");
//로그파일이 있는 디렉토리
String log_dir = request.getParameter("log_dir");
if(log_dir==null||log_dir.length()==0){
 log_dir = "/home/jeus5/logs/vmstest/";
}

StringBuffer sb = new StringBuffer();
RandomAccessFile read_file = new RandomAccessFile(log_dir+"/"+log_filename, "r");
long str_length = read_file.length();
long start_point = (end_point>0)?end_point:str_length;
read_file.seek(start_point);  
String str;
long file_point = start_point;
while((str = read_file.readLine()) != null){
 sb.append(toKorean(str)+"\n");
 file_point = read_file.getFilePointer();
 read_file.seek(file_point);  
}
read_file.close();
String log_data = sb.toString();
out.print(file_point+","+log_data);
%>

 

 

-------------------------------------------

 

--------------------------------------------

tail_log.jsp

--------------------------------------------

 

<%@ page contentType="text/html;charset=euc-kr" %>
<%@ page import="java.io.*" %>
<%!
public String toKorean(String param) {
 try{
  //param=new String(param.getBytes("ISO-8859-1"),"EUC-KR");
 }catch(Exception e){
 }
 return param;
}
%>

<%
//로그파일이 있는 디렉토리
String log_dir = request.getParameter("log_dir");
if(log_dir==null||log_dir.length()==0){
 log_dir = "/home/jeus5/logs/vmstest/";
}

//화면에 나타낼 로그파일 길이
long log_length = 100000;
//로그파일명
String log_filename = request.getParameter("log_filename") == null ? "": request.getParameter("log_filename");

// 디렉토리인 객체를 생성
File myDir = new File(log_dir);
// 디렉토리의 내용 구하기
File[] list_files = myDir.listFiles();

//파일시작위치
long start_point = 0;
//파일끝위치
long end_point = 0;
//파일내용
String log_data = "";
if(!log_filename.equals("")){
 StringBuffer sb = new StringBuffer();
 RandomAccessFile read_file = new RandomAccessFile(log_dir+"/"+log_filename, "r");
 //파일 길이를 구한다.
 long str_length = read_file.length();
 //파일을 읽을 위치를 정한다.
 start_point = (str_length<log_length)?0:(str_length-log_length);
 read_file.seek(start_point);  
 String str;
 end_point = str_length;
 while((str = read_file.readLine()) != null){
  sb.append(toKorean(str)+"\n");
  //파일을 읽고 마지막 위치를 알아낸다.
  end_point = read_file.getFilePointer();
  read_file.seek(end_point);  
 }
 read_file.close();
 //파일내용
 log_data = sb.toString();
}
%>

<HTML>
<HEAD>
<title>로그파일보기</title>
<meta http-equiv="Content-Type" content="text/html; charset=euc-kr">
<style type="text/css">
<!--
a {text-decoration:none; color: black;}
a:link {text-decoration:none; color: black;}
a:visited {text-decoration:none; color: black;}
a:active {text-decoration:none; color: black;}
a:hover {text-decoration:none; color:black;}
body,table,tr,td {  font-family: "굴림"; font-size: 9pt; font-style: normal; color: black;}
-->
</style>
<script>
 //ajax를 위한 함수입니다.
 function getXmlHttpRequest(){
  var xmlhttp = false;
  // Mozilla/Safari
  if (window.xmlhttpuest) {
    xmlhttp = new xmlhttpuest();
  }
  // IE
  else if (window.ActiveXObject) {
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  }
  return xmlhttp;
 }
</script>
</head>

<body>
<table border="0" cellspacing="0" cellpadding="0" width="100%" height="100%">
 <tr height="10%">
  <td align="left" valign="bottom">
   <table border="0" cellspacing="0" cellpadding="0">
   <form name="fileform" method="post" action="tail_log.jsp">
    <tr>
     <td align="left" valign="bottom" height="20">
      로그파일을 tail -f 명령어 처럼 보여줍니다.
     </td>
    </tr>
    <tr>
     <td>
      <select id="log_dir" name="log_dir" onChange="window.Location='tail_log.jsp?log_dir='+this.options[this.selectedIndex].value">
      <option value="/home/jeus5/logs/vmstest/">개발서버      
      </select><script>document.fileform.log_dir.value="<%=log_dir%>";</script>
      <select name="log_filename">
       <option value="">파일선택</option>
       <%
       if (list_files != null) {
        for (int i = 0; i < list_files.length; i++) {
         if(!list_files[i].isDirectory()){
          String filename = list_files[i].getName();
          filename = filename.substring(filename.lastIndexOf("/")+1,filename.length());
       %>
       <option value="<%=filename%>" <%=log_filename.equals(filename)?"selected":""%>><%=filename%></option>
       <%
         }
        }
       }
       %>
      </select>
     </td>
     <td>
      <input type="submit" value="로그파일보기">
     </td>
    </tr>
   </form>
   </table>
  </td>
 </tr>
 <tr height="90%">
  <td>
   <table border="0" cellspacing="0" cellpadding="0" width="100%" height="100%">
   <form name="logform">
    <tr>
     <td><!--textarea id="log_data_area" name="log_data" style="width:100%;height:100%;"></textarea--><xmp id="log_data" name="log_data" style="text-overflow:ellipsis;overflow:auto;border:1 solid #000000;width:1300;height:100%"><%=log_data%>
     </xmp></td>
    </tr>
   </form>
   </table>
  </td>
 </tr>
</table>
<%
if(!log_filename.equals("")){
%>
<script>
 var end_point = <%=end_point%>;
function include_ajax_repeat(){
  var url = 'tail_log_ajax.jsp?log_filename=<%=log_filename%>&end_point='+end_point;
  //alert(url);
  var xmlhttp = getXmlHttpRequest();
  if(url){
   xmlhttp.open("GET", url, true);
   xmlhttp.onreadystatechange = function() {
    if(xmlhttp.readyState == 4) {
     if(xmlhttp.status == 200) {
      var resText = xmlhttp.responseText;
      //마지막 위치
      end_point = resText.substring(0,resText.indexOf(','));
      //새로운 로그 데이타
      var data = resText.substring(resText.indexOf(',')+1,resText.length);
      log_data.innerText = log_data.innerText + data;
      //일정한 양의 로그데이타만 남긴다.
      if(log_data.innerText.length><%=log_length%>) log_data.innerText = log_data.innerText.substring(log_data.innerText.length-<%=log_length%>);
      //스크롤 이동
      document.getElementById('log_data').scrollTop=document.getElementById('log_data').scrollHeight+<%=log_length%>;
      xmlhttp = null;
     } else {
      //alert("Error loading "+url+", "+xmlhttp.status+"("+xmlhttp.statusText+")");
      top.window.title="Error loading "+url+", "+xmlhttp.status+"("+xmlhttp.statusText+")";
      xmlhttp = null;
     }
    }
   }
   xmlhttp.send(null);
  }  
  setTimeout("include_ajax_repeat()", 2000);//msec 마다 서버와 통신함
  return false;
 }
 function include_ajax_repeat_old(){
  var url = 'tail_log_ajax.jsp?log_filename=<%=log_filename%>&end_point='+end_point;
  //alert(url);
  var xmlhttp = getXmlHttpRequest();
  if(url){
   xmlhttp.open("GET", url, true);
   xmlhttp.onreadystatechange = function() {
    if(xmlhttp.readyState == 4) {
     if(xmlhttp.status == 200) {
      var resText = xmlhttp.responseText;
      //마지막 위치
      end_point = resText.substring(0,resText.indexOf(','));
      //새로운 로그 데이타
      var log_data = resText.substring(resText.indexOf(',')+1,resText.length);
      document.logform.log_data.value = document.logform.log_data.value + log_data;
      //일정한 양의 로그데이타만 남긴다.
      if(document.logform.log_data.value.length><%=log_length%>) document.logform.log_data.value = document.logform.log_data.value.substring(document.logform.log_data.value.length-<%=log_length%>);
      //스크롤 이동
      document.getElementById('log_data_area').scrollTop=document.getElementById('log_data_area').scrollHeight+<%=log_length%>;
     } else {
      //alert("Error loading "+url+", "+xmlhttp.status+"("+xmlhttp.statusText+")");
      top.window.document.title="Error loading "+url+", "+xmlhttp.status+"("+xmlhttp.statusText+")";
     }
    }
   }
   xmlhttp.send(null);
  }
  setTimeout("include_ajax_repeat()", 2000);//msec 마다 서버와 통신함
  return false;
 }
 include_ajax_repeat();
</script>
<%
}
%>
</body>
</html>

 

---------------------------------------------------------

 

출처 : http://realcool.egloos.com/4778358

*** 주의사항

Clustering 환경에서는 경로를 반드시 파일로 부터 읽어 와야 한다.

안그럼 무자게 많은 error 가 발생함.

반응형

+ Recent posts