blob: 59f74a9fd9be8c24286eea434081797f95ed7749 [file] [log] [blame]
danielp54e997e2013-02-21 01:54:23 +00001/**
2 *
3 */
4package org.frc971;
5
6import java.io.FileOutputStream;
7import java.io.FileNotFoundException;
8import java.io.PrintWriter;
9
10import java.util.logging.Handler;
11import java.util.logging.LogRecord;
12
13/**
14 * @author daniel
15 * logs data to custom files, using specific formatting.
16 */
17public class LogHandler extends Handler {
18
19 private FileOutputStream ofstream;
20 PrintWriter writer;
21
22 public LogHandler (String filename) throws FileNotFoundException {
23 super();
24
25 if (filename == null || filename == "") {
26 filename = "logfile.log";
27 }
28
29 //check if file exists, and if not, create it
30 ofstream = new FileOutputStream(filename);
31 writer = new PrintWriter(ofstream);
32 setFormatter(new TimeFormatter());
33 }
34
35 /*Required methods*/
36
37 public void publish(LogRecord message) {
38 //record a message
39 if (!isLoggable(message)) {
40 //ensure that this message should be logged by this handler
41 return;
42 }
43 writer.print(getFormatter().format(message)); //Formatter adds trailing \n
44 }
45 public void flush() {
46 writer.flush();
47 }
48 public void close() throws SecurityException {
49 writer.close();
50 }
51}