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         * maximum number 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        @Override
109        public void log(int level, String application, String message) {
110            if(level<logLevel) return;
111            count++;
112            if(count++>100) {
113                try {
114                    checkFile();
115                } catch (IOException e) {}
116                count=0;
117            }
118            String line=LogUtil.getLine(level,application,message);
119            write(line);
120            
121        }
122        private void write(String str) {
123            try {
124                IOUtil.write(res,str,charset,true);
125            } catch (IOException e) {}
126        }
127        @Override
128        public void info(String application, String message) {
129            log(LEVEL_INFO,application,message);
130        }
131        @Override
132        public void debug(String application, String message) {
133            log(LEVEL_DEBUG,application,message);    
134        }
135        @Override
136        public void warn(String application, String message) {
137            log(LEVEL_WARN,application,message);
138        }
139        @Override
140        public void error(String application, String message) {
141            log(LEVEL_ERROR,application,message);
142        }
143        @Override
144        public void fatal(String application, String message) {
145            log(LEVEL_FATAL,application,message);
146        }
147        /**
148         * @return Returns the resource.
149         */
150        public Resource getResource() {
151            return res;
152        }
153    
154        @Override
155        public int getLogLevel() {
156            return logLevel;
157        }
158    
159        @Override
160        public void setLogLevel(int level) {
161            this.logLevel=level;
162        }
163        
164    
165        @Override
166        public String toString(){
167            return res.getAbsolutePath();
168        }
169        
170        /**
171         * maximum file size of for a log file
172         */
173        public static final long MAX_FILE_SIZE=1024*1024;
174        /**
175             * @return the maxFileSize
176             */
177            public long getMaxFileSize() {
178                    return maxFileSize;
179            }
180    
181            /**
182             * @return the maxFiles
183             */
184            public int getMaxFiles() {
185                    return maxFiles;
186            }
187    }