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