網站日誌解析後並產生可讀檔案

來源:互聯網
上載者:User

標籤:input   catch   stat   jar包   讀檔案   png   jdk7   main   not   


public class ObjectOutTest {
///* 將對象寫入檔案方法 */
//public void write(Object o, String path) {
// try {
// /* 建立存取檔案 */
// FileOutputStream fileStream = new FileOutputStream(path);
// /* 將存取檔案寫入對象 */
// ObjectOutputStream os = new ObjectOutputStream(fileStream);
// /* 寫入對象 */
// os.writeObject(o);
// System.out.println("寫入資料成功");
// /* 關閉ObjectOutputStream */
// os.close();
// } catch (FileNotFoundException e) {
// e.printStackTrace();
// } catch (IOException e) {
// e.printStackTrace();
// }
//}
///* 將對象從檔案中讀出來 */
//public void read(String path) {
// try {
// /* 串連到要讀取的檔案 */
// FileInputStream fileStream = new FileInputStream(path);
// /* 怎樣讀取串連到的檔案 */
// ObjectInputStream os = new ObjectInputStream(fileStream);
// /* 讀取對象 */
// LogObject dog = (LogObject) os.readObject();
// System.out.println("輸出結果:" + dog.getFrom() + " is a "
// + dog.getIp() + ",高:" + dog.getUrl() + ",長:");
// /* 關閉對象 */
// os.close();
// } catch (FileNotFoundException e) {
// e.printStackTrace();
// } catch (IOException e) {
// e.printStackTrace();
// } catch (ClassNotFoundException e) {
// e.printStackTrace();
// }
//}
/* 往檔案寫入字串 */
public void wirteString(String path, String context) {
try {
/* 建立寫入對象 */
FileWriter fileWriter = new FileWriter(path,true);
/* 建立緩衝區 */
BufferedWriter writer = new BufferedWriter(fileWriter);
/* 寫入字串 */
writer.write("\n"+context);
/* 關掉對象 */
writer.close();
System.out.println("寫入字串成功!");
} catch (IOException e) {
e.printStackTrace();
}
}
/* 讀取檔案中的字串 */
public void readString(String path) {
try {
/* 建立讀取對象 */
FileReader fileReader = new FileReader(path);
/* 建立緩衝區 */
BufferedReader reader = new BufferedReader(fileReader);
/* 讀取檔案 */
String line = null;
while ((line = reader.readLine()) != null) {
String s[]=line.split(" ");
String str1="";
for(int i=0;i<s.length;i++){
if(i==0||i==3||i==6||i==10){
if(i==0){
str1+="ip:"+"--"+s[i]+" ";
}
if(i==3){
str1+="訪問時間:"+"--"+s[i]+" ";
}
if(i==6){
str1+="url:"+"--"+s[i]+" ";
}
if(i==10){
if(s[10].contains("baidu.com")){
str1+="站外(百度競價):"+"--"+s[i]+" ";
}else{
str1+="內部:"+"--"+s[i]+" ";
}

}
//內部 百度競價 直接存取 站外

}

//System.out.println(str1);
}
if(s[6].contains(".jpg")||s[6].contains(".png")||s[6].contains(".ico")||s[6].contains("/app/api")||s[6].contains(".gif")||s[6].contains(".css")||s[6].contains(".js")){
}else{
ObjectOutTest qqt = new ObjectOutTest();
Date date=new Date();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
String s1=sdf.format(date);
qqt.wirteString("/home/data/htdocs/web/log/"+s1+".csv", str1);
}

}

/* 關閉對象 */
reader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/* 出函數入口 */
public static void main(String args[]) {
ObjectOutTest qqt = new ObjectOutTest();
Date date=new Date();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
String s=sdf.format(date);
qqt.readString("/home/logsplit/"+s);


}
}

寫完方法產生 jar包,再將jar包放到線上,同時寫一個運行jar包的指令碼。

cd /home/timer/
export JAVA_HOME=/opt/jdk7
export CLASSPATH=.:$CLASSPATH:/home/timer/rizhijiexi.jar:
export PATH=$JAVA_HOME/bin:$PATH

java -Xms256m -Xmx512m com.ObjectOutTest >>/home/logsplit/ObjectOut.log &
將 運行指令檔放在 /opt/shell 下

然後 

運行 crontab -e 再輸入 O  即可插入定時啟動指令碼的命令

網站日誌解析後並產生可讀檔案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.