0N/A/*
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 *
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 *
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 *
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.
0N/A *
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
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.util.logging;
0N/A
0N/Aimport java.io.*;
0N/Aimport java.nio.channels.FileChannel;
0N/Aimport java.nio.channels.FileLock;
0N/Aimport java.security.*;
0N/A
0N/A/**
0N/A * Simple file logging <tt>Handler</tt>.
0N/A * <p>
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 * <p>
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 * <p>
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 * <p>
0N/A * By default the <tt>XMLFormatter</tt> class is used for formatting.
0N/A * <p>
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 * <ul>
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 * (defaults to <tt>java.util.logging.XMLFormatter</tt>)
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 * </ul>
0N/A * <p>
0N/A * <p>
0N/A * A pattern consists of a string that includes the following special
0N/A * components that will be replaced at runtime:
0N/A * <ul>
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 * </ul>
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 * <p>
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 * /var/tmp/java0.log and /var/tmp/java1.log whereas on Windows 95 they
0N/A * would be typically written to C:\TEMP\java0.log and C:\TEMP\java1.log
0N/A * <p>
0N/A * Generation numbers follow the sequence 0, 1, 2, etc.
0N/A * <p>
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 * <p>
0N/A * Thus if three processes were all trying to log to fred%u.%g.txt then
0N/A * they might end up using fred0.0.txt, fred1.0.txt, fred2.0.txt as
0N/A * the first file in their rotating sequences.
0N/A * <p>
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 *
0N/A * @since 1.4
0N/A */
0N/A
0N/Apublic class FileHandler extends StreamHandler {
0N/A private MeteredStream meter;
0N/A private boolean append;
0N/A private int limit; // zero => no limit.
0N/A private int count;
0N/A private String pattern;
0N/A private String lockFileName;
0N/A private FileOutputStream lockStream;
0N/A private File files[];
0N/A private static final int MAX_LOCKS = 100;
3323N/A private static java.util.HashMap<String, String> locks = new java.util.HashMap<>();
0N/A
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 class MeteredStream extends OutputStream {
0N/A OutputStream out;
0N/A int written;
0N/A
0N/A MeteredStream(OutputStream out, int written) {
0N/A this.out = out;
0N/A this.written = written;
0N/A }
0N/A
0N/A public void write(int b) throws IOException {
0N/A out.write(b);
0N/A written++;
0N/A }
0N/A
0N/A public void write(byte buff[]) throws IOException {
0N/A out.write(buff);
0N/A written += buff.length;
0N/A }
0N/A
0N/A public void write(byte buff[], int off, int len) throws IOException {
0N/A out.write(buff,off,len);
0N/A written += len;
0N/A }
0N/A
0N/A public void flush() throws IOException {
0N/A out.flush();
0N/A }
0N/A
0N/A public void close() throws IOException {
0N/A out.close();
0N/A }
0N/A }
0N/A
0N/A private void open(File fname, boolean append) throws IOException {
0N/A int len = 0;
0N/A if (append) {
0N/A len = (int)fname.length();
0N/A }
0N/A FileOutputStream fout = new FileOutputStream(fname.toString(), append);
0N/A BufferedOutputStream bout = new BufferedOutputStream(fout);
0N/A meter = new MeteredStream(bout, len);
0N/A setOutputStream(meter);
0N/A }
0N/A
0N/A // Private method to configure a FileHandler from LogManager
0N/A // properties and/or default values as specified in the class
0N/A // javadoc.
0N/A private void configure() {
0N/A LogManager manager = LogManager.getLogManager();
0N/A
0N/A String cname = getClass().getName();
0N/A
0N/A pattern = manager.getStringProperty(cname + ".pattern", "%h/java%u.log");
0N/A limit = manager.getIntProperty(cname + ".limit", 0);
0N/A if (limit < 0) {
0N/A limit = 0;
0N/A }
0N/A count = manager.getIntProperty(cname + ".count", 1);
0N/A if (count <= 0) {
0N/A count = 1;
0N/A }
0N/A append = manager.getBooleanProperty(cname + ".append", false);
0N/A setLevel(manager.getLevelProperty(cname + ".level", Level.ALL));
0N/A setFilter(manager.getFilterProperty(cname + ".filter", null));
0N/A setFormatter(manager.getFormatterProperty(cname + ".formatter", new XMLFormatter()));
0N/A try {
0N/A setEncoding(manager.getStringProperty(cname +".encoding", null));
0N/A } catch (Exception ex) {
0N/A try {
0N/A setEncoding(null);
0N/A } catch (Exception ex2) {
0N/A // doing a setEncoding with null should always work.
0N/A // assert false;
0N/A }
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
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 * <p>
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 */
0N/A public FileHandler() throws IOException, SecurityException {
5430N/A checkPermission();
0N/A configure();
0N/A openFiles();
0N/A }
0N/A
0N/A /**
0N/A * Initialize a <tt>FileHandler</tt> to write to the given filename.
0N/A * <p>
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 * <p>
0N/A * There is no limit on the amount of data that may be written,
0N/A * so use this with care.
0N/A *
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 */
0N/A public FileHandler(String pattern) throws IOException, SecurityException {
0N/A if (pattern.length() < 1 ) {
0N/A throw new IllegalArgumentException();
0N/A }
5430N/A checkPermission();
0N/A configure();
0N/A this.pattern = pattern;
0N/A this.limit = 0;
0N/A this.count = 1;
0N/A openFiles();
0N/A }
0N/A
0N/A /**
0N/A * Initialize a <tt>FileHandler</tt> to write to the given filename,
0N/A * with optional append.
0N/A * <p>
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 * <p>
0N/A * There is no limit on the amount of data that may be written,
0N/A * so use this with care.
0N/A *
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 */
0N/A public FileHandler(String pattern, boolean append) throws IOException, SecurityException {
0N/A if (pattern.length() < 1 ) {
0N/A throw new IllegalArgumentException();
0N/A }
5430N/A checkPermission();
0N/A configure();
0N/A this.pattern = pattern;
0N/A this.limit = 0;
0N/A this.count = 1;
0N/A this.append = append;
0N/A openFiles();
0N/A }
0N/A
0N/A /**
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 * of count files.
0N/A * <p>
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 * <p>
0N/A * The count must be at least 1.
0N/A *
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 */
0N/A public FileHandler(String pattern, int limit, int count)
0N/A throws IOException, SecurityException {
0N/A if (limit < 0 || count < 1 || pattern.length() < 1) {
0N/A throw new IllegalArgumentException();
0N/A }
5430N/A checkPermission();
0N/A configure();
0N/A this.pattern = pattern;
0N/A this.limit = limit;
0N/A this.count = count;
0N/A openFiles();
0N/A }
0N/A
0N/A /**
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 * <p>
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 * <p>
0N/A * The count must be at least 1.
0N/A *
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 *
0N/A */
0N/A public FileHandler(String pattern, int limit, int count, boolean append)
0N/A throws IOException, SecurityException {
0N/A if (limit < 0 || count < 1 || pattern.length() < 1) {
0N/A throw new IllegalArgumentException();
0N/A }
5430N/A checkPermission();
0N/A configure();
0N/A this.pattern = pattern;
0N/A this.limit = limit;
0N/A this.count = count;
0N/A this.append = append;
0N/A openFiles();
0N/A }
0N/A
0N/A // Private method to open the set of output files, based on the
0N/A // configured instance variables.
0N/A private void openFiles() throws IOException {
0N/A LogManager manager = LogManager.getLogManager();
5430N/A manager.checkPermission();
0N/A if (count < 1) {
0N/A throw new IllegalArgumentException("file count = " + count);
0N/A }
0N/A if (limit < 0) {
0N/A limit = 0;
0N/A }
0N/A
0N/A // We register our own ErrorManager during initialization
0N/A // so we can record exceptions.
0N/A InitializationErrorManager em = new InitializationErrorManager();
0N/A setErrorManager(em);
0N/A
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 int unique = -1;
0N/A for (;;) {
0N/A unique++;
0N/A if (unique > MAX_LOCKS) {
0N/A throw new IOException("Couldn't get lock for " + pattern);
0N/A }
0N/A // Generate a lock file name from the "unique" int.
0N/A lockFileName = generate(pattern, 0, unique).toString() + ".lck";
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 synchronized(locks) {
0N/A if (locks.get(lockFileName) != null) {
0N/A // We already own this lock, for a different FileHandler
0N/A // object. Try again.
0N/A continue;
0N/A }
0N/A FileChannel fc;
0N/A try {
0N/A lockStream = new FileOutputStream(lockFileName);
0N/A fc = lockStream.getChannel();
0N/A } catch (IOException ix) {
0N/A // We got an IOException while trying to open the file.
0N/A // Try the next file.
0N/A continue;
0N/A }
4013N/A boolean available;
0N/A try {
4013N/A available = fc.tryLock() != null;
0N/A // We got the lock OK.
0N/A } catch (IOException ix) {
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 available = true;
0N/A }
4013N/A if (available) {
4013N/A // We got the lock. Remember it.
4013N/A locks.put(lockFileName, lockFileName);
4013N/A break;
4013N/A }
4013N/A
4013N/A // We failed to get the lock. Try next file.
4013N/A fc.close();
0N/A }
0N/A }
0N/A
0N/A files = new File[count];
0N/A for (int i = 0; i < count; i++) {
0N/A files[i] = generate(pattern, i, unique);
0N/A }
0N/A
0N/A // Create the initial log file.
0N/A if (append) {
0N/A open(files[0], true);
0N/A } else {
0N/A rotate();
0N/A }
0N/A
0N/A // Did we detect any exceptions during initialization?
0N/A Exception ex = em.lastException;
0N/A if (ex != null) {
0N/A if (ex instanceof IOException) {
0N/A throw (IOException) ex;
0N/A } else if (ex instanceof SecurityException) {
0N/A throw (SecurityException) ex;
0N/A } else {
0N/A throw new IOException("Exception: " + ex);
0N/A }
0N/A }
0N/A
0N/A // Install the normal default ErrorManager.
0N/A setErrorManager(new ErrorManager());
0N/A }
0N/A
0N/A // Generate a filename from a pattern.
0N/A private File generate(String pattern, int generation, int unique) throws IOException {
0N/A File file = null;
0N/A String word = "";
0N/A int ix = 0;
0N/A boolean sawg = false;
0N/A boolean sawu = false;
0N/A while (ix < pattern.length()) {
0N/A char ch = pattern.charAt(ix);
0N/A ix++;
0N/A char ch2 = 0;
0N/A if (ix < pattern.length()) {
0N/A ch2 = Character.toLowerCase(pattern.charAt(ix));
0N/A }
0N/A if (ch == '/') {
0N/A if (file == null) {
0N/A file = new File(word);
0N/A } else {
0N/A file = new File(file, word);
0N/A }
0N/A word = "";
0N/A continue;
0N/A } else if (ch == '%') {
0N/A if (ch2 == 't') {
0N/A String tmpDir = System.getProperty("java.io.tmpdir");
0N/A if (tmpDir == null) {
0N/A tmpDir = System.getProperty("user.home");
0N/A }
0N/A file = new File(tmpDir);
0N/A ix++;
0N/A word = "";
0N/A continue;
0N/A } else if (ch2 == 'h') {
0N/A file = new File(System.getProperty("user.home"));
0N/A if (isSetUID()) {
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 throw new IOException("can't use %h in set UID program");
0N/A }
0N/A ix++;
0N/A word = "";
0N/A continue;
0N/A } else if (ch2 == 'g') {
0N/A word = word + generation;
0N/A sawg = true;
0N/A ix++;
0N/A continue;
0N/A } else if (ch2 == 'u') {
0N/A word = word + unique;
0N/A sawu = true;
0N/A ix++;
0N/A continue;
0N/A } else if (ch2 == '%') {
0N/A word = word + "%";
0N/A ix++;
0N/A continue;
0N/A }
0N/A }
0N/A word = word + ch;
0N/A }
0N/A if (count > 1 && !sawg) {
0N/A word = word + "." + generation;
0N/A }
0N/A if (unique > 0 && !sawu) {
0N/A word = word + "." + unique;
0N/A }
0N/A if (word.length() > 0) {
0N/A if (file == null) {
0N/A file = new File(word);
0N/A } else {
0N/A file = new File(file, word);
0N/A }
0N/A }
0N/A return file;
0N/A }
0N/A
0N/A // Rotate the set of output files
0N/A private synchronized void rotate() {
0N/A Level oldLevel = getLevel();
0N/A setLevel(Level.OFF);
0N/A
0N/A super.close();
0N/A for (int i = count-2; i >= 0; i--) {
0N/A File f1 = files[i];
0N/A File f2 = files[i+1];
0N/A if (f1.exists()) {
0N/A if (f2.exists()) {
0N/A f2.delete();
0N/A }
0N/A f1.renameTo(f2);
0N/A }
0N/A }
0N/A try {
0N/A open(files[0], false);
0N/A } catch (IOException ix) {
0N/A // We don't want to throw an exception here, but we
0N/A // report the exception to any registered ErrorManager.
0N/A reportError(null, ix, ErrorManager.OPEN_FAILURE);
0N/A
0N/A }
0N/A setLevel(oldLevel);
0N/A }
0N/A
0N/A /**
0N/A * Format and publish a <tt>LogRecord</tt>.
0N/A *
0N/A * @param record description of the log event. A null record is
0N/A * silently ignored and is not published
0N/A */
0N/A public synchronized void publish(LogRecord record) {
0N/A if (!isLoggable(record)) {
0N/A return;
0N/A }
0N/A super.publish(record);
0N/A flush();
0N/A if (limit > 0 && meter.written >= limit) {
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 AccessController.doPrivileged(new PrivilegedAction<Object>() {
0N/A public Object run() {
0N/A rotate();
0N/A return null;
0N/A }
0N/A });
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Close all the files.
0N/A *
0N/A * @exception SecurityException if a security manager exists and if
0N/A * the caller does not have <tt>LoggingPermission("control")</tt>.
0N/A */
0N/A public synchronized void close() throws SecurityException {
0N/A super.close();
0N/A // Unlock any lock file.
0N/A if (lockFileName == null) {
0N/A return;
0N/A }
0N/A try {
0N/A // Closing the lock file's FileOutputStream will close
0N/A // the underlying channel and free any locks.
0N/A lockStream.close();
0N/A } catch (Exception ex) {
0N/A // Problems closing the stream. Punt.
0N/A }
0N/A synchronized(locks) {
0N/A locks.remove(lockFileName);
0N/A }
0N/A new File(lockFileName).delete();
0N/A lockFileName = null;
0N/A lockStream = null;
0N/A }
0N/A
0N/A private static class InitializationErrorManager extends ErrorManager {
0N/A Exception lastException;
0N/A public void error(String msg, Exception ex, int code) {
0N/A lastException = ex;
0N/A }
0N/A }
0N/A
0N/A // Private native method to check if we are in a set UID program.
0N/A private static native boolean isSetUID();
0N/A}