Last active
August 29, 2015 13:59
-
-
Save Ribesg/10454894 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*************************************************************************** | |
* Project file: NPlugins - NCore - Filter.java * | |
* Full Class name: fr.ribesg.bukkit.ncore.common.logging.Filter * | |
* * | |
* Copyright (c) 2012-2014 Ribesg - www.ribesg.fr * | |
* This file is under GPLv3 -> http://www.gnu.org/licenses/gpl-3.0.txt * | |
* Please contact me at ribesg[at]yahoo.fr if you improve this file! * | |
***************************************************************************/ | |
package fr.ribesg.bukkit.ncore.common.logging; | |
/** | |
* Represents a simple Log filter. | |
* | |
* @author Ribesg | |
*/ | |
public interface Filter { | |
/** | |
* Check if this Filter denies a message or not. | |
* | |
* @param message the message to check | |
* | |
* @return true if this Filter prevents this message from being logged, | |
* false otherwise | |
*/ | |
public boolean denies(final String message); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*************************************************************************** | |
* Project file: NPlugins - NCore - FilterManager.java * | |
* Full Class name: fr.ribesg.bukkit.ncore.common.logging.FilterManager * | |
* * | |
* Copyright (c) 2012-2014 Ribesg - www.ribesg.fr * | |
* This file is under GPLv3 -> http://www.gnu.org/licenses/gpl-3.0.txt * | |
* Please contact me at ribesg[at]yahoo.fr if you improve this file! * | |
***************************************************************************/ | |
package fr.ribesg.bukkit.ncore.common.logging; | |
import org.apache.logging.log4j.LogManager; | |
import org.apache.logging.log4j.core.LogEvent; | |
import org.apache.logging.log4j.core.LoggerContext; | |
import org.apache.logging.log4j.core.config.Configuration; | |
import org.apache.logging.log4j.core.config.LoggerConfig; | |
import org.apache.logging.log4j.core.filter.AbstractFilter; | |
import org.apache.logging.log4j.core.filter.RegexFilter; | |
/** | |
* Manages log4j2 filters | |
* | |
* @author Ribesg | |
*/ | |
public class FilterManager { | |
/** | |
* Adds a new RegexFilter to log4j2. | |
* | |
* @param regex a regex String | |
*/ | |
public void addRegexFilter(final String regex) { | |
addFilter(RegexFilter.createFilter(regex, "FALSE", "DENY", "NEUTRAL")); | |
} | |
/** | |
* Adds a new CustomFilter to log4j2. | |
* | |
* @param filter the filter | |
*/ | |
public void addCustomFilter(final Filter filter) { | |
addFilter(new CustomFilter(filter)); | |
} | |
/** | |
* Adds a new log4j Filter to log4j. | |
* | |
* @param log4jFilter a log4j filter | |
*/ | |
private void addFilter(final org.apache.logging.log4j.core.Filter log4jFilter) { | |
final LoggerContext context = (LoggerContext) LogManager.getContext(); | |
final Configuration config = context.getConfiguration(); | |
final LoggerConfig loggerConfig = config.getLoggerConfig(LogManager.getRootLogger().getName()); | |
loggerConfig.addFilter(log4jFilter); | |
context.updateLoggers(); | |
} | |
/** | |
* Represents a Custom log4j2 filter | |
*/ | |
private class CustomFilter extends AbstractFilter { | |
/** | |
* The actual filter | |
*/ | |
private final Filter filter; | |
/** | |
* Builds a CustomFilter from a Filter. | |
* | |
* @param filter a Filter | |
*/ | |
private CustomFilter(final Filter filter) { | |
this.filter = filter; | |
} | |
@Override | |
public Result filter(final LogEvent event) { | |
return filter.denies(event.getMessage().getFormattedMessage()) ? Result.DENY : Result.NEUTRAL; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment