Class TempLogFile


  • public class TempLogFile
    extends Object
    This class represents a temporary log file which should be usually deleted if linked operation succeeded.
    • Method Detail

      • newTempLogFile

        public static TempLogFile newTempLogFile​(String prefix)
        Creates a new temporary log file.

        Log file will be generated in the OS temporary directory and its name will have the following pattern: prefix-[RANDOM_NUMBER_STRING].log

        Parameters:
        prefix - log file prefix to which log messages will be written.
        Returns:
        a new temporary log file.
      • newTempLogFile

        public static TempLogFile newTempLogFile​(String prefix,
                                                 File directory)
        Creates a new temporary log file in the given directory.

        The directory is created if it does not exist yet. When it is null or cannot be used, the log file goes to the OS temporary directory instead, as with newTempLogFile(String). The name of the file follows the pattern prefix-[RANDOM_NUMBER_STRING].log either way.

        Parameters:
        prefix - log file prefix to which log messages will be written.
        directory - the directory to create the log file in, or null for the OS temporary directory.
        Returns:
        a new temporary log file.
      • getLogFile

        public File getLogFile()
        Gets the name of the log file.
        Returns:
        File representing the log file
      • deleteLogFileAfterSuccess

        public void deleteLogFileAfterSuccess()
        Closes the log file handler and delete the temp log file .

        The publishers installed by the constructor go with it: they are held by the logger singletons, which outlive this object, and once the writer is shut everything they are handed is written to a closed stream and swallowed.

      • isEnabled

        public boolean isEnabled()
        Return true if a temp log file has been created and could be used to log messages.
        Returns:
        true if a temp log file has been created and could be used to log messages.
      • isReadable

        public boolean isReadable()
        Return true if the temp log file is still on disk and can be read.

        Unlike isEnabled() this is about the file, not the logger: something else may have removed the file while the logger still writes to it (see issue #1030), and then there is nothing to hand over to whoever needs the log.

        Returns:
        true if the temp log file is there and readable.
      • readContents

        public String readContents()
                            throws IOException
        Reads the whole temp log file.

        The file is decoded with the default charset of the JVM, which is the one TextWriter.STREAM wrote it with: reader and writer are the same JVM, so a non-ASCII path or base DN in a report comes back as it was logged.

        Returns:
        the contents of the temp log file.
        Throws:
        IOException - if the file cannot be read, for instance because it is no longer there.
      • getPath

        public String getPath()
        Return the absolute path of the temp log file.
        Returns:
        the absolute path of the temp log file.