SOURCECODE |
How to... understand Logging
Description:
You can log almost anything that happens in your application, from the first start of it to the final shutdown and between these two, any use of salespoints, databaskets, logons, processes and so on.
Although it is not quite obvious, this is a very useful feature. You may not only create some kind of history file, but also reconstruct interrupted processes, document buys and sells, resolve statistics, and more.
How it works
Logs can be put into any kind of InputStream, of which a FileInputStream is the most suitable. This OutputStream may be both, locally defined or globally, which, too, is the most common way to use logging.
The GlobalOutputStream is being set by the static method Log.setGlobalOutputStream(OutputStream newOS)
, where newOS mostly is new FileOutputStream(String filename, true)
(true to append the Stream to the file and not overwrite it), which creates a new file called filename if it doesn't exist.
As said above, it is also possible to create a simple local Log by using the static method Log.createLog(OutputStream os)
or use the constructor Log(OutputStream os)
. You may even implement the interface LogCreator and use it to create new Logs, but allways remember to specify the Log you want to log an event into if you don't use the GlobalLog. Log by the way may be understood as a representation of the log file.
The interface Loggable
To log an event means to write a LogEntry to the OutputStream. This is done by the Interface Loggable, to be more precise, the method public LogEntry getLogData()
. Known implementing classes are DataBasketEntryImpl and SaleProcess, but you may find the implementation quite poor and should feel free to redefine them.
The most important thing for the event you want to log is, to put the right LogEntry into the OutputStream. This might be by implementing the getLogData method in the class you want to log in correctly, or by creating a seperate implementing class of Loggable and use it with Log.log(Loggable myLoggable)
.
The abstract class LogEntry (which is not really abstract...)
Either way you have to make sure that the myLoggable or the method getLogData returns the right LogEntry. In this class, the method public String toString()
defines what will be seen as the Description on the LogTableForm, which is being used to visualize a LogInputStream (normally the log file). The method getLogDate
specifies the displayed Date on that TableForm. By default, the Date will be the present system time and the String will be "Object logged: " + getLogDate().
Feel free to redefine these two methods and add anything you want to your implementation in order to create sufficient input for the log file, but be aware that you allways have to get a String and a Date into it. It might also be useful to implement new LogEntry classes for each kind of log to be able to work with the LogEntryFilter.
The method public boolean accept(LogEntry le)
of that filter returns true, if le suits your specified condition, which, when implementing new LogEntry classes, might be the result of the class() method.