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