HowTos Log Management

Aus Salespoint

(Unterschied zwischen Versionen)
Wechseln zu: Navigation, Suche
(Define a new LogEntryFilter)
(Define a new ProcessLogEntry)
Zeile 92: Zeile 92:
</code>
</code>
-
===Define a new ProcessLogEntry===  
+
===Define a new ProcessLogEntry===
 +
 
 +
'''Description:'''
 +
The ProcessLogEntry is a subclass of LogEntry. The extension consists of the Process being handed over to the constructor and the redefinition of the toString() method, which roughly describes the process that is being logged on by the String "Process \"" + getProcessName() + "\" logged on " + getLogDate() + ".". There is also an additonal public method getProcessName(), which returns the name of the handed over Process.
 +
The reason for this subclass or for subclassing it by yourself might be the need to use the LogEntryFilter provided by the static field SaleProcess.LOGENTRYFILTER_PROCESSES_ONLY. It accepts only instances of ProcessLogEntry. So if you ever want to filter logs that belong directly to a SaleProcess, you have to subclass the ProcessLogEntry and use the LogEntryFilter on it.
 +
 
 +
'''ToDo:'''
 +
 
 +
# Create a subclass the SaleProcess.ProcessLogEntry.
 +
# Add the constructor, call the superclass and hand over the SaleProcess.
 +
# Redefine the toString() method to suit your log description.
 +
 
 +
'''Example Source Code:'''
 +
<code java>
 +
1
 +
public class GiveBackLogEntry extends SaleProcess.ProcessLogEntry
 +
{
 +
    String name;
 +
    String customerId;
 +
    Object date;
 +
    2
 +
    public GiveBackLogEntry(SaleProcess process, String name, String customerId, Object date)
 +
    {
 +
        super(process);
 +
        this.name = name;
 +
        this.customerId = customerId;
 +
        this.date = date;
 +
    }
 +
 +
    3
 +
    public String toString()
 +
    {
 +
        return name +
 +
        " gave back by customer " + customerId +
 +
        " (ID) at turn " + date;
 +
    }
 +
}
 +
</code>
 +
 
===Implement the Interface Loggable===  
===Implement the Interface Loggable===  
===Log the opening and closing of a SalesPoint===  
===Log the opening and closing of a SalesPoint===  
===Log the opening and closing of a log file===  
===Log the opening and closing of a log file===  
===Understand logging===
===Understand logging===

Version vom 22:23, 5. Apr. 2009

Inhaltsverzeichnis

Define a Log

Description: As described in #Understand logging, a Log is the representation of the "log file", which only is a real file, if the specified OutputStream is the recommended FileOutputstream. For a global Log, you only have to define a GlobalOutputStream by calling the static method Log.setGlobalLogOutputStream(OutputStream newOS). For local logging, you have to initialize a new instance of Log, either by using the constructor, the static method Log.createLog(OutputStream newOS) or a LogCreator.

ToDo:

  1. For the initialization find a place at the beginning of your application runtime to make sure, the LogOutputStream exists before any logging starts.
  2. Due to the new FileOutputStream, remember to catch the thrown IOException.
  3. Use the static method setGlobalOutputStream, which will initialize a new Log and a globally reachable OutputStream, enabling logging to it anywhere in your application.

Example Source Code:

1
// within the main method, but may be anywhere before any logging starts
public static void main(String[] args)
{
LogShop logShop = new LogShop();
Shop.setTheShop(logShop);
 
2
// remember to catch the possible IOException of creating a new FileOutputStream
try
{
3
// the static method will initialize a new Log and set the FileOutputStream global
// so it can be used anywhere in the application
Log.setGlobalOutputStream(new FileOutputStream("machine.log", true));
}
catch(IOException ioException)
{
System.err.println("Unable to create log file.");
}
}

Define a new LogEntry

Description: A LogEntry is what is being put into the LogOutputStream when logging an event. The information needed for the Log is provided by the two methods public String toString() and public Date getLogDate(). These are the methods to be redefined in order to suit your event log. By default they return "Object logged: " + getLogDate() and the present system date at the point of logging. In our OpenLogEntry we keep the system date as return of getLogDate, but redefine the toString() method to return the String "Counter opened".

ToDo:

  1. Create a new subclass of LogEntry.
  2. Redefine the toString() method to return a suitable text for the event.
  3. Use the LogEntry to be returned by the method getLogData() of the implementation of Loggable.

Example Source Code:

1
public class OpenLogEntry extends LogEntry
{
 
2
public String toString()
{
return "Counter opened";
}
}

Define a new LogEntryFilter

Description: A LogEntryFilter is used either to enable logging of certain LogEntries only or to display / process certain LogEntries of a Log (which is a representation of the "log file"). To "filter" the LogEntries, set the filter to the LogOutputStream or LogInputStream that is being used. It will check wether the handed over LogEntry suits the condition described in the method accept(LogEntry logEntry) of LogEntryFilter. The easyest way to decide on the acception is by cheking wether a LogEntry is an instance of something or not, but anything that tells LogEntries from will do. In this expample we define a LogEntryFilter that will only accept instances of OpenLogEntry, which is another example, defining a LogEntry for the opening of the a SalesPoint. (Therefore see: #Define a new LogEntry)

ToDo:

  1. Create a new class implementing LogEntryFilter.
  2. Implement accept(LogEntry logEntry) to return true if a le is an instance of OpenLogEntry.
  3. Use the LogEntryFilter with the designated LogInputStream or LogOutputStream.

Example Source Code:

1
public class OpenLogEntryFilter implements LogEntryFilter
{
 
2
public boolean accept(LogEntry logEntry)
{
// that will return true if le is an instance of OpenLogEntry
return (logEntry instanceof OpenLogEntry);
}
}

Define a new ProcessLogEntry

Description: The ProcessLogEntry is a subclass of LogEntry. The extension consists of the Process being handed over to the constructor and the redefinition of the toString() method, which roughly describes the process that is being logged on by the String "Process \"" + getProcessName() + "\" logged on " + getLogDate() + ".". There is also an additonal public method getProcessName(), which returns the name of the handed over Process. The reason for this subclass or for subclassing it by yourself might be the need to use the LogEntryFilter provided by the static field SaleProcess.LOGENTRYFILTER_PROCESSES_ONLY. It accepts only instances of ProcessLogEntry. So if you ever want to filter logs that belong directly to a SaleProcess, you have to subclass the ProcessLogEntry and use the LogEntryFilter on it.

ToDo:

  1. Create a subclass the SaleProcess.ProcessLogEntry.
  2. Add the constructor, call the superclass and hand over the SaleProcess.
  3. Redefine the toString() method to suit your log description.

Example Source Code:

1
public class GiveBackLogEntry extends SaleProcess.ProcessLogEntry
{
String name;
String customerId;
Object date;
2
public GiveBackLogEntry(SaleProcess process, String name, String customerId, Object date)
{
super(process);
this.name = name;
this.customerId = customerId;
this.date = date;
}
 
3
public String toString()
{
return name +
" gave back by customer " + customerId +
" (ID) at turn " + date;
}
}

Implement the Interface Loggable

Log the opening and closing of a SalesPoint

Log the opening and closing of a log file

Understand logging

Persönliche Werkzeuge