Simple Logger is a small and simple class that I wrote for a project that couldn’t not be integrated with log4j.
This class simply writes to the system.out but you can enable or disable logging without having to search every single system.out you wrote in your code and comment or decomment it.
You have also two levels of logging: INFO and ERROR
Usage
First of all you need to instantiate a SimpleLogger object: the costructor accepts four parameters:
- boolean - display INFO logs
- boolean - display ERROR logs
- boolean - display a timestamp
- String - a string to be displayed in the output (for example the class name)
SimpleLogger logger = new SimpleLogger(true,true,true,MyClass.class.toString());
and then use the logger object in your code:
logger.info("This is an info message");
logger.error("This is an error message");
Example output:
2912008 11:27:56:809 class com.my.package.MyClass - INFO: This is an info message
2912008 11:27:56:819 class com.my.package.MyClass - ERROR: This is an error message
Copyright
Copyright © 2008 Alessandro Melandri
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
The code
As you can see, the code is very simple. Use as you like it, but remember to share your improvements! :-)
Download SimpleLogger 1.0 (3Kb zip archive)
/*
* (c) 2008 Alessandro Melandri
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* http://www.gnu.org/licenses/gpl.txt
* */
import java.util.Calendar;
import java.util.GregorianCalendar;
/**
* @author Alessandro Melandri - http://www.melandri.net
* @version 1.0
*/
public class SimpleLogger {
private final boolean SHOW_INFO;
private final boolean SHOW_ERROR;
private final boolean SHOW_TIME;
private final String PRE;
/**
* Setup the logger
*
* @param SHOW_INFO Triggers INFO messages
* @param SHOW_ERROR Triggers ERROR messages
* @param SHOW_TIME Triggers timestamp display
* @param PRE String to be shown before the message (Class name, etc...)
* */
public SimpleLogger(boolean showInfo,boolean showError,boolean showTime,String pre){
this.SHOW_INFO = showInfo;
this.SHOW_ERROR = showError;
this.SHOW_TIME = showTime;
this.PRE = pre;
}
/** info implementation for String */
public void info(String message){
writeInfo(message);
}
/** error implementation for String */
public void error(String message){
writeError(message);
}
/** info implementation for Object */
public void info(Object message){
writeInfo(message.toString());
}
/** error implementation for Object */
public void error(Object message){
writeError(message.toString());
}
/** info implementation for float */
public void info(float message){
writeInfo(""+message);
}
/** info implementation for float */
public void error(float message){
writeError(""+message);
}
/** info implementation for int */
public void info(int message){
writeInfo(""+message);
}
/** error implementation for int */
public void error(int message){
writeError(""+message);
}
/** info implementation for double */
public void info(double message){
writeInfo(""+message);
}
/** error implementation for double */
public void error(double message){
writeError(""+message);
}
/** info implementation for boolean */
public void info(boolean message){
writeInfo(""+message);
}
/** error implementation for boolean */
public void error(boolean message){
writeError(""+message);
}
/**
* Writes the INFO message to the system.out
* @param message The text to be written in the system.out
* */
private void writeInfo(Object message){
if (SHOW_INFO){
String time = "";
if (SHOW_TIME)
time = timestamp();
System.out.println(time + " " + PRE +" - INFO: " + message.toString());
}
}
/**
* Writes the ERROR message to the system.out
* @param message The text to be written in the system.out
* */
private void writeError(Object message){
if (SHOW_ERROR){
String time = "";
if (SHOW_TIME)
time = timestamp();
System.out.println(time + " " + PRE +" - ERROR: " + message.toString());
}
}
/**
* Generates the current timestamp
*
* @return Returns a timestamp formatted as "DDMMYYY HH:MM:SS:mmm"
* */
private String timestamp(){
String date = "";
Calendar now = new GregorianCalendar();
date = "" + now.get(Calendar.DAY_OF_MONTH) +
now.get(Calendar.MONTH) +
now.get(Calendar.YEAR) + " " +
now.get(Calendar.HOUR_OF_DAY)+":"+
now.get(Calendar.MINUTE)+":" +
now.get(Calendar.SECOND)+":"+
now.get(Calendar.MILLISECOND);
return date;
}
}
