001 // This file is part of AceWiki.
002 // Copyright 2008-2012, AceWiki developers.
003 //
004 // AceWiki is free software: you can redistribute it and/or modify it under the terms of the GNU
005 // Lesser General Public License as published by the Free Software Foundation, either version 3 of
006 // the License, or (at your option) any later version.
007 //
008 // AceWiki is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
009 // even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
010 // Lesser General Public License for more details.
011 //
012 // You should have received a copy of the GNU Lesser General Public License along with AceWiki. If
013 // not, see http://www.gnu.org/licenses/.
014
015 package ch.uzh.ifi.attempto.base;
016
017 import java.io.DataOutputStream;
018 import java.io.File;
019 import java.io.FileOutputStream;
020 import java.io.IOException;
021 import java.util.Calendar;
022
023 /**
024 * This class is used to log the events on the server.
025 *
026 * @author Tobias Kuhn
027 */
028 // TODO: Use java.util.logging.Logger
029 public class Logger {
030
031 private final String fileName;
032 private String username;
033 private final int sessionID;
034
035 /**
036 * Creates a new logger instance for the given file, user name, and session id.
037 *
038 * @param fileName The name of the log file.
039 * @param username The user name.
040 * @param sessionID The session id.
041 */
042 public Logger(String fileName, String username, int sessionID) {
043 this.fileName = fileName;
044 this.username = username;
045 this.sessionID = sessionID;
046 }
047
048 /**
049 * Creates a new logger instance for the given file and session id.
050 *
051 * @param fileName The name of the log file.
052 * @param sessionID The session id.
053 */
054 public Logger(String fileName, int sessionID) {
055 this(fileName, "", sessionID);
056 }
057
058 /**
059 * Sets the user name.
060 *
061 * @param username The user name.
062 */
063 public void setUsername(String username) {
064 this.username = username;
065 }
066
067 /**
068 * Writes a log entry into the log file of the respective ontology.
069 *
070 * @param type The type of the log entry.
071 * @param text The text of the log entry.
072 */
073 public void log(String type, String text) {
074 Calendar c = Calendar.getInstance();
075 long timestamp = System.currentTimeMillis();
076 c.setTimeInMillis(timestamp);
077 String year = c.get(Calendar.YEAR) + "";
078 String month = makeString(c.get(Calendar.MONTH)+1, 2);
079 String day = makeString(c.get(Calendar.DAY_OF_MONTH), 2);
080 String hour = makeString(c.get(Calendar.HOUR_OF_DAY), 2);
081 String min = makeString(c.get(Calendar.MINUTE), 2);
082 String sec = makeString(c.get(Calendar.SECOND), 2);
083 String millis = makeString(c.get(Calendar.MILLISECOND), 3);
084 String dateTime = year + "-" + month + "-" + day + " " + hour + ":" + min + ":" + sec + "." + millis;
085 String session = makeString(sessionID, 4);
086 String un;
087 if (username == null || username.equals("")) {
088 un = "";
089 } else {
090 un = " '" + username + "'";
091 }
092 String dir = "logs";
093 String fn = fileName;
094 if (fileName.indexOf("/") > -1) {
095 dir = fileName.replaceFirst("(.*)/[^/]*", "$1");
096 fn = fileName.replaceFirst(".*/([^/]*)", "$1");
097 }
098 try {
099 if (!(new File(dir)).exists()) (new File(dir)).mkdir();
100 DataOutputStream out = new DataOutputStream(new FileOutputStream(dir + "/" + fn + ".log", true));
101 out.writeBytes(timestamp + " (" + dateTime + ") [" + session + "]" + un + " [" + type + "] " + text.replaceAll("\\n", "~n") + "\n");
102 out.flush();
103 out.close();
104 } catch (IOException ex) {
105 ex.printStackTrace();
106 }
107 }
108
109 private static String makeString(int value, int size) {
110 String s = value + "";
111 while (s.length() < size) {
112 s = "0" + s;
113 }
114 return s;
115 }
116
117 }