3909N/A * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved. 0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 0N/A * This code is free software; you can redistribute it and/or modify it 0N/A * under the terms of the GNU General Public License version 2 only, as 2362N/A * published by the Free Software Foundation. Oracle designates this 0N/A * particular file as subject to the "Classpath" exception as provided 2362N/A * by Oracle in the LICENSE file that accompanied this code. 0N/A * This code is distributed in the hope that it will be useful, but WITHOUT 0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 0N/A * version 2 for more details (a copy is included in the LICENSE file that 0N/A * accompanied this code). 0N/A * You should have received a copy of the GNU General Public License version 0N/A * 2 along with this work; if not, write to the Free Software Foundation, 0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2362N/A * or visit www.oracle.com if you need additional information or have any 0N/A * Simple file logging <tt>Handler</tt>. 0N/A * The <tt>FileHandler</tt> can either write to a specified file, 0N/A * or it can write to a rotating set of files. 0N/A * For a rotating set of files, as each file reaches a given size 0N/A * limit, it is closed, rotated out, and a new file opened. 0N/A * Successively older files are named by adding "0", "1", "2", 1664N/A * etc. into the base filename. 0N/A * By default buffering is enabled in the IO libraries but each log 0N/A * record is flushed out when it is complete. 0N/A * By default the <tt>XMLFormatter</tt> class is used for formatting. 0N/A * <b>Configuration:</b> 0N/A * By default each <tt>FileHandler</tt> is initialized using the following 0N/A * <tt>LogManager</tt> configuration properties. If properties are not defined 0N/A * (or have invalid values) then the specified default values are used. 0N/A * <li> java.util.logging.FileHandler.level 0N/A * specifies the default level for the <tt>Handler</tt> 0N/A * (defaults to <tt>Level.ALL</tt>). 0N/A * <li> java.util.logging.FileHandler.filter 0N/A * specifies the name of a <tt>Filter</tt> class to use 0N/A * (defaults to no <tt>Filter</tt>). 0N/A * <li> java.util.logging.FileHandler.formatter 0N/A * specifies the name of a <tt>Formatter</tt> class to use 0N/A * <li> java.util.logging.FileHandler.encoding 0N/A * the name of the character set encoding to use (defaults to 0N/A * the default platform encoding). 0N/A * <li> java.util.logging.FileHandler.limit 0N/A * specifies an approximate maximum amount to write (in bytes) 0N/A * to any one file. If this is zero, then there is no limit. 0N/A * (Defaults to no limit). 0N/A * <li> java.util.logging.FileHandler.count 0N/A * specifies how many output files to cycle through (defaults to 1). 0N/A * <li> java.util.logging.FileHandler.pattern 0N/A * specifies a pattern for generating the output file name. See 0N/A * below for details. (Defaults to "%h/java%u.log"). 0N/A * <li> java.util.logging.FileHandler.append 0N/A * specifies whether the FileHandler should append onto 0N/A * any existing files (defaults to false). 0N/A * A pattern consists of a string that includes the following special 0N/A * components that will be replaced at runtime: 0N/A * <li> "/" the local pathname separator 0N/A * <li> "%t" the system temporary directory 0N/A * <li> "%h" the value of the "user.home" system property 0N/A * <li> "%g" the generation number to distinguish rotated logs 0N/A * <li> "%u" a unique number to resolve conflicts 0N/A * <li> "%%" translates to a single percent sign "%" 0N/A * If no "%g" field has been specified and the file count is greater 0N/A * than one, then the generation number will be added to the end of 0N/A * the generated filename, after a dot. 0N/A * Thus for example a pattern of "%t/java%g.log" with a count of 2 0N/A * would typically cause log files to be written on Solaris to 0N/A * would be typically written to C:\TEMP\java0.log and C:\TEMP\java1.log 0N/A * Generation numbers follow the sequence 0, 1, 2, etc. 0N/A * Normally the "%u" unique field is set to 0. However, if the <tt>FileHandler</tt> 0N/A * tries to open the filename and finds the file is currently in use by 0N/A * another process it will increment the unique number field and try 0N/A * again. This will be repeated until <tt>FileHandler</tt> finds a file name that 0N/A * is not currently in use. If there is a conflict and no "%u" field has 0N/A * been specified, it will be added at the end of the filename after a dot. 0N/A * (This will be after any automatically added generation number.) 0N/A * Thus if three processes were all trying to log to fred%u.%g.txt then 0N/A * the first file in their rotating sequences. 0N/A * Note that the use of unique ids to avoid conflicts is only guaranteed 0N/A * to work reliably when using a local disk file system. 0N/A // A metered stream is a subclass of OutputStream that 0N/A // (a) forwards all its output to a target stream 0N/A // (b) keeps track of how many bytes have been written 0N/A // Private method to configure a FileHandler from LogManager 0N/A // properties and/or default values as specified in the class 0N/A // doing a setEncoding with null should always work. 0N/A * Construct a default <tt>FileHandler</tt>. This will be configured 0N/A * entirely from <tt>LogManager</tt> properties (or their default values). 0N/A * @exception IOException if there are IO problems opening the files. 0N/A * @exception SecurityException if a security manager exists and if 0N/A * the caller does not have <tt>LoggingPermission("control"))</tt>. 0N/A * @exception NullPointerException if pattern property is an empty String. 0N/A * Initialize a <tt>FileHandler</tt> to write to the given filename. 0N/A * The <tt>FileHandler</tt> is configured based on <tt>LogManager</tt> 0N/A * properties (or their default values) except that the given pattern 0N/A * argument is used as the filename pattern, the file limit is 0N/A * set to no limit, and the file count is set to one. 0N/A * There is no limit on the amount of data that may be written, 0N/A * so use this with care. 0N/A * @param pattern the name of the output file 0N/A * @exception IOException if there are IO problems opening the files. 0N/A * @exception SecurityException if a security manager exists and if 0N/A * the caller does not have <tt>LoggingPermission("control")</tt>. 0N/A * @exception IllegalArgumentException if pattern is an empty string 0N/A * Initialize a <tt>FileHandler</tt> to write to the given filename, 0N/A * with optional append. 0N/A * The <tt>FileHandler</tt> is configured based on <tt>LogManager</tt> 0N/A * properties (or their default values) except that the given pattern 0N/A * argument is used as the filename pattern, the file limit is 0N/A * set to no limit, the file count is set to one, and the append 0N/A * mode is set to the given <tt>append</tt> argument. 0N/A * There is no limit on the amount of data that may be written, 0N/A * so use this with care. 0N/A * @param pattern the name of the output file 0N/A * @param append specifies append mode 0N/A * @exception IOException if there are IO problems opening the files. 0N/A * @exception SecurityException if a security manager exists and if 0N/A * the caller does not have <tt>LoggingPermission("control")</tt>. 0N/A * @exception IllegalArgumentException if pattern is an empty string 0N/A * Initialize a <tt>FileHandler</tt> to write to a set of files. When 0N/A * (approximately) the given limit has been written to one file, 0N/A * another file will be opened. The output will cycle through a set 0N/A * The <tt>FileHandler</tt> is configured based on <tt>LogManager</tt> 0N/A * properties (or their default values) except that the given pattern 0N/A * argument is used as the filename pattern, the file limit is 0N/A * set to the limit argument, and the file count is set to the 0N/A * given count argument. 0N/A * The count must be at least 1. 0N/A * @param pattern the pattern for naming the output file 0N/A * @param limit the maximum number of bytes to write to any one file 0N/A * @param count the number of files to use 0N/A * @exception IOException if there are IO problems opening the files. 0N/A * @exception SecurityException if a security manager exists and if 0N/A * the caller does not have <tt>LoggingPermission("control")</tt>. 0N/A * @exception IllegalArgumentException if limit < 0, or count < 1. 0N/A * @exception IllegalArgumentException if pattern is an empty string 0N/A * Initialize a <tt>FileHandler</tt> to write to a set of files 0N/A * with optional append. When (approximately) the given limit has 0N/A * been written to one file, another file will be opened. The 0N/A * output will cycle through a set of count files. 0N/A * The <tt>FileHandler</tt> is configured based on <tt>LogManager</tt> 0N/A * properties (or their default values) except that the given pattern 0N/A * argument is used as the filename pattern, the file limit is 0N/A * set to the limit argument, and the file count is set to the 0N/A * given count argument, and the append mode is set to the given 0N/A * <tt>append</tt> argument. 0N/A * The count must be at least 1. 0N/A * @param pattern the pattern for naming the output file 0N/A * @param limit the maximum number of bytes to write to any one file 0N/A * @param count the number of files to use 0N/A * @param append specifies append mode 0N/A * @exception IOException if there are IO problems opening the files. 0N/A * @exception SecurityException if a security manager exists and if 0N/A * the caller does not have <tt>LoggingPermission("control")</tt>. 0N/A * @exception IllegalArgumentException if limit < 0, or count < 1. 0N/A * @exception IllegalArgumentException if pattern is an empty string 0N/A // Private method to open the set of output files, based on the 0N/A // configured instance variables. 0N/A // We register our own ErrorManager during initialization 0N/A // so we can record exceptions. 0N/A // Create a lock file. This grants us exclusive access 0N/A // to our set of output files, as long as we are alive. 0N/A // Generate a lock file name from the "unique" int. 0N/A // Now try to lock that filename. 1664N/A // Because some systems (e.g., Solaris) can only do file locks 0N/A // between processes (and not within a process), we first check 0N/A // if we ourself already have the file locked. 0N/A // We already own this lock, for a different FileHandler 0N/A // object. Try again. 0N/A // We got an IOException while trying to open the file. 0N/A // Try the next file. 0N/A // We got the lock OK. 0N/A // We got an IOException while trying to get the lock. 0N/A // This normally indicates that locking is not supported 0N/A // on the target directory. We have to proceed without 0N/A // getting a lock. Drop through. 4013N/A // We got the lock. Remember it. 4013N/A // We failed to get the lock. Try next file. 0N/A // Create the initial log file. 0N/A // Did we detect any exceptions during initialization? 0N/A // Install the normal default ErrorManager. 0N/A // Generate a filename from a pattern. 0N/A // Ok, we are in a set UID program. For safety's sake 0N/A // we disallow attempts to open files relative to %h. 0N/A // Rotate the set of output files 0N/A // We don't want to throw an exception here, but we 0N/A // report the exception to any registered ErrorManager. 0N/A * Format and publish a <tt>LogRecord</tt>. 0N/A * @param record description of the log event. A null record is 0N/A * silently ignored and is not published 0N/A // We performed access checks in the "init" method to make sure 0N/A // we are only initialized from trusted code. So we assume 0N/A // it is OK to write the target files, even if we are 0N/A // currently being called from untrusted code. 0N/A // So it is safe to raise privilege here. 0N/A * Close all the files. 0N/A * @exception SecurityException if a security manager exists and if 0N/A * the caller does not have <tt>LoggingPermission("control")</tt>. 0N/A // Unlock any lock file. 0N/A // Closing the lock file's FileOutputStream will close 0N/A // the underlying channel and free any locks. 0N/A // Problems closing the stream. Punt. 0N/A // Private native method to check if we are in a set UID program.