hallo,
ich habe ein Beispiel gefunden für Logging als Singleton. Wie kann ich es noch so modifizieren, dass ich meine Ausgaben in einer Textdatei schreibe?
Danke Euch,
seven-12
ich habe ein Beispiel gefunden für Logging als Singleton. Wie kann ich es noch so modifizieren, dass ich meine Ausgaben in einer Textdatei schreibe?
Code:
public class Logger
{
private static Logger self;
//key word PRIVATE prevent to invoke this constructor
private Logger()
{
}
//use this static method to get a instance of Logger
public static Logger getInstance()
{
if (self null)
self new Logger();
return self;
}
//keyword "synchronized" make this method thread safe, only one thread
can invoke this method at one time
public synchronized void debug(String msg)
{
}
public synchronized void info(String msg)
{
}
public synchronized void fatal(String msg)
{
}
}
Code:
public class LoggerTest
{
/**
* @param args
*/
public static void main(String[] args)
{
Logger logger Logger.getInstance();
logger.info("info test");
logger.debug("debug test");
}
}
Danke Euch,
seven-12