Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  //
  //  ========================================================================
  //  Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
  //  ------------------------------------------------------------------------
  //  All rights reserved. This program and the accompanying materials
  //  are made available under the terms of the Eclipse Public License v1.0
  //  and Apache License v2.0 which accompanies this distribution.
  //
  //      The Eclipse Public License is available at
 //      http://www.eclipse.org/legal/epl-v10.html
 //
 //      The Apache License v2.0 is available at
 //      http://www.opensource.org/licenses/apache2.0.php
 //
 //  You may elect to redistribute this code under either of these licenses.
 //  ========================================================================
 //
 
 package org.eclipse.jetty.util; 
 
 import java.io.File;
 import java.util.Date;
RolloverFileOutputStream This output stream puts content in a file that is rolled over every 24 hours. The filename must include the string "yyyy_mm_dd", which is replaced with the actual date when creating and rolling over the file. Old files are retained for a number of days before being deleted.
 
 {
     private static Timer __rollover;
     
     final static String YYYY_MM_DD="yyyy_mm_dd";
     final static String ROLLOVER_FILE_DATE_FORMAT = "yyyy_MM_dd";
     final static String ROLLOVER_FILE_BACKUP_FORMAT = "HHmmssSSS";
     final static int ROLLOVER_FILE_RETAIN_DAYS = 31;
 
     private RollTask _rollTask;
     private SimpleDateFormat _fileDateFormat;
 
     private String _filename;
     private File _file;
     private boolean _append;
     private int _retainDays;
     
     /* ------------------------------------------------------------ */
    

Parameters:
filename The filename must include the string "yyyy_mm_dd", which is replaced with the actual date when creating and rolling over the file.
Throws:
java.io.IOException
 
     public RolloverFileOutputStream(String filename)
         throws IOException
     {
         this(filename,true,);
     }
     
     /* ------------------------------------------------------------ */
    

Parameters:
filename The filename must include the string "yyyy_mm_dd", which is replaced with the actual date when creating and rolling over the file.
append If true, existing files will be appended to.
Throws:
java.io.IOException
 
     public RolloverFileOutputStream(String filenameboolean append)
         throws IOException
     {
         this(filename,append,);
     }
 
     /* ------------------------------------------------------------ */
    

Parameters:
filename The filename must include the string "yyyy_mm_dd", which is replaced with the actual date when creating and rolling over the file.
append If true, existing files will be appended to.
retainDays The number of days to retain files before deleting them. 0 to retain forever.
Throws:
java.io.IOException
 
     public RolloverFileOutputStream(String filename,
                                     boolean append,
                                     int retainDays)
        throws IOException
    {
        this(filename,append,retainDays,TimeZone.getDefault());
    }
    /* ------------------------------------------------------------ */
    

Parameters:
filename The filename must include the string "yyyy_mm_dd", which is replaced with the actual date when creating and rolling over the file.
append If true, existing files will be appended to.
retainDays The number of days to retain files before deleting them. 0 to retain forever.
Throws:
java.io.IOException
    public RolloverFileOutputStream(String filename,
                                    boolean append,
                                    int retainDays,
                                    TimeZone zone)
        throws IOException
    {
         this(filename,append,retainDays,zone,null,null);
    }
     
    /* ------------------------------------------------------------ */
    

Parameters:
filename The filename must include the string "yyyy_mm_dd", which is replaced with the actual date when creating and rolling over the file.
append If true, existing files will be appended to.
retainDays The number of days to retain files before deleting them. 0 to retain forever.
dateFormat The format for the date file substitution. The default is "yyyy_MM_dd".
backupFormat The format for the file extension of backup files. The default is "HHmmssSSS".
Throws:
java.io.IOException
    public RolloverFileOutputStream(String filename,
                                    boolean append,
                                    int retainDays,
                                    TimeZone zone,
                                    String dateFormat,
                                    String backupFormat)
        throws IOException
    {
        super(null);
        if (dateFormat==null)
            dateFormat=;
         = new SimpleDateFormat(dateFormat);
        
        if (backupFormat==null)
            backupFormat=;
         = new SimpleDateFormat(backupFormat);
        
        .setTimeZone(zone);
        .setTimeZone(zone);
        
        if (filename!=null)
        {
            filename=filename.trim();
            if (filename.length()==0)
                filename=null;
        }
        if (filename==null)
            throw new IllegalArgumentException("Invalid filename");
        =filename;
        =append;
        =retainDays;
        setFile();
        
        synchronized(RolloverFileOutputStream.class)
        {
            if (==null)
                =new Timer(RolloverFileOutputStream.class.getName(),true);
            
            =new RollTask();
             Calendar now = Calendar.getInstance();
             now.setTimeZone(zone);
             GregorianCalendar midnight =
                 new GregorianCalendar(now.get(.),
                         now.get(.),
                         now.get(.),
                         23,0);
             midnight.setTimeZone(zone);
             midnight.add(.,1);
             .scheduleAtFixedRate(,midnight.getTime(),1000L*60*60*24);
        }
    }
    /* ------------------------------------------------------------ */
    public String getFilename()
    {
        return ;
    }
    
    /* ------------------------------------------------------------ */
    public String getDatedFilename()
    {
        if (==null)
            return null;
        return .toString();
    }
    
    /* ------------------------------------------------------------ */
    public int getRetainDays()
    {
        return ;
    }
    /* ------------------------------------------------------------ */
    private synchronized void setFile()
        throws IOException
    {
        // Check directory
        File file = new File();
        =file.getCanonicalPath();
        file=new File();
        File dirnew File(file.getParent());
        if (!dir.isDirectory() || !dir.canWrite())
            throw new IOException("Cannot write log directory "+dir);
            
        Date now=new Date();
        
        // Is this a rollover file?
        String filename=file.getName();
        int i=filename.toLowerCase(.).indexOf();
        if (i>=0)
        {
            file=new File(dir,
                          filename.substring(0,i)+
                          .format(now)+
                          filename.substring(i+.length()));
        }
            
        if (file.exists()&&!file.canWrite())
            throw new IOException("Cannot write log file "+file);
        // Do we need to change the output stream?
        if (==null || !file.equals())
        {
            // Yep
            =file;
            if (! && file.exists())
                file.renameTo(new File(file.toString()+"."+.format(now)));
            OutputStream oldOut=;
            =new FileOutputStream(file.toString(),);
            if (oldOut!=null)
                oldOut.close();
            //if(log.isDebugEnabled())log.debug("Opened "+_file);
        }
    }
    /* ------------------------------------------------------------ */
    private void removeOldFiles()
    {
        if (>0)
        {
            long now = System.currentTimeMillis();
            
            File filenew File();
            File dir = new File(file.getParent());
            String fn=file.getName();
            int s=fn.toLowerCase(.).indexOf();
            if (s<0)
                return;
            String prefix=fn.substring(0,s);
            String suffix=fn.substring(s+.length());
            String[] logList=dir.list();
            for (int i=0;i<logList.length;i++)
            {
                fn = logList[i];
                if(fn.startsWith(prefix)&&fn.indexOf(suffix,prefix.length())>=0)
                {        
                    File f = new File(dir,fn);
                    long date = f.lastModified();
                    if ( ((now-date)/(1000*60*60*24))>)
                        f.delete();   
                }
            }
        }
    }
    /* ------------------------------------------------------------ */
    @Override
    public void write (byte[] buf)
            throws IOException
     {
            .write (buf);
     }
    /* ------------------------------------------------------------ */
    @Override
    public void write (byte[] bufint offint len)
            throws IOException
     {
            .write (bufofflen);
     }
    
    /* ------------------------------------------------------------ */
    
    @Override
    public void close()
        throws IOException
    {
        synchronized(RolloverFileOutputStream.class)
        {
            try{super.close();}
            finally
            {
                =null;
                =null;
            }
            .cancel(); 
        }
    }
    
    /* ------------------------------------------------------------ */
    /* ------------------------------------------------------------ */
    /* ------------------------------------------------------------ */
    private class RollTask extends TimerTask
    {
        @Override
        public void run()
        {
            try
            {
                RolloverFileOutputStream.this.setFile();
                RolloverFileOutputStream.this.removeOldFiles();
            }
            catch(IOException e)
            {
                // Cannot log this exception to a LOG, as RolloverFOS can be used by logging
                e.printStackTrace();
            }
        }
    }
New to GrepCode? Check out our FAQ X