Mercurial > hg > ccmieditor
comparison java/src/uk/ac/qmul/eecs/ccmi/utils/InteractionLog.java @ 0:9418ab7b7f3f
Initial import
author | Fiore Martin <fiore@eecs.qmul.ac.uk> |
---|---|
date | Fri, 16 Dec 2011 17:35:51 +0000 |
parents | |
children | 9e67171477bc |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:9418ab7b7f3f |
---|---|
1 /* | |
2 CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool | |
3 | |
4 Copyright (C) 2011 Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/) | |
5 | |
6 This program is free software: you can redistribute it and/or modify | |
7 it under the terms of the GNU General Public License as published by | |
8 the Free Software Foundation, either version 3 of the License, or | |
9 (at your option) any later version. | |
10 | |
11 This program is distributed in the hope that it will be useful, | |
12 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 GNU General Public License for more details. | |
15 | |
16 You should have received a copy of the GNU General Public License | |
17 along with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 */ | |
19 | |
20 package uk.ac.qmul.eecs.ccmi.utils; | |
21 | |
22 import java.io.IOException; | |
23 import java.text.SimpleDateFormat; | |
24 import java.util.Date; | |
25 import java.util.logging.FileHandler; | |
26 import java.util.logging.Formatter; | |
27 import java.util.logging.Level; | |
28 import java.util.logging.LogRecord; | |
29 import java.util.logging.Logger; | |
30 | |
31 /** | |
32 * A logger class using the {@code java.util.logging} package to log all the user's | |
33 * relevant actions. | |
34 */ | |
35 public class InteractionLog { | |
36 public static void enable(String logFileDir) throws IOException{ | |
37 logger.setLevel(Level.FINE); | |
38 logger.setUseParentHandlers(false); | |
39 if(fileHandler == null){ | |
40 fileHandler = new FileHandler(logFileDir+System.getProperty("file.separator")+"interaction%u.log",true); | |
41 fileHandler.setFormatter(new CCmILogFormatter()); | |
42 logger.addHandler(fileHandler); | |
43 } | |
44 | |
45 /* also print the log on the console */ | |
46 java.util.logging.ConsoleHandler ch = new java.util.logging.ConsoleHandler(); | |
47 ch.setLevel(Level.ALL); | |
48 ch.setFormatter(new CCmILogFormatter()); | |
49 logger.addHandler(ch); | |
50 } | |
51 | |
52 public static void disable(){ | |
53 logger.setLevel(Level.OFF); | |
54 } | |
55 | |
56 public static CCmILogFormatter newFormatter(){ | |
57 return new CCmILogFormatter(); | |
58 } | |
59 | |
60 public static void log(String source, String action, String args){ | |
61 StringBuilder builder = new StringBuilder(source); | |
62 builder.append(SEPARATOR) | |
63 .append(action) | |
64 .append(SEPARATOR) | |
65 .append(args); | |
66 | |
67 logger.fine(builder.toString()); | |
68 } | |
69 | |
70 public static void log(String msg){ | |
71 logger.config(msg); | |
72 } | |
73 | |
74 public static class CCmILogFormatter extends Formatter{ | |
75 | |
76 private CCmILogFormatter(){ | |
77 super(); | |
78 } | |
79 | |
80 @Override | |
81 public String format(LogRecord record) { | |
82 StringBuilder builder = new StringBuilder(); | |
83 if(record.getLevel() == Level.CONFIG){ | |
84 SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss"); | |
85 builder.append("--- ") | |
86 .append(dateFormat.format(new Date(record.getMillis()))) | |
87 .append(" - ") | |
88 .append(record.getMessage()) | |
89 .append(" ---") | |
90 .append(NEW_LINE); | |
91 }else if(record.getLevel() == Level.FINE){ | |
92 SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss.SSS"); | |
93 builder.append(dateFormat.format(new Date(record.getMillis()))) | |
94 .append(SEPARATOR) | |
95 .append(record.getMessage()) | |
96 .append(NEW_LINE); | |
97 } | |
98 | |
99 return builder.toString(); | |
100 } | |
101 } | |
102 | |
103 public static void dispose(){ | |
104 if(fileHandler != null) | |
105 fileHandler.close(); | |
106 } | |
107 | |
108 private static Logger logger = Logger.getLogger("interaction"); | |
109 private static FileHandler fileHandler; | |
110 private static char SEPARATOR = ','; | |
111 private final static String NEW_LINE = System.getProperty("line.separator"); | |
112 } |