/**
* Copyright 2011-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.slf4j.Logger;
/**
* Wraps an <code>org.slf4j.Logger</code> instance for Persistit logging. Code
* to enable default logging through Slf4j is shown here: <code><pre>
* Logger log = ... instance of org.slf4j.Logger ...
* Persistit.setPersistitLogger(new Slf4jAdapter(logger));
* </pre></code>
*
* @version 1.1
*/
public class Slf4jAdapter implements PersistitLogger {
private final Logger _logger;
/**
* Constructs a wrapped Slf4j Logger.
*
* @param logger
* A <code>Logger</code> to which Persistit log messages will be
* directed.
*/
public Slf4jAdapter(final Logger logger) {
_logger = logger;
}
/**
* 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
}
}