001 package railo.commons.io.log; 002 003 004 import java.io.IOException; 005 006 import railo.commons.io.IOUtil; 007 import railo.commons.io.res.Resource; 008 009 /** 010 * Simple Logger to log data to a file 011 */ 012 public class LogResource implements Log { 013 014 private Resource res; 015 016 017 /** 018 * maximal count of files (history of files) 019 */ 020 public static final int MAX_FILES=10; 021 022 private long maxFileSize; 023 private int maxFiles; 024 private int count=0; 025 private int logLevel; 026 private String charset; 027 028 029 /** 030 * Constructor of the Logger 031 * @param res resource to log to 032 * @param logLevel 033 * @throws IOException 034 */ 035 public LogResource(Resource res, int logLevel, String charset) throws IOException { 036 this(res,MAX_FILE_SIZE,MAX_FILES,logLevel,charset); 037 } 038 039 /** 040 * Constructor of the Logger 041 * @param res resource to log to 042 * @param maxFileSize max file size if file is greater creates a backup file of the actuell file and creates a new one. 043 * @param logLevel 044 * @throws IOException 045 */ 046 public LogResource(Resource res, long maxFileSize, int logLevel, String charset) throws IOException { 047 this(res,maxFileSize,MAX_FILES,logLevel,charset); 048 } 049 050 /** 051 * Constructor of the Logger 052 * @param res resource to log to 053 * @param maxFileSize max file size if file is greater creates a backup file of the actuell file and creates a new one. 054 * @param maxFiles max count of files 055 * @param logLevel 056 * @throws IOException 057 */ 058 public LogResource(Resource res, long maxFileSize, int maxFiles, int logLevel, String charset) throws IOException { 059 this.res=res; 060 this.maxFileSize=maxFileSize; 061 this.maxFiles=maxFiles; 062 this.logLevel=logLevel; 063 this.charset=charset; 064 checkFile(); 065 066 } 067 068 069 /** 070 * check files and creates a new one if to great or not exist 071 * @throws IOException 072 */ 073 private void checkFile() throws IOException { 074 boolean writeHeader=false; 075 // create file 076 if(!res.exists()) { 077 res.createFile(true); 078 writeHeader=true; 079 } 080 else if(res.length()==0) { 081 writeHeader=true; 082 } 083 // creaste new file 084 else if(res.length()>maxFileSize) { 085 Resource parent = res.getParentResource(); 086 String name = res.getName(); 087 088 for(int i=maxFiles;i>0;i--) { 089 090 Resource to=parent.getRealResource(name+"."+i+".bak"); 091 Resource from=parent.getRealResource(name+"."+(i-1)+".bak"); 092 if(from.exists()) { 093 if(to.exists())to.delete(); 094 from.renameTo(to); 095 } 096 } 097 res.renameTo(parent.getRealResource(name+".1.bak")); 098 res=parent.getRealResource(name);//new File(parent,name); 099 res.createNewFile(); 100 writeHeader=true; 101 } 102 if(writeHeader) { 103 writeHeader=false; 104 write(LogUtil.getHeader()); 105 } 106 } 107 108 /** 109 * @see railo.commons.io.log.Log#log(int, java.lang.String, java.lang.String) 110 */ 111 public void log(int level, String application, String message) { 112 if(level<logLevel) return; 113 count++; 114 if(count++>100) { 115 try { 116 checkFile(); 117 } catch (IOException e) {} 118 count=0; 119 } 120 String line=LogUtil.getLine(level,application,message); 121 write(line); 122 123 } 124 private void write(String str) { 125 try { 126 IOUtil.write(res,str,charset,true); 127 } catch (IOException e) {} 128 } 129 /** 130 * @see railo.commons.io.log.Log#info(java.lang.String, java.lang.String) 131 */ 132 public void info(String application, String message) { 133 log(LEVEL_INFO,application,message); 134 } 135 /** 136 * @see railo.commons.io.log.Log#debug(java.lang.String, java.lang.String) 137 */ 138 public void debug(String application, String message) { 139 log(LEVEL_DEBUG,application,message); 140 } 141 /** 142 * @see railo.commons.io.log.Log#warn(java.lang.String, java.lang.String) 143 */ 144 public void warn(String application, String message) { 145 log(LEVEL_WARN,application,message); 146 } 147 /** 148 * @see railo.commons.io.log.Log#error(java.lang.String, java.lang.String) 149 */ 150 public void error(String application, String message) { 151 log(LEVEL_ERROR,application,message); 152 } 153 /** 154 * @see railo.commons.io.log.Log#fatal(java.lang.String, java.lang.String) 155 */ 156 public void fatal(String application, String message) { 157 log(LEVEL_FATAL,application,message); 158 } 159 /** 160 * @return Returns the resource. 161 */ 162 public Resource getResource() { 163 return res; 164 } 165 166 /** 167 * @see railo.commons.io.log.Log#getLogLevel() 168 */ 169 public int getLogLevel() { 170 return logLevel; 171 } 172 173 /** 174 * @see railo.commons.io.log.Log#setLogLevel(int) 175 */ 176 public void setLogLevel(int level) { 177 this.logLevel=level; 178 } 179 180 181 /** 182 * @see java.lang.Object#toString() 183 */ 184 public String toString(){ 185 return res.getAbsolutePath(); 186 } 187 188 /** 189 * maximal file size of for a log file 190 */ 191 public static final long MAX_FILE_SIZE=1024*1024; 192 /** 193 * @return the maxFileSize 194 */ 195 public long getMaxFileSize() { 196 return maxFileSize; 197 } 198 199 /** 200 * @return the maxFiles 201 */ 202 public int getMaxFiles() { 203 return maxFiles; 204 } 205 }