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