/*
 * Copyright (C) The Apache Software Foundation. All rights reserved.
 *
 * This software is published under the terms of the Apache Software
 * License version 1.1, a copy of which has been included with this
 * distribution in the LICENSE.txt file.  */

package org.apache.log4j;

import org.apache.log4j.spi.LoggingEvent;
import org.apache.log4j.spi.LoggerRepository;

/**

*/
public abstract class ReceiverBase implements Receiver {
  protected String name;
  protected LoggerRepository repository;
  
  /**
    Gets the name of the receiver. */
  public String getName() {
    return name;
  }
  
  /**
    Sets the name of the receiver. */
  public void setName(String _name) {
    name = _name;
  }
      
  /**
    Gets the logger repository for this receiver. If not
    explicity set, returns the value of 
    LogManager.getLoggerRepository(). */
  public LoggerRepository getLoggerRepository() {
    if (repository != null) {
      return repository;
    } else {
      return LogManager.getLoggerRepository();
    }
  }

  /**
    Sets the logger repository used by this reciever. This
    repository will be used to retrieve the Logger object
    matching the received LoggingEvent objects and used to post
    the event. */
  public void setLoggerRepository(LoggerRepository _repository) {
    repository = _repository;
  }
  
  /**
    Posts the logging event to a logger in the set logger
    repository. */
  public void doPost(LoggingEvent event) {
    // get the "local" logger for this event from the
    // configured repository.
  	Logger localLogger = 
      getLoggerRepository().getLogger(event.categoryName);
  	
  	// if the logger level is greater or equal to the level
  	// of the event, use the logger to append the event.
  	if(event.level.isGreaterOrEqual(localLogger.getEffectiveLevel())) {
      // set the logger for the event
      event.logger = localLogger;
      
      // call the loggers appenders to process the event
  	  localLogger.callAppenders(event);
  	}
  }

}