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 2014-2015 ForgeRock AS. 015 */ 016package org.opends.server.loggers; 017 018import java.util.logging.ErrorManager; 019import java.util.logging.Formatter; 020import java.util.logging.Handler; 021import java.util.logging.Level; 022import java.util.logging.LogManager; 023import java.util.logging.LogRecord; 024import java.util.logging.Logger; 025import java.util.logging.SimpleFormatter; 026 027/** 028 * Utility class for java.util.logging support. 029 */ 030public class JDKLogging 031{ 032 /** Root packages that contains all OpenDJ related classes. */ 033 private static final String[] LOGGING_ROOTS = new String[] { "org.opends", "org.forgerock.opendj"}; 034 035 /** 036 * Disable java.util.logging. 037 */ 038 public static void disableLogging() 039 { 040 LogManager.getLogManager().reset(); 041 Logger.getLogger("").setLevel(Level.OFF); 042 } 043 044 /** 045 * Enable JDK logging to stderr at provided level for OpenDJ classes. 046 * 047 * @param level 048 * The level to log. 049 */ 050 public static void enableConsoleLoggingForOpenDJ(Level level) 051 { 052 LogManager.getLogManager().reset(); 053 Handler handler = new OpenDJHandler(); 054 handler.setFormatter(getFormatter()); 055 handler.setLevel(level); 056 for (String loggingRoot : LOGGING_ROOTS) 057 { 058 Logger logger = Logger.getLogger(loggingRoot); 059 logger.setLevel(level); 060 logger.addHandler(handler); 061 } 062 } 063 064 /** 065 * Custom handler to log to either stdout or stderr depending on the log level 066 */ 067 private static final class OpenDJHandler extends Handler 068 { 069 @Override 070 public void publish(LogRecord record) 071 { 072 if (getFormatter() == null) 073 { 074 setFormatter(new SimpleFormatter()); 075 } 076 077 try 078 { 079 String message = getFormatter().format(record); 080 if (record.getLevel().intValue() >= Level.WARNING.intValue()) 081 { 082 System.err.write(message.getBytes()); 083 } 084 else 085 { 086 System.out.write(message.getBytes()); 087 } 088 } 089 catch (Exception exception) 090 { 091 reportError(null, exception, ErrorManager.FORMAT_FAILURE); 092 return; 093 } 094 } 095 096 @Override 097 public void close() throws SecurityException 098 { 099 } 100 101 @Override 102 public void flush() 103 { 104 System.out.flush(); 105 System.err.flush(); 106 } 107 } 108 109 /** 110 * Get a formatter. 111 * 112 * @return a formatter for loggers 113 */ 114 public static Formatter getFormatter() 115 { 116 return new JDKLoggingFormater(); 117 } 118 119 /** 120 * Returns the packages to be used as root for logging. 121 * <p> 122 * This package covers all OpenDJ classes. 123 * 124 * @return the root packages to log 125 */ 126 public static String[] getOpendDJLoggingRoots() { 127 return LOGGING_ROOTS; 128 } 129 130}