Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Logback: the reliable, generic, fast and flexible logging framework. Copyright (C) 1999-2011, QOS.ch. All rights reserved. This program and the accompanying materials are dual-licensed under either the terms of the Eclipse Public License v1.0 as published by the Eclipse Foundation or (per the licensee's choosing) under the terms of the GNU Lesser General Public License version 2.1 as published by the Free Software Foundation.
 
 package ch.qos.logback.core;
 
 import java.io.File;
 
FileAppender appends log events to a file. For more information about this appender, please refer to the online manual at http://logback.qos.ch/manual/appenders.html#FileAppender

Author(s):
Ceki Gülcü
 
 public class FileAppender<E> extends OutputStreamAppender<E> {

  
Append to or truncate the file? The default value for this variable is true, meaning that by default a FileAppender will append to an existing file and not truncate it.
 
   protected boolean append = true;

  
The name of the active log file.
 
   protected String fileName = null;
 
   private boolean prudent = false;

  
The File property takes a string value which should be the name of the file to append to.
 
   public void setFile(String file) {
     if (file == null) {
        = file;
     } else {
       // Trim spaces from both ends. The users probably does not want
       // trailing spaces in file names.
        = file.trim();
     }
   }

  
Returns the value of the Append property.
 
   public boolean isAppend() {
     return ;
   }

  
This method is used by derived classes to obtain the raw file property. Regular users should not be calling this method.

Returns:
the value of the file property
 
   final public String rawFileProperty() {
     return ;
   }

  
Returns the value of the File property.

This method may be overridden by derived classes.

 
   public String getFile() {
     return ;
   }

  
If the value of File is not null, then openFile(java.lang.String) is called with the values of File and Append properties.
 
   public void start() {
     int errors = 0;
     if (getFile() != null) {
       addInfo("File property is set to [" +  + "]");
 
      if () {
        if (!isAppend()) {
          setAppend(true);
          addWarn("Setting \"Append\" property to true on account of \"Prudent\" mode");
        }
      }
      try {
        openFile(getFile());
      } catch (java.io.IOException e) {
        errors++;
        addError("openFile(" +  + "," +  + ") call failed."e);
      }
    } else {
      errors++;
      addError("\"File\" property not set for appender named [" +  + "].");
    }
    if (errors == 0) {
      super.start();
    }
  }

  

Sets and opens the file where the log output will go. The specified file must be writable.

If there was already an opened file, then the previous file is closed first.

Do not use this method directly. To configure a FileAppender or one of its subclasses, set its properties one by one and then call start().

Parameters:
file_name The path to the log file.
  public void openFile(String file_namethrows IOException {
    synchronized () {
      File file = new File(file_name);
      if (FileUtil.isParentDirectoryCreationRequired(file)) {
        boolean result = FileUtil.createMissingParentDirectories(file);
        if (!result) {
          addError("Failed to create parent directories for ["
              + file.getAbsolutePath() + "]");
        }
      }
      ResilientFileOutputStream resilientFos = new ResilientFileOutputStream(
          file);
      resilientFos.setContext();
      setOutputStream(resilientFos);
    }
  }

  

Returns:
true if in prudent mode
See also:
setPrudent(boolean)
  public boolean isPrudent() {
    return ;
  }

  
When prudent is set to true, file appenders from multiple JVMs can safely write to the same file.

Parameters:
prudent
  public void setPrudent(boolean prudent) {
    this. = prudent;
  }
  public void setAppend(boolean append) {
    this. = append;
  }
  private void safeWrite(E eventthrows IOException {
    FileChannel fileChannel = resilientFOS.getChannel();
    if (fileChannel == null) {
      return;
    }
    FileLock fileLock = null;
    try {
      fileLock = fileChannel.lock();
      long position = fileChannel.position();
      long size = fileChannel.size();
      if (size != position) {
        fileChannel.position(size);
      }
      super.writeOut(event);
    } finally {
      if (fileLock != null) {
        fileLock.release();
      }
    }
  }
  protected void writeOut(E eventthrows IOException {
    if () {
      safeWrite(event);
    } else {
      super.writeOut(event);
    }
  }
New to GrepCode? Check out our FAQ X