org.apache.log4j.Appender类的使用及代码示例

x33g5p2x  于2022-01-15 转载在 其他  
字(10.0k)|赞(0)|评价(0)|浏览(126)

本文整理了Java中org.apache.log4j.Appender类的一些代码示例,展示了Appender类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Appender类的具体详情如下:
包路径:org.apache.log4j.Appender
类名称:Appender

Appender介绍

[英]Implement this interface for your own strategies for outputting log statements.
[中]为您自己的日志语句输出策略实现此接口。

代码示例

代码示例来源:origin: log4j/log4j

/**
  Close all attached appenders implementing the AppenderAttachable
  interface.
  @since 1.0
*/
synchronized
void closeNestedAppenders() {
 Enumeration enumeration = this.getAllAppenders();
 if(enumeration != null) {
  while(enumeration.hasMoreElements()) {
 Appender a = (Appender) enumeration.nextElement();
 if(a instanceof AppenderAttachable) {
  a.close();
 }
  }
 }
}

代码示例来源:origin: log4j/log4j

Enumeration e = props.keys();
String name = "";
while (e.hasMoreElements()) {
 String key = (String) e.nextElement();
 if (key.startsWith(filterPrefix)) {
  int dotIdx = key.indexOf('.', fIdx);
   filterOpts = new Vector();
   filters.put(filterKey, filterOpts);
   String value = OptionConverter.findAndSubst(key, props);
   filterOpts.add(new NameValue(name, value));
   propSetter.activate();
   LogLog.debug("Adding filter of type ["+filter.getClass()
    +"] to appender named ["+appender.getName()+"].");
   appender.addFilter(filter);
  LogLog.warn("Missing class definition for filter: ["+key+"]");

代码示例来源:origin: pentaho/pentaho-kettle

public static void setLayout( Layout layout ) {
 LogWriter.layout = layout; // save for later creation of new files...
 Enumeration<?> appenders = logWriter.pentahoLogger.getAllAppenders();
 while ( appenders.hasMoreElements() ) {
  Appender appender = (Appender) appenders.nextElement();
  if ( appender instanceof Log4jConsoleAppender || appender instanceof Log4jFileAppender ) {
   appender.setLayout( layout );
  }
 }
}

代码示例来源:origin: log4j/log4j

void parseFilters(Element element, Appender appender) {
 String clazz = subst(element.getAttribute(CLASS_ATTR));
 Filter filter = (Filter) OptionConverter.instantiateByClassName(clazz,
                       Filter.class, null);
  PropertySetter propSetter = new PropertySetter(filter);
  NodeList children = element.getChildNodes();
  final int length 	= children.getLength();
  propSetter.activate();
  LogLog.debug("Adding filter of type ["+filter.getClass()
     +"] to appender named ["+appender.getName()+"].");
  appender.addFilter(filter);

代码示例来源:origin: log4j/log4j

String layoutPrefix = prefix + ".layout";
appender.setName(appenderName);
 if(appender.requiresLayout()) {
Layout layout = (Layout) OptionConverter.instantiateByKey(props,
               layoutPrefix,
               Layout.class,
               null);
if(layout != null) {
 appender.setLayout(layout);
 LogLog.debug("Parsing layout options for \"" + appenderName +"\".");
   PropertySetter.setProperties(layout, props, layoutPrefix + ".");
 String errorHandlerClass = OptionConverter.findAndSubst(errorHandlerPrefix, props);
         null);
    if (eh != null) {
       appender.setErrorHandler(eh);
       PropertySetter.setProperties(eh, edited, errorHandlerPrefix + ".");
 PropertySetter.setProperties(appender, props, prefix + ".");

代码示例来源:origin: log4j/log4j

String appenderString = (prio == null ? "" : prio.toString());
while (appenders.hasMoreElements()) {
 Appender app = (Appender) appenders.nextElement();
 String name;
  if ((name = app.getName()) == null || isGenAppName(name)) {
    name = genAppName();
  if (app.getLayout() != null) {
   printOptions(out, app.getLayout(), "log4j.appender."+name+".layout");
String catKey = (cat == Logger.getRootLogger())
  ? "log4j.rootLogger"
  : "log4j.logger." + cat.getName();
 out.println(catKey + "=" + appenderString);
if (!cat.getAdditivity() && cat != Logger.getRootLogger()) {
  out.println("log4j.additivity." + cat.getName() + "=false");

代码示例来源:origin: apache/ignite

for (Enumeration appenders = l.getAllAppenders(); appenders.hasMoreElements(); ) {
    Appender appender = (Appender)appenders.nextElement();
      if ("CONSOLE_ERR".equals(appender.getName())) {
  impl.setLevel(logLevel);
impl.setLevel(Level.OFF);

代码示例来源:origin: log4j/log4j

} catch (InterruptedException e) {
 Thread.currentThread().interrupt();
 org.apache.log4j.helpers.LogLog.error(
  "Got an InterruptedException while waiting for the "
  + "dispatcher to finish.", e);
  while (iter.hasMoreElements()) {
   Object next = iter.nextElement();
    ((Appender) next).close();

代码示例来源:origin: pentaho/pentaho-kettle

public boolean close() {
 boolean isNotEmbedded = true;
 try {
  // Close all appenders only if we are not embedded (ie. running a report in BA Server
  // that has a PDI data source is considered embedded)
  Logger logger = Logger.getLogger( STRING_PENTAHO_DI_LOGGER_NAME );
  Enumeration<?> appenders = logger.getAllAppenders();
  while ( appenders.hasMoreElements() ) {
   final Appender appender = (Appender) appenders.nextElement();
   // Check to see if we have registered BA Server appenders
   if ( ( appender.getName().compareTo( STRING_PENTAHO_BASERVER_FILE_APPENDER ) == 0 )
    || ( appender.getName().compareTo( STRING_PENTAHO_BASERVER_CONSOLE_APPENDER ) == 0 ) ) {
    isNotEmbedded = false;
    break;
   }
  }
  // If we are not embedded, we can safely close all appenders.
  if ( isNotEmbedded == true ) {
   pentahoLogger.removeAllAppenders();
   LogWriter.unsetLogWriter();
  }
 } catch ( Exception e ) {
  isNotEmbedded = false;
 }
 return isNotEmbedded;
}

代码示例来源:origin: tarun3kumar/seleniumtestsframework

public static Logger getLogger(final Class<?> cls) {
  boolean rootIsConfigured = Logger.getRootLogger().getAllAppenders().hasMoreElements();
  if (!rootIsConfigured) {
    BasicConfigurator.configure();
    Logger.getRootLogger().setLevel(Level.INFO);
    Appender appender = (Appender) Logger.getRootLogger().getAllAppenders().nextElement();
    appender.setLayout(new PatternLayout(" %-5p %d [%t] %C{1}: %m%n"));
  }
  return Logger.getLogger(cls);
}

代码示例来源:origin: openmrs/openmrs-core

Logger rootLogger = Logger.getRootLogger();
Enumeration appenders = rootLogger.getAllAppenders();
while (appenders.hasMoreElements()) {
  Appender appender = (Appender) appenders.nextElement();
  if (OpenmrsConstants.LOG_OPENMRS_FILE_APPENDER.equals(appender.getName())) {
    fileAppender = new RollingFileAppender(patternLayout, logLocation);
    fileAppender.setName(OpenmrsConstants.LOG_OPENMRS_FILE_APPENDER);
    rootLogger.addAppender(fileAppender);
  } else {
    fileAppender.setFile(logLocation);

代码示例来源:origin: org.wso2.wsas/wso2wsas-admin

private Appender getAppenderInLoggerWithName(Logger logger, String appenderName) {
  Enumeration appenders = logger.getAllAppenders();
  Appender targetAppender = null;
  while (appenders.hasMoreElements()) {
    Appender appender = (Appender) appenders.nextElement();
    if (appender.getName().equals(appenderName)) {
      targetAppender = appender;
      break;
    }
  }
  return targetAppender;
}

代码示例来源:origin: waterguo/antsdb

public BetterCommandLine() {
  BasicConfigurator.configure();
  LogManager.getRootLogger().setLevel(Level.OFF);
  Appender appender = (Appender)LogManager.getRootLogger().getAllAppenders().nextElement();
  appender.setLayout(new PatternLayout("%d{HH:mm:ss,SSS} [%t] %-5p %c{2} - %m%n"));
}

代码示例来源:origin: log4j/log4j

/**
 * Remove and close all previously attached appenders.
 * */
public
void removeAllAppenders() {
 if(appenderList != null) {
  int len = appenderList.size();      
  for(int i = 0; i < len; i++) {
 Appender a = (Appender) appenderList.elementAt(i);
 a.close();
  }
  appenderList.removeAllElements();
  appenderList = null;      
 }
}

代码示例来源:origin: log4j/log4j

/**
  Call the <code>doAppend</code> method on all attached appenders.  */
public
int appendLoopOnAppenders(LoggingEvent event) {
 int size = 0;
 Appender appender;
 if(appenderList != null) {
  size = appenderList.size();
  for(int i = 0; i < size; i++) {
 appender = (Appender) appenderList.elementAt(i);
 appender.doAppend(event);
  }
 }    
 return size;
}

代码示例来源:origin: log4j/log4j

/**
  Prints the message and the stack trace of the exception on
  <code>System.err</code>.
 */
public
void error(String message, Exception e, int errorCode, LoggingEvent event) {
 if (e instanceof InterruptedIOException) {
   Thread.currentThread().interrupt();
 }
 LogLog.debug("FB: The following error reported: " + message, e);
 LogLog.debug("FB: INITIATING FALLBACK PROCEDURE.");
 if (loggers != null) {
   for(int i = 0; i < loggers.size(); i++) {
      Logger l = (Logger) loggers.elementAt(i);
      LogLog.debug("FB: Searching for ["+primary.getName()+"] in logger ["
         +l.getName() + "].");
      LogLog.debug("FB: Replacing ["+primary.getName()+"] by ["
         + backup.getName() + "] in logger ["+ l.getName() +"].");
      l.removeAppender(primary);
      LogLog.debug("FB: Adding appender ["+backup.getName()+"] to logger "
         +  l.getName());
      l.addAppender(backup);
   }
 }    
}

代码示例来源:origin: log4j/log4j

/**
 Remove the appender with the name passed as parameter form the
 list of appenders.  
*/
public
void removeAppender(String name) {
 if(name == null || appenderList == null) return;
 int size = appenderList.size();
 for(int i = 0; i < size; i++) {
  if(name.equals(((Appender)appenderList.elementAt(i)).getName())) {
  appenderList.removeElementAt(i);
  break;
  }
 }
}

代码示例来源:origin: cloudfoundry/uaa

@Test
public void messages_over_multiple_lines_are_formatted_per_line() throws Exception {
  ByteArrayOutputStream output = new ByteArrayOutputStream();
  LineAwareLayout lineAwareLayout = new LineAwareLayout();
  lineAwareLayout.setLineLayout(new PatternLayout("%m" + delimiter));
  Appender appender = new WriterAppender(lineAwareLayout, output);
  appender.setName("TestLog");
  appender.setLayout(lineAwareLayout);
  Logger testLogger = LogManager.getLogger("test-logger");
  testLogger.addAppender((appender));
  testLogger.setLevel(Level.INFO);
  String eventMessage = "test message\nwith\nmultiple lines";
  testLogger.info(eventMessage);
  String expectedLog = String.join(delimiter, eventMessage.split("\n")) + delimiter;
  assertEquals(expectedLog, output.toString());
}

代码示例来源:origin: log4j/log4j

cat.debug("getAttribute called with ["+attributeName+"].");
if(attributeName.startsWith("appender="+appender.getName()+",layout")) {
 try {
  return new ObjectName("log4j:"+attributeName );
 } catch(MalformedObjectNameException e) {
  cat.error("attributeName", e);
 } catch(RuntimeException e) {
  cat.error("attributeName", e);

代码示例来源:origin: log4j/log4j

/**
  Look for an attached appender named as <code>name</code>.
  <p>Return the appender with that name if in the list. Return null
  otherwise.  
  
 */
public
Appender getAppender(String name) {
  if(appenderList == null || name == null)
  return null;
  int size = appenderList.size();
  Appender appender;
  for(int i = 0; i < size; i++) {
   appender = (Appender) appenderList.elementAt(i);
   if(name.equals(appender.getName()))
  return appender;
  }
  return null;    
}

相关文章