Java Reference
In-Depth Information
writeEntry() logs the given string or object collection. How it is logged depends on the
implementation. One possibility is to send the log messages to the console.
ConsoleLogger.java
0 import java.util.*; // for Collection and Iterator
1
2 class ConsoleLogger implements Logger {
3
public synchronized void writeEntry(Collection entry) {
4
for (Iterator line = entry.iterator(); line.hasNext();)
5
System.out.println(line.next());
6
System.out.println();
7
}
8
9
public synchronized void writeEntry(String entry) {
10
System.out.println(entry);
11
System.out.println();
12
}
13 }
ConsoleLogger.java
Another possibility is to write the log messages to a file specified in the constructor, as
in the following:
FileLogger.java
0 import java.io.*; // for PrintWriter and FileWriter
1 import java.util.*; // for Collection and Iterator
2
3 class FileLogger implements Logger {
4
5
PrintWriter out; // Log file
6
7
public FileLogger(String filename) throws IOException {
8
out = new PrintWriter(new FileWriter(filename), true); // Create log file
9
}
10
11
public synchronized void writeEntry(Collection entry) {
12
for (Iterator line = entry.iterator(); line.hasNext();)
13
out.println(line.next());
14
out.println();
15
}
16
 
Search WWH ::




Custom Search