本文整理匯總了Java中org.apache.logging.log4j.core.Logger.addAppender方法的典型用法代碼示例。如果您正苦於以下問題:Java Logger.addAppender方法的具體用法?Java Logger.addAppender怎麽用?Java Logger.addAppender使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.logging.log4j.core.Logger
的用法示例。
在下文中一共展示了Logger.addAppender方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: onDisable
import org.apache.logging.log4j.core.Logger; //導入方法依賴的package包/類
@Override
public void onDisable() {
//restore the old format
Appender terminalAppender = CommonLogInstaller.getTerminalAppender(TERMINAL_NAME);
Logger rootLogger = ((Logger) LogManager.getRootLogger());
ColorPluginAppender colorPluginAppender = null;
for (Appender value : rootLogger.getAppenders().values()) {
if (value instanceof ColorPluginAppender) {
colorPluginAppender = (ColorPluginAppender) value;
break;
}
}
if (colorPluginAppender != null) {
rootLogger.removeAppender(terminalAppender);
rootLogger.addAppender(colorPluginAppender.getOldAppender());
}
try {
CommonLogInstaller.setLayout(oldLayout, terminalAppender);
} catch (ReflectiveOperationException ex) {
getLogger().log(Level.WARNING, "Cannot revert log format", ex);
}
}
示例2: preInit
import org.apache.logging.log4j.core.Logger; //導入方法依賴的package包/類
@Mod.EventHandler
public void preInit(FMLPreInitializationEvent event)
{
// logging stuff.
if (!FMLForgePlugin.RUNTIME_DEOBF) // not runtime deobf = dev env
{
String packageName = this.getClass().getPackage().getName();
Logger baseLogger = (Logger) LogManager.getLogger(packageName);
ConsoleAppender appender = ConsoleAppender.createAppender(null, null, Target.SYSTEM_OUT.toString(), "console", "true", "false");
baseLogger.addAppender(appender);
baseLogger.setLevel(Level.DEBUG);
appender.start();
// testing levels..
for (Level l : Level.values())
{
baseLogger.log(l, "TESTING {} on level {}", baseLogger.getName(), l);
LogManager.getLogger().log(l, "TESTING {} on level {}", this.getClass().getName(), l);
}
}
Configuration c = new Configuration(event.getSuggestedConfigurationFile());
OUR_SERVER = c.get("main", "globalbloodstain", 1, "Turn on global bloodstains?0=no, 1=yes").getInt() == 0 ? false : true;
c.save();
}
示例3: getLogger
import org.apache.logging.log4j.core.Logger; //導入方法依賴的package包/類
/**
* Gets the test logger and assign the appender to it
*
* @param appender the name of the appender
* @return the logger
*/
private Logger getLogger(String appender) {
final LoggerContext loggerContext = Configurator.initialize(getUniqueMarker(), CONFIG_LOCATION);
Logger logger = loggerContext.getLogger(appender);
logger.getAppenders().clear();
logger.addAppender(loggerContext.getConfiguration().getAppenders().get(appender));
return logger;
}
示例4: call
import org.apache.logging.log4j.core.Logger; //導入方法依賴的package包/類
public Object call() throws Exception {
// Setup for capturing logger messages
Appender mockAppender = mock(Appender.class);
when(mockAppender.getName()).thenReturn("MockAppender");
when(mockAppender.isStarted()).thenReturn(true);
when(mockAppender.isStopped()).thenReturn(false);
Logger logger = (Logger) LogManager.getLogger(ColocationLogger.class);
logger.addAppender(mockAppender);
logger.setLevel(Level.WARN);
loggingEventCaptor = ArgumentCaptor.forClass(LogEvent.class);
// Logger interval may have been hooked by the test, so adjust test delays here
int logInterval = ColocationLogger.getLogInterval();
List<LogEvent> logEvents = Collections.emptyList();
AtomicBoolean isDone = new AtomicBoolean(false);
try {
createPR(PR_REGION_NAME, true);
// Let this thread continue running long enough for the missing region to be logged a
// couple times.
// Child regions do not get created by this thread.
await().atMost(MAX_WAIT, TimeUnit.MILLISECONDS).until(() -> {
verify(mockAppender, times(numExpectedLogMessages))
.append(loggingEventCaptor.capture());
});
// createPR("region2", PR_REGION_NAME, true); // This child region is never created
} finally {
logEvents = loggingEventCaptor.getAllValues();
assertEquals(String.format("Expected %d messages to be logged, got %d.",
numExpectedLogMessages, logEvents.size()), numExpectedLogMessages,
logEvents.size());
String logMsg = logEvents.get(0).getMessage().getFormattedMessage();
logger.removeAppender(mockAppender);
numExpectedLogMessages = 1;
return logMsg;
}
}
示例5: installAppender
import org.apache.logging.log4j.core.Logger; //導入方法依賴的package包/類
public static void installAppender(Appender colorAppender, String terminalName) {
Logger rootLogger = (Logger) LogManager.getRootLogger();
colorAppender.start();
rootLogger.removeAppender(getTerminalAppender(terminalName));
rootLogger.addAppender(colorAppender);
}
示例6: loadLogAppender
import org.apache.logging.log4j.core.Logger; //導入方法依賴的package包/類
private boolean loadLogAppender()
{
Logger log = (Logger)LogManager.getRootLogger();
for(Appender appender : log.getAppenders().values())
{
if(appender instanceof RConsoleAppender)
log.removeAppender(appender);
}
mAppender = new RConsoleAppender(new DefaultConfiguration());
mAppender.start();
log.addAppender(mAppender);
return true;
}
示例7: testStructured
import org.apache.logging.log4j.core.Logger; //導入方法依賴的package包/類
@Test
public void testStructured() throws InterruptedException, IOException {
final Agent[] agents = new Agent[] { Agent.createAgent("localhost",
testPort) };
final FlumeAppender avroAppender = FlumeAppender.createAppender(agents,
null, "false", "Avro", null, "1000", "1000", "1", "1000",
"avro", "false", null, null, null, "ReqCtx_", null, "true",
"1", null, null, null, null);
avroAppender.start();
final Logger eventLogger = (Logger) LogManager.getLogger("EventLogger");
Assert.assertNotNull(eventLogger);
eventLogger.addAppender(avroAppender);
eventLogger.setLevel(Level.ALL);
final StructuredDataMessage msg = new StructuredDataMessage("Transfer",
"Success", "Audit");
msg.put("memo", "This is a memo");
msg.put("acct", "12345");
msg.put("amount", "100.00");
ThreadContext.put("id", UUID.randomUUID().toString());
ThreadContext.put("memo", null);
ThreadContext.put("test", "123");
EventLogger.logEvent(msg);
final Transaction transaction = channel.getTransaction();
transaction.begin();
final Event event = channel.take();
Assert.assertNotNull(event);
Assert.assertTrue("Channel contained event, but not expected message",
getBody(event).endsWith("Success"));
transaction.commit();
transaction.close();
eventSource.stop();
eventLogger.removeAppender(avroAppender);
avroAppender.stop();
}
示例8: testServer
import org.apache.logging.log4j.core.Logger; //導入方法依賴的package包/類
protected void testServer(final String message1, final String message2) throws Exception {
final Filter socketFilter = new ThreadFilter(Filter.Result.NEUTRAL, Filter.Result.DENY);
final Filter serverFilter = new ThreadFilter(Filter.Result.DENY, Filter.Result.NEUTRAL);
final SocketAppender appender = SocketAppender.createAppender("localhost", this.port, this.protocol, "-1", null, "Test", null,
"false", null, socketFilter, null, null);
appender.start();
final ListAppender listApp = new ListAppender("Events", serverFilter, null, false, false);
listApp.start();
final PatternLayout layout = PatternLayout.createLayout("%m %ex%n", null, null, null, null);
final ConsoleAppender console = ConsoleAppender.createAppender(layout, null, "SYSTEM_OUT", "Console", "false", "true");
final Logger serverLogger = ctx.getLogger(this.getClass().getName());
serverLogger.addAppender(console);
serverLogger.setAdditive(false);
// set appender on root and set level to debug
root.addAppender(appender);
root.addAppender(listApp);
root.setAdditive(false);
root.setLevel(Level.DEBUG);
root.debug(message1);
root.debug(message2);
Thread.sleep(100);
final List<LogEvent> events = listApp.getEvents();
assertNotNull("No event retrieved", events);
assertTrue("No events retrieved", events.size() > 0);
assertTrue("Incorrect event", events.get(0).getMessage().getFormattedMessage().equals(message1));
assertTrue("Incorrect number of events received: " + events.size(), events.size() == 2);
assertTrue("Incorrect event", events.get(1).getMessage().getFormattedMessage().equals(message2));
}
示例9: testServer
import org.apache.logging.log4j.core.Logger; //導入方法依賴的package包/類
@Test
public void testServer() throws Exception {
final Filter clientFilter = new MessageFilter(Filter.Result.NEUTRAL, Filter.Result.DENY);
final Filter serverFilter = new MessageFilter(Filter.Result.DENY, Filter.Result.NEUTRAL);
final CompositeFilter clientFilters = CompositeFilter.createFilters(new Filter[]{clientFilter});
final JMSQueueAppender appender = JMSQueueAppender.createAppender("Test", null, null, null, null, null, FACTORY_NAME,
QUEUE_NAME, null, null, null, clientFilters, "true");
appender.start();
final CompositeFilter serverFilters = CompositeFilter.createFilters(new Filter[]{serverFilter});
final ListAppender listApp = new ListAppender("Events", serverFilters, null, false, false);
listApp.start();
final PatternLayout layout = PatternLayout.createLayout("%m %ex%n", null, null, null, null);
final ConsoleAppender console = ConsoleAppender.createAppender(layout, null, "SYSTEM_OUT", "Console", "false", "true");
console.start();
final Logger serverLogger = ctx.getLogger(JMSTopicReceiver.class.getName());
serverLogger.addAppender(console);
serverLogger.setAdditive(false);
// set appender on root and set level to debug
root.addAppender(listApp);
root.addAppender(appender);
root.setAdditive(false);
root.setLevel(Level.DEBUG);
root.debug("This is a test message");
Thread.sleep(100);
final List<LogEvent> events = listApp.getEvents();
assertNotNull("No event retrieved", events);
assertTrue("No events retrieved", events.size() > 0);
assertTrue("Incorrect event", events.get(0).getMessage().getFormattedMessage().equals("This is a test message"));
}
示例10: testServer
import org.apache.logging.log4j.core.Logger; //導入方法依賴的package包/類
@Test
public void testServer() throws Exception {
final Filter clientFilter = new MessageFilter(Filter.Result.NEUTRAL, Filter.Result.DENY);
final Filter serverFilter = new MessageFilter(Filter.Result.DENY, Filter.Result.NEUTRAL);
final CompositeFilter clientFilters = CompositeFilter.createFilters(new Filter[]{clientFilter});
final JMSTopicAppender appender = JMSTopicAppender.createAppender("Test", null, null, null, null, null, FACTORY_NAME,
TOPIC_NAME, null, null, null, clientFilters, "true");
appender.start();
final CompositeFilter serverFilters = CompositeFilter.createFilters(new Filter[]{serverFilter});
final ListAppender listApp = new ListAppender("Events", serverFilters, null, false, false);
listApp.start();
final PatternLayout layout = PatternLayout.createLayout("%m %ex%n", null, null, null, null);
final ConsoleAppender console =
ConsoleAppender.createAppender(layout, null, "SYSTEM_OUT", "Console", "false", "true");
console.start();
final Logger serverLogger = ctx.getLogger(JMSTopicReceiver.class.getName());
serverLogger.addAppender(console);
serverLogger.setAdditive(false);
// set appender on root and set level to debug
root.addAppender(listApp);
root.addAppender(appender);
root.setAdditive(false);
root.setLevel(Level.DEBUG);
root.debug("This is a test message");
Thread.sleep(100);
final List<LogEvent> events = listApp.getEvents();
assertNotNull("No event retrieved", events);
assertTrue("No events retrieved", events.size() > 0);
assertTrue("Incorrect event", events.get(0).getMessage().getFormattedMessage().equals("This is a test message"));
}
示例11: LayoutTestLogAppender
import org.apache.logging.log4j.core.Logger; //導入方法依賴的package包/類
public LayoutTestLogAppender(String loggerName, String pattern) {
super(loggerName, null, getLayout(pattern), false, false);
Logger logger = (Logger) LogManager.getLogger(loggerName);
logger.getContext().reconfigure();
logger.addAppender(this);
this.start();
}
示例12: testLayoutNormalApi
import org.apache.logging.log4j.core.Logger; //導入方法依賴的package包/類
static void testLayoutNormalApi(final Logger root, final AbstractCsvLayout layout, final boolean messageApi)
throws Exception {
removeAppenders(root);
// set up appender
final ListAppender appender = new ListAppender("List", null, layout, true, false);
appender.start();
appender.countDownLatch = new CountDownLatch(4);
// set appender on root and set level to debug
root.addAppender(appender);
root.setLevel(Level.DEBUG);
// output messages
if (messageApi) {
logDebugObjectArrayMessage(root);
} else {
logDebugNormalApi(root);
}
final int msgCount = 4;
if (appender.getMessages().size() < msgCount) {
// wait until background thread finished processing
appender.countDownLatch.await(10, TimeUnit.SECONDS);
}
assertEquals("Background thread did not finish processing: msg count", msgCount, appender.getMessages().size());
// don't stop appender until background thread is done
appender.stop();
final List<String> list = appender.getMessages();
final char d = layout.getFormat().getDelimiter();
Assert.assertEquals("1" + d + "2" + d + "3", list.get(0));
Assert.assertEquals("2" + d + "3", list.get(1));
Assert.assertEquals("5" + d + "6", list.get(2));
Assert.assertEquals("7" + d + "8" + d + "9" + d + "10", list.get(3));
}
示例13: testStructured
import org.apache.logging.log4j.core.Logger; //導入方法依賴的package包/類
@Test
public void testStructured() throws IOException {
final Agent[] agents = new Agent[] { Agent.createAgent("localhost",
testPort) };
final FlumeAppender avroAppender = FlumeAppender.createAppender(agents,
null, null, "false", "Avro", null, "1000", "1000", "1", "1000",
"avro", "false", null, null, null, "ReqCtx_", null, "true",
"1", null, null, null, null);
avroAppender.start();
final Logger eventLogger = (Logger) LogManager.getLogger("EventLogger");
Assert.assertNotNull(eventLogger);
eventLogger.addAppender(avroAppender);
eventLogger.setLevel(Level.ALL);
final StructuredDataMessage msg = new StructuredDataMessage("Transfer",
"Success", "Audit");
msg.put("memo", "This is a memo");
msg.put("acct", "12345");
msg.put("amount", "100.00");
ThreadContext.put("id", UUID.randomUUID().toString());
ThreadContext.put("memo", null);
ThreadContext.put("test", "123");
EventLogger.logEvent(msg);
final Transaction transaction = channel.getTransaction();
transaction.begin();
final Event event = channel.take();
Assert.assertNotNull(event);
Assert.assertTrue("Channel contained event, but not expected message", getBody(event).endsWith("Success"));
transaction.commit();
transaction.close();
eventSource.stop();
eventLogger.removeAppender(avroAppender);
avroAppender.stop();
}
示例14: activate
import org.apache.logging.log4j.core.Logger; //導入方法依賴的package包/類
public void activate() {
Logger coreLogger = (Logger) LogManager.getLogger(loggerName);
appender.start();
coreLogger.addAppender(appender);
}
示例15: ConsoleAppender
import org.apache.logging.log4j.core.Logger; //導入方法依賴的package包/類
public ConsoleAppender() {
super("DiscordSRV-ConsoleChannel", null, PATTERN_LAYOUT, false);
Logger rootLogger = (Logger) LogManager.getRootLogger();
rootLogger.addAppender(this);
}