|
SimpleLogUsage
IntroductionThe following short page will show how to use SimpleLog for your application. Setting up the environmentThe libraries needed in order to use SimpleLog are:
My advice is to use Eclipse as IDE. Copy the downloaded Jar-file in the directory of your project (created with Eclipse) and perform a refresh of the Eclipse Package Explorer. The added Jar-files should appear. Now right-click on them and choose "Build Path" and "Add to Build Path". Everything should work now. Once done the setup stuff...Creating a SimpleLog configuration fileIt is not necessary, but recommended to create a SimpleLog configuration file. The file has to be created in XML format. This is a possible sample-file: <?xml version="1.0" encoding="UTF-8"?> <configurations> <configuration> <outputpath></outputpath> <outputfile>logs.xml</outputfile> <toStdout>true</toStdout> <!--saves the stack-trace if available--> <saveStackTrace>true</saveStackTrace> <!--false: uses names of the form: org.strumpflohner.main.TestClass; true: TestClass--> <useSimpleName>true</useSimpleName> </configuration> </configurations> Loading the configuration fileConsider you created a configuration file in the project directory (same directory from which your application starts) and you named it "logconfig.xml". Then loading of the configuration file is done as follows: try {
Logger.configure("logconfig.xml");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}Example of a usageClass MyObject representing some business class of your app that uses the loggerimport org.simplelog.main.Logger;
public class MyObject {
private Logger log = Logger.getLogger();
public MyObject(){
//do some things...
}
/**
* Test-method to show logging
* @param a
* @param b
* @return
*/
public int performDivision(int a, int b){
int result = 0;
try{
result = a/b;
}catch(ArithmeticException e){
log.error(e, this.getClass());
}
return result;
}
}Main Class from which your application startsThe following class could represent the main class from which your application starts, where you may show a splash-screen, perform initial loadings etc... Here you should also initialize SimpleLog and pass it an eventually created configuration file. import java.io.IOException;
import org.simplelog.main.Logger;
public class TestClass {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
try {
Logger.configure("logconfig.xml");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Logger log = Logger.getLogger();
//do application startup loadings
MyObject obj = new MyObject();
int result = obj.performDivision(4, 0);
}
}SimpleLog automatically creates a log-file (in XML) format and an associated xsl file for formatting the created log-data. |