/** * Copyright 2005-2012 Akiban Technologies, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.persistit.logging; import java.util.EnumMap; import java.util.logging.Level; import java.util.logging.Logger; /** * Wraps a JDK 1.4 <code>java.util.logging.Logger</code> for Persistit logging. * Code to enable default logging through the JDK 1.4 Logging API is shown here: * <code><pre> * // refer to any appropriate java.util.logging.Logger, for example * Logger logger = java.util.logging.Logger.getLogger("com.persistit"); //(for example) * Persistit.setPersistitLogger(new JDK14LoggingAdapter(logger)); * </pre></code> * * @version 1.1 */ public class JDK14LoggingAdapter implements PersistitLogger { private final static EnumMap<PersistitLevel, Level> LEVEL_MAP = new EnumMap<PersistitLevel, Level>( PersistitLevel.class); static { LEVEL_MAP.put(PersistitLevel.NONE, Level.OFF); LEVEL_MAP.put(PersistitLevel.TRACE, Level.FINER); LEVEL_MAP.put(PersistitLevel.DEBUG, Level.FINE); LEVEL_MAP.put(PersistitLevel.INFO, Level.INFO); LEVEL_MAP.put(PersistitLevel.WARNING, Level.WARNING); LEVEL_MAP.put(PersistitLevel.ERROR, Level.SEVERE); } private final Logger _logger; /** * Constructs a wrapped JDK 1.4 Logger. * * @param logger * A <code>Logger</code> to which Persistit log messages will be * directed. */ public JDK14LoggingAdapter(final Logger logger) { _logger = logger; } /** * Overrides <code>isLoggable</code> to allow control by the wrapped * <code>Logger</code>. * * @param level * The <code>PersistitLevel</code> */ @Override public boolean isLoggable(final PersistitLevel level) { return _logger.isLoggable(LEVEL_MAP.get(level)); } /** * Writes a log message generated by Persistit to the wrapped * <code>Logger</code>. * * @param level * The <code>PersistitLevel</code> * @param message * The message to write to the log. */ @Override public void log(final PersistitLevel level, final String message) { _logger.log(LEVEL_MAP.get(level), message); } @Override public void open() { // Nothing to do - the log is created and destroyed by the embedding // application } @Override public void close() { // Nothing to do - the log is created and destroyed by the embedding // application } @Override public void flush() { // Nothing to do - log output is managed by the embedding // application } }