001/*
002 * CDDL HEADER START
003 *
004 * The contents of this file are subject to the terms of the
005 * Common Development and Distribution License, Version 1.0 only
006 * (the "License").  You may not use this file except in compliance
007 * with the License.
008 *
009 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
010 * or http://forgerock.org/license/CDDLv1.0.html.
011 * See the License for the specific language governing permissions
012 * and limitations under the License.
013 *
014 * When distributing Covered Code, include this CDDL HEADER in each
015 * file and include the License file at legal-notices/CDDLv1_0.txt.
016 * If applicable, add the following below this CDDL HEADER, with the
017 * fields enclosed by brackets "[]" replaced with your own identifying
018 * information:
019 *      Portions Copyright [yyyy] [name of copyright owner]
020 *
021 * CDDL HEADER END
022 *
023 *
024 *      Copyright 2008-2010 Sun Microsystems, Inc.
025 *      Portions Copyright 2011-2015 ForgeRock AS
026 */
027package org.opends.quicksetup;
028
029import java.io.File;
030import java.io.IOException;
031import java.util.logging.FileHandler;
032import java.util.logging.Logger;
033import java.util.Date;
034import java.text.DateFormat;
035
036import org.opends.server.loggers.JDKLogging;
037
038/** Utilities for setting up QuickSetup application log. */
039public class QuickSetupLog
040{
041  private static final String OPENDS_LOGGER_NAME = "org.opends";
042
043  private static File LOG_FILE;
044  private static FileHandler FILE_HANDLER;
045
046  /**
047   * Creates a new file handler for writing log messages to the file indicated by <code>file</code>.
048   *
049   * @param file
050   *          log file to which log messages will be written
051   * @throws IOException
052   *           if something goes wrong
053   */
054  public static void initLogFileHandler(File file) throws IOException
055  {
056    if (!isInitialized())
057    {
058      LOG_FILE = file;
059      FILE_HANDLER = new FileHandler(LOG_FILE.getCanonicalPath());
060      FILE_HANDLER.setFormatter(JDKLogging.getFormatter());
061      Logger logger = Logger.getLogger(OPENDS_LOGGER_NAME);
062      logger.addHandler(FILE_HANDLER);
063      disableConsoleLogging(logger);
064      logger = Logger.getLogger(OPENDS_LOGGER_NAME + ".quicksetup");
065      logger.info(getInitialLogRecord());
066    }
067  }
068
069  /**
070   * Creates a new file handler for writing log messages of a given package to the file indicated by <code>file</code>.
071   *
072   * @param file
073   *          log file to which log messages will be written.
074   * @param packageName
075   *          the name of the package of the classes that generate log messages.
076   * @throws IOException
077   *           if something goes wrong
078   */
079  public static void initLogFileHandler(File file, String packageName) throws IOException
080  {
081    initLogFileHandler(file);
082    final Logger logger = Logger.getLogger(packageName);
083    logger.addHandler(FILE_HANDLER);
084    disableConsoleLogging(logger);
085  }
086
087  /** Prevents messages written to loggers from appearing in the console output. */
088  private static void disableConsoleLogging(final Logger logger)
089  {
090    if (!"true".equals(System.getenv("OPENDJ_LOG_TO_STDOUT")))
091    {
092      logger.setUseParentHandlers(false);
093    }
094  }
095
096  /**
097   * Gets the name of the log file.
098   *
099   * @return File representing the log file
100   */
101  public static File getLogFile()
102  {
103    return LOG_FILE;
104  }
105
106  /**
107   * Indicates whether or not the log file has been initialized.
108   *
109   * @return true when the log file has been initialized
110   */
111  public static boolean isInitialized()
112  {
113    return LOG_FILE != null;
114  }
115
116  /** Closes the log file and deletes it. */
117  public static void closeAndDeleteLogFile()
118  {
119    if (LOG_FILE != null)
120    {
121      FILE_HANDLER.close();
122      LOG_FILE.delete();
123    }
124  }
125
126  private static String getInitialLogRecord()
127  {
128    // Note; currently the logs are not internationalized.
129    return "QuickSetup application launched "
130        + DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG).format(new Date());
131  }
132}