I wrote a logging class for several of my applications, but I have noticed that it is fairly in-efficient. Can anyone suggest improvements?
What I am looking for:
- Pointers on improving efficiency
- Tips for lowering the code-footprint
- Suggested functional improvements
Please be as nit-picky as possible, I'd like to make this class as robust as possible.
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
class FunctionLogging
{
private String path;
private boolean appendToFile = false;
public FunctionLogging(String file_path, int logLevel, boolean append_value)
{
path = file_path; //used as a placeholder variable for the output path. Can be hard-coded but is preferredly configurable from properties file
appendToFile = append_value; //Set as true when called in each class used in. true appends line to the end of the file, false overwrites file contents
}
public void writeToFile(int logType, String textLine) throws IOException
{
FileInputStream propFile = new FileInputStream("config.ini");
Properties config = new Properties(System.getProperties());
config.load(propFile);
String tempLev = config.getProperty("loggingLevel");
int logLevel = Integer.parseInt(tempLev);
if(logType<=logLevel)
{
//This section is devoted to creating a timestamp of the instance when the output string is created.
Date date = new Date();
SimpleDateFormat format = new SimpleDateFormat();
format = new SimpleDateFormat("dd-MM-yyyy|HH:mm:ss:SSSSSS|");
String timeStamp = format.format(date);
SimpleDateFormat app = new SimpleDateFormat();
app = new SimpleDateFormat("dd-MM-yyyy");
String dateApp = app.format(date);
int id = 0;
String[] messages = new String[4];
messages[0]="FATAL";
messages[1]="WARN";
messages[2]="NOTICE";
messages[3]="DEBUG";
FileWriter logWriter = new FileWriter(path+dateApp+".log", appendToFile);
PrintWriter loggerLines = new PrintWriter(logWriter);
loggerLines.println(timeStamp+id+++"|"+messages[logType]+"|"+textLine);
loggerLines.close();
}
}
}