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.guitools.controlpanel.util; 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/** 039 * Utilities for setting up Control Panel application log. 040 */ 041public class ControlPanelLog 042{ 043 private static String[] packages = { 044 "org.opends" 045 }; 046 private static File logFile; 047 private static FileHandler fileHandler; 048 049 /** 050 * Creates a new file handler for writing log messages to the file indicated by <code>file</code>. 051 * @param file log file to which log messages will be written 052 * @throws IOException if something goes wrong 053 */ 054 public static void initLogFileHandler(File file) throws IOException { 055 if (!isInitialized()) 056 { 057 logFile = file; 058 fileHandler = new FileHandler(logFile.getCanonicalPath()); 059 fileHandler.setFormatter(JDKLogging.getFormatter()); 060 boolean initialLogDone = false; 061 for (String root : JDKLogging.getOpendDJLoggingRoots()) 062 { 063 Logger logger = Logger.getLogger(root); 064 if (disableLoggingToConsole()) 065 { 066 logger.setUseParentHandlers(false); // disable logging to console 067 } 068 logger.addHandler(fileHandler); 069 if (!initialLogDone) { 070 logger.info(getInitialLogRecord()); 071 initialLogDone = true; 072 } 073 } 074 } 075 } 076 077 /** 078 * Writes messages under a given package in the file handler defined when calling initLogFileHandler. 079 * Note that initLogFileHandler should be called before calling this method. 080 * @param packageName the package name. 081 * @throws IOException if something goes wrong 082 */ 083 public static void initPackage(String packageName) throws IOException { 084 Logger logger = Logger.getLogger(packageName); 085 if (disableLoggingToConsole()) 086 { 087 logger.setUseParentHandlers(false); // disable logging to console 088 } 089 logger.addHandler(fileHandler); 090 logger.info(getInitialLogRecord()); 091 } 092 093 /** 094 * Gets the name of the log file. 095 * @return File representing the log file 096 */ 097 public static File getLogFile() { 098 return logFile; 099 } 100 101 /** 102 * Indicates whether or not the log file has been initialized. 103 * @return true when the log file has been initialized 104 */ 105 public static boolean isInitialized() { 106 return logFile != null; 107 } 108 109 /** Closes the log file and deletes it. */ 110 public static void closeAndDeleteLogFile() 111 { 112 if (logFile != null) 113 { 114 fileHandler.close(); 115 logFile.delete(); 116 } 117 } 118 119 private static String getInitialLogRecord() 120 { 121 StringBuilder sb = new StringBuilder() 122 .append("Application launched " + 123 DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG).format(new Date())); 124 return sb.toString(); 125 } 126 127 private static boolean disableLoggingToConsole() 128 { 129 return !"true".equals(System.getenv("OPENDJ_LOG_TO_STDOUT")); 130 } 131} 132