org.snmp4j.log.Log4jLogAdapter Maven / Gradle / Ivy
/*_############################################################################
_##
_## SNMP4J-LOG4J - Log4jLogAdapter.java
_##
_## Copyright (C) 2003-2020 Frank Fock (SNMP4J.org)
_##
_## 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 org.snmp4j.log;
import org.apache.logging.log4j.*;
import java.io.Serializable;
import java.util.Iterator;
import org.apache.logging.log4j.core.Appender;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.Configurator;
import org.apache.logging.log4j.spi.StandardLevel;
/**
* The {@code Log4jLogAdapter} implements a logging adapter for Log4J.
*
* @author Frank Fock
* @version 2.8.2
* @since 1.2.1
*/
public class Log4jLogAdapter implements LogAdapter, Comparable {
private final Logger logger;
/**
* Creates a Log4J log adapter from a Log4J Logger.
*
* @param logger the Log4J Logger to use with this adapter.
* @since 1.2.1
*/
public Log4jLogAdapter(Logger logger) {
this.logger = logger;
}
/**
* Logs a debug message.
*
* @param message the message to log.
*/
public void debug(Serializable message) {
logger.log(Level.DEBUG, message, null);
}
/**
* Logs an error message.
*
* @param message the message to log.
*/
public void error(Serializable message) {
logger.log(Level.ERROR, message, null);
}
/**
* Logs an error message.
*
* @param message the message to log.
* @param throwable the exception that caused to error.
*/
public void error(CharSequence message, Throwable throwable) {
logger.log(Level.ERROR, message, throwable);
}
/**
* Logs an informational message.
*
* @param message the message to log.
*/
public void info(CharSequence message) {
logger.log(Level.INFO, message, null);
}
/**
* Checks whether DEBUG level logging is activated for this log adapter.
*
* @return {@code true} if logging is enabled or {@code false}
* otherwise.
*/
public boolean isDebugEnabled() {
return logger.isDebugEnabled();
}
/**
* Checks whether INFO level logging is activated for this log adapter.
*
* @return {@code true} if logging is enabled or {@code false}
* otherwise.
*/
public boolean isInfoEnabled() {
return logger.isInfoEnabled();
}
/**
* Checks whether WARN level logging is activated for this log adapter.
*
* @return {@code true} if logging is enabled or {@code false}
* otherwise.
*/
public boolean isWarnEnabled() {
return logger.isWarnEnabled();
}
/**
* Logs an warning message.
*
* @param message the message to log.
*/
public void warn(Serializable message) {
logger.log(Level.WARN, message, null);
}
public void fatal(Object message) {
logger.log(Level.FATAL, message, null);
}
public void fatal(CharSequence message, Throwable throwable) {
logger.log(Level.FATAL, message, throwable);
}
public void setLogLevel(LogLevel level) {
Level l;
switch (level.getLevel()) {
case LogLevel.LEVEL_OFF:
l = Level.OFF;
break;
case LogLevel.LEVEL_ALL:
l = Level.ALL;
break;
case LogLevel.LEVEL_TRACE:
l = Level.DEBUG;
break;
case LogLevel.LEVEL_DEBUG:
l = Level.DEBUG;
break;
case LogLevel.LEVEL_INFO:
l = Level.INFO;
break;
case LogLevel.LEVEL_WARN:
l = Level.WARN;
break;
case LogLevel.LEVEL_ERROR:
l = Level.ERROR;
break;
case LogLevel.LEVEL_FATAL:
l = Level.FATAL;
break;
default:
l = null;
}
Configurator.setLevel(logger.getName(), l);
}
/**
* Gets the Log4J logger.
* @return
* the internal Log4J logger.
*/
public Logger getLogger() {
return logger;
}
public String getName() {
return logger.getName();
}
public LogLevel getLogLevel() {
Level l = logger.getLevel();
return toLogLevel(l);
}
private LogLevel toLogLevel(Level l) {
if (l == null) {
return LogLevel.NONE;
}
switch (StandardLevel.getStandardLevel(l.intLevel())) {
case OFF:
return LogLevel.OFF;
case ALL:
return LogLevel.ALL;
case DEBUG:
return LogLevel.DEBUG;
case INFO:
return LogLevel.INFO;
case WARN:
return LogLevel.WARN;
case ERROR:
return LogLevel.ERROR;
case FATAL:
return LogLevel.FATAL;
}
return LogLevel.DEBUG;
}
public int compareTo(Object o) {
return getName().compareTo(((Log4jLogAdapter) o).getName());
}
public LogLevel getEffectiveLogLevel() {
Level l = logger.getLevel();
return toLogLevel(l);
}
@SuppressWarnings("unchecked")
public Iterator getLogHandler() {
return LoggerContext.getContext().getConfiguration().getAppenders().values().iterator();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy