本文整理匯總了Java中org.apache.logging.log4j.core.LoggerContext.getContext方法的典型用法代碼示例。如果您正苦於以下問題:Java LoggerContext.getContext方法的具體用法?Java LoggerContext.getContext怎麽用?Java LoggerContext.getContext使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.logging.log4j.core.LoggerContext
的用法示例。
在下文中一共展示了LoggerContext.getContext方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: start
import org.apache.logging.log4j.core.LoggerContext; //導入方法依賴的package包/類
@Setup
public void start() {
counter = 0;
if (log == null) {
System.setProperty(
"Log4jContextSelector",
"org.apache.logging.log4j.core.async.AsyncLoggerContextSelector");
LoggerContext context = LoggerContext.getContext();
log = context.getLogger("Log4j2");
log.setAdditive(false);
ArrayList<Appender> list = new ArrayList<Appender>();
list.addAll(log.getAppenders().values());
for (Appender a : list) {
log.removeAppender(a);
}
}
}
示例2: testLogReconfiguration
import org.apache.logging.log4j.core.LoggerContext; //導入方法依賴的package包/類
/**
* This test asserts that after re-configuration of the log engine the total appenders are
* different. The 'comparison' will be done between:
*
* - default config (src/main/resources/log4j2.xml)
* - alternative config (src/test/resources/xml/config-for-LogManagerTest.xml)
*/
@Test (groups = { GROUP_CHANGE_CONFIG }, description = "Validate the numbers of Appenders associated with the LoggerContext")
public void testLogReconfiguration() {
LOGGER.debug("About to test re-configuration of the logger context");
final String pathToAlternativeLogConfig = "src/test/resources/config/log/config-for-LogManagerTest.xml";
LoggerContext loggerContext = LoggerContext.getContext(false);
Configuration configuration = loggerContext.getConfiguration();
Map<String, Appender> appenderMap = configuration.getAppenders();
Assert.assertTrue(appenderMap.size() == 1, "Expected 1 appender");
Assert.assertTrue(appenderMap.containsKey("console"));
LogManager.initializeLogging(pathToAlternativeLogConfig);
// Refresh reference of configuration as it changed
configuration = loggerContext.getConfiguration();
appenderMap = configuration.getAppenders();
Assert.assertTrue(appenderMap.size() == 2, "Expected 2 appenders");
Assert.assertTrue(appenderMap.containsKey("console"));
Assert.assertTrue(appenderMap.containsKey("filterAppender"));
}
示例3: updateLoggingSeverity
import org.apache.logging.log4j.core.LoggerContext; //導入方法依賴的package包/類
public void updateLoggingSeverity(@Observes @ConfigurationUpdate AppConfiguration appConfiguration) {
String loggingLevel = appConfiguration.getLoggingLevel();
if (StringHelper.isEmpty(loggingLevel)) {
return;
}
log.info("Setting loggers level to: '{}'", loggingLevel);
LoggerContext loggerContext = LoggerContext.getContext(false);
if (StringHelper.equalsIgnoreCase("DEFAULT", loggingLevel)) {
log.info("Reloadming log4j configuration");
loggerContext.reconfigure();
return;
}
Level level = Level.toLevel(loggingLevel, Level.INFO);
for (org.apache.logging.log4j.core.Logger logger : loggerContext.getLoggers()) {
String loggerName = logger.getName();
if (loggerName.startsWith("org.xdi.service") || loggerName.startsWith("org.xdi.oxauth") || loggerName.startsWith("org.gluu")) {
logger.setLevel(level);
}
}
}
示例4: setExternalLoggerConfig
import org.apache.logging.log4j.core.LoggerContext; //導入方法依賴的package包/類
private boolean setExternalLoggerConfig() {
log.info("External log configuration: " + appConfiguration.getExternalLoggerConfiguration());
if (StringUtils.isEmpty(appConfiguration.getExternalLoggerConfiguration())) {
return false;
}
File log4jFile = new File(appConfiguration.getExternalLoggerConfiguration());
if (!log4jFile.exists()) {
log.info("External log configuration does not exist.");
return false;
}
LoggerContext loggerContext = LoggerContext.getContext(false);
loggerContext.setConfigLocation(log4jFile.toURI());
loggerContext.reconfigure();
configurationUpdateEvent.select(ConfigurationUpdate.Literal.INSTANCE).fire(this.appConfiguration);
return true;
}
示例5: updateLoggingSeverity
import org.apache.logging.log4j.core.LoggerContext; //導入方法依賴的package包/類
public void updateLoggingSeverity(@Observes @ConfigurationUpdate AppConfiguration appConfiguration) {
String loggingLevel = appConfiguration.getLoggingLevel();
if (StringHelper.isEmpty(loggingLevel)) {
return;
}
log.info("Setting loggers level to: '{}'", loggingLevel);
LoggerContext loggerContext = LoggerContext.getContext(false);
if (StringHelper.equalsIgnoreCase("DEFAULT", loggingLevel)) {
log.info("Reloading log4j configuration");
loggerContext.reconfigure();
return;
}
Level level = Level.toLevel(loggingLevel, Level.INFO);
for (org.apache.logging.log4j.core.Logger logger : loggerContext.getLoggers()) {
String loggerName = logger.getName();
if (loggerName.startsWith("org.xdi.service") || loggerName.startsWith("org.xdi.oxauth") || loggerName.startsWith("org.gluu") || level == Level.OFF) {
logger.setLevel(level);
}
}
}
示例6: setup
import org.apache.logging.log4j.core.LoggerContext; //導入方法依賴的package包/類
@Setup
public void setup() throws Exception {
connectionHSQLDB = getConnectionHSQLDB();
connectionH2 = getConnectionH2();
createTable(connectionHSQLDB, toCreateTableSqlStringHQLDB("fmLogEntry"));
createTable(connectionH2, toCreateTableSqlStringH2("fmLogEntry"));
System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, "log4j2-jdbc-appender.xml");
final LoggerContext context = LoggerContext.getContext(false);
if (context.getConfiguration() instanceof DefaultConfiguration) {
context.reconfigure();
}
StatusLogger.getLogger().reset();
loggerH2 = LogManager.getLogger("H2Logger");
loggerHSQLDB = LogManager.getLogger("HSQLDBLogger");
}
示例7: setUp
import org.apache.logging.log4j.core.LoggerContext; //導入方法依賴的package包/類
@Before
public void setUp() throws Exception {
final File file = new File("target/file-channel");
deleteFiles(file);
/*
* Clear out all other appenders associated with this logger to ensure we're
* only hitting the Avro appender.
*/
final int primaryPort = AvailablePortFinder.getNextAvailable();
final int altPort = AvailablePortFinder.getNextAvailable();
System.setProperty("primaryPort", Integer.toString(primaryPort));
System.setProperty("alternatePort", Integer.toString(altPort));
primary = new EventCollector(primaryPort);
alternate = new EventCollector(altPort);
System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, CONFIG);
ctx = LoggerContext.getContext(false);
ctx.reconfigure();
}
示例8: setUp
import org.apache.logging.log4j.core.LoggerContext; //導入方法依賴的package包/類
@Before
public void setUp() throws Exception {
final File file = new File("target/persistent");
deleteFiles(file);
/*
* Clear out all other appenders associated with this logger to ensure we're
* only hitting the Avro appender.
*/
final int primaryPort = AvailablePortFinder.getNextAvailable();
final int altPort = AvailablePortFinder.getNextAvailable();
System.setProperty("primaryPort", Integer.toString(primaryPort));
System.setProperty("alternatePort", Integer.toString(altPort));
primary = new EventCollector(primaryPort);
alternate = new EventCollector(altPort);
System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, CONFIG);
ctx = LoggerContext.getContext(false);
ctx.reconfigure();
}
示例9: setAllLevels
import org.apache.logging.log4j.core.LoggerContext; //導入方法依賴的package包/類
/**
* Sets the levels of <code>parentLogger</code> and all 'child' loggers to the given <code>level</code>.
* @param parentLogger the parent logger
* @param level the new level
*/
public static void setAllLevels(final String parentLogger, final Level level) {
// 1) get logger config
// 2) if exact match, use it, if not, create it.
// 3) set level on logger config
// 4) update child logger configs with level
// 5) update loggers
final LoggerContext loggerContext = LoggerContext.getContext(false);
final Configuration config = loggerContext.getConfiguration();
boolean set = setLevel(parentLogger, level, config);
for (final Map.Entry<String, LoggerConfig> entry : config.getLoggers().entrySet()) {
if (entry.getKey().startsWith(parentLogger)) {
set |= setLevel(entry.getValue(), level);
}
}
if (set) {
loggerContext.updateLoggers();
}
}
示例10: testAccessManagerWithStrings
import org.apache.logging.log4j.core.LoggerContext; //導入方法依賴的package包/類
/**
* Not a real test, just make sure we can compile access to the typed manager.
*
* @throws IOException
*/
@Test
public void testAccessManagerWithStrings() throws IOException {
try (final LoggerContext ctx = LoggerContext.getContext(false)) {
final Configuration config = ctx.getConfiguration();
final File file = File.createTempFile("RollingFileAppenderAccessTest", ".tmp");
file.deleteOnExit();
final RollingFileAppender appender = RollingFileAppender.createAppender(file.getCanonicalPath(),
"FilePattern", null, "Name", null, null, null, OnStartupTriggeringPolicy.createPolicy(1), null,
null, null, null, null, null, config);
final RollingFileManager manager = appender.getManager();
// Since the RolloverStrategy and TriggeringPolicy are immutable, we could also use generics to type their
// access.
Assert.assertNotNull(manager.getRolloverStrategy());
Assert.assertNotNull(manager.getTriggeringPolicy());
}
}
示例11: testReplacement
import org.apache.logging.log4j.core.LoggerContext; //導入方法依賴的package包/類
@Test
public void testReplacement() {
final LogEvent event = Log4jLogEvent.newBuilder() //
.setLoggerName(EncodingPatternConverterTest.class.getName()) //
.setLevel(Level.DEBUG) //
.setMessage(new SimpleMessage("Test \r\n<div class=\"test\">this</div> & <div class='test'>that</div>"))
.build();
final StringBuilder sb = new StringBuilder();
final LoggerContext ctx = LoggerContext.getContext();
final String[] options = new String[]{"%msg"};
final EncodingPatternConverter converter = EncodingPatternConverter
.newInstance(ctx.getConfiguration(), options);
assertNotNull("Error creating converter", converter);
converter.format(event, sb);
assertEquals(
"Test \\r\\n<div class="test">this</div> & <div class='test'>that</div>",
sb.toString());
}
示例12: testCrlfEscaping
import org.apache.logging.log4j.core.LoggerContext; //導入方法依賴的package包/類
@Test
public void testCrlfEscaping() {
final LogEvent event = Log4jLogEvent.newBuilder() //
.setLoggerName(EncodingPatternConverterTest.class.getName()) //
.setLevel(Level.DEBUG) //
.setMessage(new SimpleMessage("Test \r\n<div class=\"test\">this\r</div> & \n<div class='test'>that</div>"))
.build();
final StringBuilder sb = new StringBuilder();
final LoggerContext ctx = LoggerContext.getContext();
final String[] options = new String[]{"%msg", "CRLF"};
final EncodingPatternConverter converter = EncodingPatternConverter
.newInstance(ctx.getConfiguration(), options);
assertNotNull("Error creating converter", converter);
converter.format(event, sb);
assertEquals(
"Test \\r\\n<div class=\"test\">this\\r</div> & \\n<div class='test'>that</div>",
sb.toString());
}
示例13: main
import org.apache.logging.log4j.core.LoggerContext; //導入方法依賴的package包/類
public static void main(String [] args){
try{
System.out.println(logger1.isDebugEnabled());
System.out.println(logger1.isErrorEnabled());
System.out.println(logger1.isInfoEnabled());
System.out.println(logger2.isDebugEnabled());
System.out.println(logger2.isErrorEnabled());
System.out.println(logger2.isInfoEnabled());
System.out.println(logger3.isDebugEnabled());
System.out.println(logger3.isErrorEnabled());
System.out.println(logger3.isInfoEnabled());
System.out.println(logger4.isDebugEnabled());
System.out.println(logger4.isErrorEnabled());
System.out.println(logger4.isInfoEnabled());
org.apache.logging.log4j.spi.LoggerContext context=LogManager.getContext();
Logger logger = (Logger) LogManager.getLogger();
LoggerConfig config=logger.get();
Map<Property, Boolean> properties=config.getProperties();
System.out.println(config.getName());
LoggerContext ctx=LoggerContext.getContext();
Object appenders=ctx.getConfiguration().getAppenders();
System.out.println(appenders.toString());
}catch(Exception e){
e.printStackTrace();
}finally{
System.exit(0);
}
}
示例14: setupClass
import org.apache.logging.log4j.core.LoggerContext; //導入方法依賴的package包/類
@BeforeClass
public static void setupClass() {
ThreadContext.clearAll();
ConfigurationFactory.setConfigurationFactory(cf);
final LoggerContext ctx = LoggerContext.getContext();
ctx.reconfigure();
}
示例15: addFollowConsoleAppender
import org.apache.logging.log4j.core.LoggerContext; //導入方法依賴的package包/類
private void addFollowConsoleAppender() {
final LoggerContext context = LoggerContext.getContext(false);
final Configuration config = context.getConfiguration();
final PatternLayout layout = PatternLayout.newBuilder().withPattern("%-5level %c{-4} - %msg%n").build();
Appender appender = ConsoleAppender.newBuilder().setFollow(true).setTarget(ConsoleAppender.Target.SYSTEM_OUT)
.withName("ExecutionRecorder").withLayout(layout).build();
appender.start();
config.addAppender(appender);
config.getRootLogger().addAppender(appender, logLevel, null);
}