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