Skip to content

Instantly share code, notes, and snippets.

@opamp
Created October 1, 2012 04:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save opamp/3809509 to your computer and use it in GitHub Desktop.
Save opamp/3809509 to your computer and use it in GitHub Desktop.
Logger
package com.example.Logger;
/* This class is for reading.When you use Logger::getLogData method,Logger class will return Array. its member is this class. */
public class LogData{
protected String message,state,date;
protected int y,m,d,h,min,s;
protected boolean e;
public LogData(){
e = true;
}
public LogData(String linedata){
setLogData(linedata);
}
public void setLogData(String linedata){
e = false;
int[] stc = new int[2];
int[] dtc = new int[2];
stc[0] = linedata.indexOf('[');
stc[1] = linedata.indexOf(']');
dtc[0] = linedata.indexOf('<');
dtc[1] = linedata.indexOf('>');
if(stc[0] >= stc[1] || stc[0]<0){
e = true;
return;
}
if(dtc[0] >= dtc[1] || dtc[0]<0){
e = true;
return;
}
state = linedata.substring(stc[0] + 1,stc[1]);
date = linedata.substring(dtc[0] + 1,dtc[1]);
message = linedata.substring(dtc[1] + 1,linedata.length());
dateStringToNumber(date);
}
private void dateStringToNumber(String dateString){
//yyyy-mm-dd hh:mm:ss
String years = dateString.substring(0,4);
String months = dateString.substring(5,7);
String days = dateString.substring(8,10);
String hours = dateString.substring(11,13);
String minutes = dateString.substring(14,16);
String seconds = dateString.substring(17,19);
y = Integer.valueOf(years).intValue();
m = Integer.valueOf(months).intValue();
d = Integer.valueOf(days).intValue();
h = Integer.valueOf(hours).intValue();
min = Integer.valueOf(minutes).intValue();
s = Integer.valueOf(seconds).intValue();
}
public String getMsg(){
return message;
}
public String getState(){
return state;
}
public String getStringDates(){
return date;
}
public int getYear(){
return y;
}
public int getMonth(){
return m;
}
public int getDay(){
return d;
}
public int getHour(){
return h;
}
public int getMinute(){
return min;
}
public int getSecond(){
return s;
}
public boolean error(){
return e;
}
}
package com.example.Logger;
/*
Logger provide with a function which can read/write this application's logfile.
You have to set path to logfile.
---------------------------------------------------------------------------------
THIS IS CONSTRUCTION OF LOGFILE.
*[STATE]<YYYY-MM-dd HH:mm:ss>msg
---------------------------------------------------------------------------------
LIST OF STATES.
* SUCCESS
* WARNING
* ERROR
* BUG
*/
import java.io.*;
import java.util.Date;
import java.util.TimeZone;
import java.text.SimpleDateFormat;
public class Logger{
private File pathToLogFile;
public Logger(){}
public void setPathToLogfile(String path) throws FileNotFoundException{
File f = new File(path);
if(f.exists() == true && f.isFile() == true){
pathToLogFile = new File(path);
}else{
FileNotFoundException e = new FileNotFoundException(path + " is not found.");
throw e;
}
}
public String getPathToLogFile(){
if(pathToLogFile == null){
return null;
}else{
return pathToLogFile.getAbsolutePath();
}
}
private String getDateForLog(){
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
return sdf.format(d);
}
public void putSuccessfulLog(String msg){
try{
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(pathToLogFile, true), "UTF-8"));
bw.write("[SUCCESS]" + "<" + getDateForLog() + ">" + msg);
bw.newLine();
bw.close();
}catch (FileNotFoundException e){
e.printStackTrace();
}catch (UnsupportedEncodingException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
}
public void putWarningLog(String msg){
try{
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(pathToLogFile, true), "UTF-8"));
bw.write("[WARNING]" + "<" + getDateForLog() + ">" + msg);
bw.newLine();
bw.close();
}catch (FileNotFoundException e){
e.printStackTrace();
}catch (UnsupportedEncodingException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
}
public void putErrorLog(String msg){
try{
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(pathToLogFile, true), "UTF-8"));
bw.write("[ERROR]" + "<" + getDateForLog() + ">" + msg);
bw.newLine();
bw.close();
}catch (FileNotFoundException e){
e.printStackTrace();
}catch (UnsupportedEncodingException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
}
public void putBugLog(String msg){
try{
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(pathToLogFile, true), "UTF-8"));
bw.write("[BUG]" + "<" + getDateForLog() + ">" + msg);
bw.newLine();
bw.close();
}catch (FileNotFoundException e){
e.printStackTrace();
}catch (UnsupportedEncodingException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
}
public boolean clearAllLogs(){
if(pathToLogFile.isFile() == true){
pathToLogFile.delete();
try{
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(pathToLogFile,true),"UTF-8"));
bw.write("[LOGFILE]");
bw.newLine();
bw.close();
}catch (FileNotFoundException e){
e.printStackTrace();
return false;
}catch (UnsupportedEncodingException e){
e.printStackTrace();
return false;
}catch (IOException e){
e.printStackTrace();
return false;
}
return true;
}else{
return false;
}
}
public int getNumberOfLogs(){
int num = 0;
try{
BufferedReader b = new BufferedReader(new InputStreamReader(new FileInputStream(pathToLogFile)));
String str;
while((str = b.readLine())!=null){
num = num + 1;
};
}catch(Exception e){
e.printStackTrace();
}
return num - 1;
}
public LogData[] getLogData() throws RuntimeException{
LogData[] r = new LogData[getNumberOfLogs()];
try{
BufferedReader b = new BufferedReader(new InputStreamReader(new FileInputStream(pathToLogFile)));
String str = b.readLine();
LogData buf;int i = 0;
while((str = b.readLine()) != null){
buf = new LogData(str);
if(buf.error() == true){
throw (new RuntimeException("Log file is broken."));
}else{
r[i] = buf;
}
i += 1;
}
b.close();
}catch(Exception e){
e.printStackTrace();
}
return r;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment