/**
* 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 org.apache.commons.logging.Log;
/**
* Wraps an <code>org.apache.commons.logging.Log</code> for Persistit logging.
* Code to enable default logging through Apache commons logging is shown here:
* <code><pre>
* // refer to any appropriate org.apache.commons.logging.Log, for example
* Log log = LogFactory.getLog(getClass());
* Persistit.setPersistitLogger(new ApacheCommonsAdapter(logger));
* </pre></code>
*
* @version 1.1
*/
public class ApacheCommonsLogAdapter implements PersistitLogger {
/**
* The Log object wrapped by this adapter.
*/
private final Log _logger;
/**
* Wraps an Apache commons <code>Log</code> so that Persistit can write to
* it.
*
* @param log
* A <code>Log</code> to which Persistit log messages will be
* directed.
*/
public ApacheCommonsLogAdapter(final Log log) {
_logger = log;
}
/**
* Overrides <code>isLoggable</code> to allow control by the wrapped
* <code>Logger</code>.
*
* @param level
* The <code>level</code>
*/
@Override
public boolean isLoggable(final PersistitLevel level) {
switch (level) {
case NONE:
return false;
case TRACE:
return _logger.isTraceEnabled();
case DEBUG:
return _logger.isDebugEnabled();
case INFO:
return _logger.isInfoEnabled();
case WARNING:
return _logger.isWarnEnabled();
case ERROR:
return _logger.isErrorEnabled();
default:
throw new RuntimeException("base switch");
}
}
/**
* 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) {
switch (level) {
case NONE:
break;
case TRACE:
_logger.trace(message);
break;
case DEBUG:
_logger.debug(message);
break;
case INFO:
_logger.info(message);
break;
case WARNING:
_logger.warn(message);
break;
case ERROR:
_logger.error(message);
break;
default:
throw new RuntimeException("base switch");
}
}
@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
}
}