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-2015 ForgeRock AS. 016 */ 017package org.opends.quicksetup; 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/** Utilities for setting up QuickSetup application log. */ 029public class QuickSetupLog 030{ 031 private static final String OPENDS_LOGGER_NAME = "org.opends"; 032 033 private static File LOG_FILE; 034 private static FileHandler FILE_HANDLER; 035 036 /** 037 * Creates a new file handler for writing log messages to the file indicated by <code>file</code>. 038 * 039 * @param file 040 * log file to which log messages will be written 041 * @throws IOException 042 * if something goes wrong 043 */ 044 public static void initLogFileHandler(File file) throws IOException 045 { 046 if (!isInitialized()) 047 { 048 LOG_FILE = file; 049 FILE_HANDLER = new FileHandler(LOG_FILE.getCanonicalPath()); 050 FILE_HANDLER.setFormatter(JDKLogging.getFormatter()); 051 Logger logger = Logger.getLogger(OPENDS_LOGGER_NAME); 052 logger.addHandler(FILE_HANDLER); 053 disableConsoleLogging(logger); 054 logger = Logger.getLogger(OPENDS_LOGGER_NAME + ".quicksetup"); 055 logger.info(getInitialLogRecord()); 056 } 057 } 058 059 /** 060 * Creates a new file handler for writing log messages of a given package to the file indicated by <code>file</code>. 061 * 062 * @param file 063 * log file to which log messages will be written. 064 * @param packageName 065 * the name of the package of the classes that generate log messages. 066 * @throws IOException 067 * if something goes wrong 068 */ 069 public static void initLogFileHandler(File file, String packageName) throws IOException 070 { 071 initLogFileHandler(file); 072 final Logger logger = Logger.getLogger(packageName); 073 logger.addHandler(FILE_HANDLER); 074 disableConsoleLogging(logger); 075 } 076 077 /** Prevents messages written to loggers from appearing in the console output. */ 078 private static void disableConsoleLogging(final Logger logger) 079 { 080 if (!"true".equals(System.getenv("OPENDJ_LOG_TO_STDOUT"))) 081 { 082 logger.setUseParentHandlers(false); 083 } 084 } 085 086 /** 087 * Gets the name of the log file. 088 * 089 * @return File representing the log file 090 */ 091 public static File getLogFile() 092 { 093 return LOG_FILE; 094 } 095 096 /** 097 * Indicates whether or not the log file has been initialized. 098 * 099 * @return true when the log file has been initialized 100 */ 101 public static boolean isInitialized() 102 { 103 return LOG_FILE != null; 104 } 105 106 /** Closes the log file and deletes it. */ 107 public static void closeAndDeleteLogFile() 108 { 109 if (LOG_FILE != null) 110 { 111 FILE_HANDLER.close(); 112 LOG_FILE.delete(); 113 } 114 } 115 116 private static String getInitialLogRecord() 117 { 118 // Note; currently the logs are not internationalized. 119 return "QuickSetup application launched " 120 + DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG).format(new Date()); 121 } 122}