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