Java 类ch.qos.logback.core.Context 实例源码

项目:minijax    文件:CloudWatchAppenderTest.java   
@Test
public void testDefaultValues() {
    final Context mockContext = mock(Context.class);

    final PutLogEventsResult mockResult = mock(PutLogEventsResult.class);
    when(mockResult.getNextSequenceToken()).thenReturn("2");

    final AWSLogs mockAwsLogs = mock(AWSLogs.class);
    when(mockAwsLogs.putLogEvents(any())).thenReturn(mockResult);

    final CloudWatchAppender appender = new CloudWatchAppender();
    appender.setContext(mockContext);
    appender.setAwsLogs(mockAwsLogs);
    appender.start();
    appender.doAppend(new LoggingEvent());
    appender.stop();
}
项目:minijax    文件:CloudWatchAppenderTest.java   
@Test
public void testAlreadyExists() {
    final Context mockContext = mock(Context.class);

    final PutLogEventsResult mockResult = mock(PutLogEventsResult.class);
    when(mockResult.getNextSequenceToken()).thenReturn("2");

    final AWSLogs mockAwsLogs = mock(AWSLogs.class);
    when(mockAwsLogs.createLogGroup(any())).thenThrow(ResourceAlreadyExistsException.class);
    when(mockAwsLogs.createLogStream(any())).thenThrow(ResourceAlreadyExistsException.class);
    when(mockAwsLogs.putLogEvents(any())).thenReturn(mockResult);

    final CloudWatchAppender appender = new CloudWatchAppender();
    appender.setContext(mockContext);
    appender.setAwsLogs(mockAwsLogs);
    appender.start();
    appender.doAppend(new LoggingEvent());
    appender.stop();
}
项目:bartleby    文件:PatternLayoutBase.java   
/**
 * Returns a map where the default converter map is merged with the map
 * contained in the context.
 */
public Map<String, String> getEffectiveConverterMap() {
  Map<String, String> effectiveMap = new HashMap<String, String>();

  // add the least specific map fist
  Map<String, String> defaultMap = getDefaultConverterMap();
  if (defaultMap != null) {
    effectiveMap.putAll(defaultMap);
  }

  // contextMap is more specific than the default map
  Context context = getContext();
  if (context != null) {
    @SuppressWarnings("unchecked")
    Map<String, String> contextMap = (Map<String, String>) context
        .getObject(CoreConstants.PATTERN_RULE_REGISTRY);
    if (contextMap != null) {
      effectiveMap.putAll(contextMap);
    }
  }
  // set the most specific map last
  effectiveMap.putAll(instanceConverterMap);
  return effectiveMap;
}
项目:bartleby    文件:StatusPrinter.java   
/**
 * Print the contents of the context status, but only if they contain
 * warnings or errors occurring later then the threshold.
 *
 * @param context
 */
public static void printInCaseOfErrorsOrWarnings(Context context, long threshold) {
  if (context == null) {
    throw new IllegalArgumentException("Context argument cannot be null");
  }

  StatusManager sm = context.getStatusManager();
  if (sm == null) {
    ps.println("WARN: Context named \"" + context.getName()
        + "\" has no status manager");
  } else {
    StatusUtil statusUtil = new StatusUtil(context);
    if (statusUtil.getHighestLevel(threshold) >= ErrorStatus.WARN) {
      print(sm, threshold);
    }
  }
}
项目:bartleby    文件:StatusPrinter.java   
/**
 * Print the contents of the context statuses, but only if they contain
 * errors.
 *
 * @param context
 */
public static void printIfErrorsOccured(Context context) {
  if (context == null) {
    throw new IllegalArgumentException("Context argument cannot be null");
  }

  StatusManager sm = context.getStatusManager();
  if (sm == null) {
    ps.println("WARN: Context named \"" + context.getName()
        + "\" has no status manager");
  } else {
    StatusUtil statusUtil = new StatusUtil(context);
    if (statusUtil.getHighestLevel(0) == ErrorStatus.ERROR) {
      print(sm);
    }
  }
}
项目:bartleby    文件:HTMLLayoutBase.java   
/**
 * Returns a map where the default converter map is merged with the map
 * contained in the context.
 */
public Map<String, String> getEffectiveConverterMap() {
  Map<String, String> effectiveMap = new HashMap<String, String>();

  // add the least specific map fist
  Map<String, String> defaultMap = getDefaultConverterMap();
  if (defaultMap != null) {
    effectiveMap.putAll(defaultMap);
  }

  // contextMap is more specific than the default map
  Context context = getContext();
  if (context != null) {
    @SuppressWarnings("unchecked")
    Map<String, String> contextMap = (Map<String, String>) context
        .getObject(CoreConstants.PATTERN_RULE_REGISTRY);
    if (contextMap != null) {
      effectiveMap.putAll(contextMap);
    }
  }
  return effectiveMap;
}
项目:bartleby    文件:FruitFactory.java   
public Fruit buildFruit() {

  Context context = new ContextBase();
  this.fruit = null;
  context.putProperty("fruitKey", "orange-"+count);
  // for next round
  count++;
  FruitConfigurator fruitConfigurator = new FruitConfigurator(this);
  fruitConfigurator.setContext(context);
  try {
    fruitConfigurator.doConfigure(eventList);
  } catch(JoranException je) {
    je.printStackTrace();
  }
  return fruit;
}
项目:bartleby    文件:PrintMe.java   
public static void main(String[] args) throws Exception {
  Context context = new ContextBase();

  Map<ElementSelector, Action> ruleMap = new HashMap<ElementSelector, Action>();

  // we start with the rule for the top-most (root) element
  ruleMap.put(new ElementSelector("*/foo"), new NOPAction());

  // Add an implicit action. 
  List<ImplicitAction> iaList = new ArrayList<ImplicitAction>();
  iaList.add(new PrintMeImplicitAction());
  SimpleConfigurator simpleConfigurator = new SimpleConfigurator(ruleMap,
      iaList); 

  // link the configurator with its context
  simpleConfigurator.setContext(context);

  simpleConfigurator.doConfigure(args[0]);
  StatusPrinter.printInCaseOfErrorsOrWarnings(context);

}
项目:bartleby    文件:NewRuleCalculator.java   
public static void main(String[] args) throws Exception {

    Context context = new ContextBase();

    Map<ElementSelector, Action> ruleMap = new HashMap<ElementSelector, Action>();

    // we start with the rule for the top-most (root) element
    ruleMap.put(new ElementSelector("*/computation"), new ComputationAction1());

    // Associate "/new-rule" pattern with NewRuleAction from the
    // org.apache.joran.action package.
    // 
    // We will let the XML file to teach the Joran interpreter about new rules
    ruleMap.put(new ElementSelector("/computation/newRule"), new NewRuleAction());

    SimpleConfigurator simpleConfigurator = new SimpleConfigurator(ruleMap);
    // link the configurator with its context
    simpleConfigurator.setContext(context);

    simpleConfigurator.doConfigure(args[0]);

    // Print any errors that might have occured.
    StatusPrinter.printInCaseOfErrorsOrWarnings(context);
  }
项目:bartleby    文件:Calculator2.java   
public static void main(String[] args) throws Exception {
  Map<ElementSelector, Action> ruleMap = new HashMap<ElementSelector, Action>();


  // Note the wild card character '*', in the paterns, signifying any level 
  // of nesting.
  ruleMap.put(new ElementSelector("*/computation"), new ComputationAction2());

  ruleMap.put(new ElementSelector("*/computation/literal"), new LiteralAction());
  ruleMap.put(new ElementSelector("*/computation/add"), new AddAction());
  ruleMap.put(new ElementSelector("*/computation/multiply"), new MultiplyAction());

  Context context = new ContextBase();
  SimpleConfigurator simpleConfigurator = new SimpleConfigurator(ruleMap);
  // link the configurator with its context
  simpleConfigurator.setContext(context);

  try {
    simpleConfigurator.doConfigure(args[0]);
  } catch (JoranException e) {
    // Print any errors that might have occured.
    StatusPrinter.print(context);
  }
}
项目:bartleby    文件:Calculator1.java   
public static void main(String[] args) throws Exception {
  Context context = new ContextBase();

  Map<ElementSelector, Action> ruleMap = new HashMap<ElementSelector, Action>();

  // Associate "/computation" pattern with ComputationAction1
  ruleMap.put(new ElementSelector("/computation"), new ComputationAction1());

  // Other associations
  ruleMap.put(new ElementSelector("/computation/literal"), new LiteralAction());
  ruleMap.put(new ElementSelector("/computation/add"), new AddAction());
  ruleMap.put(new ElementSelector("/computation/multiply"), new MultiplyAction());

  SimpleConfigurator simpleConfigurator = new SimpleConfigurator(ruleMap);
  // link the configurator with its context
  simpleConfigurator.setContext(context);

  simpleConfigurator.doConfigure(args[0]);
  // Print any errors that might have occured.
  StatusPrinter.print(context);
}
项目:agent-tooling    文件:AgentLog.java   
@Override
public Appender<ILoggingEvent> buildAppender(Context logcingContext, String fileName) throws JoranException {
    // first sets up the paterns for log layout
    PatternLayoutEncoder encoder = new PatternLayoutEncoder();
    encoder.setPattern(logTemplate);
    encoder.setContext(logcingContext);
    encoder.start();
    // then create a file appender for the given filename
    FileAppender<ILoggingEvent> appender = new FileAppender<ILoggingEvent>();
    appender.setContext(logcingContext);
    appender.setAppend(false);
    appender.setFile(logFolderName + "/" + fileName + ".log");
    appender.setEncoder(encoder);
    appender.start();

    return appender;
}
项目:tasfe-framework    文件:HostNameKeyingStrategy.java   
@Override
public void setContext(Context context) {
    super.setContext(context);
    final String hostname = context.getProperty(CoreConstants.HOSTNAME_KEY);
    if (hostname == null) {
        addError("Hostname could not be found in context. HostNamePartitioningStrategy will not work.");
    } else {
        hostnameHash = ByteBuffer.allocate(4).putInt(hostname.hashCode()).array();
    }
}
项目:tasfe-framework    文件:ContextNameKeyingStrategy.java   
@Override
public void setContext(Context context) {
    super.setContext(context);
    final String hostname = context.getProperty(CoreConstants.CONTEXT_NAME_KEY);
    if (hostname == null) {
        addError("Hostname could not be found in context. HostNamePartitioningStrategy will not work.");
    } else {
        contextNameHash = ByteBuffer.allocate(4).putInt(hostname.hashCode()).array();
    }
}
项目:wngn-jms-kafka    文件:HostNameKeyingStrategy.java   
@Override
public void setContext(Context context) {
    super.setContext(context);
    final String hostname = context.getProperty(CoreConstants.HOSTNAME_KEY);
    if (hostname == null) {
        addError("Hostname could not be found in context. HostNamePartitioningStrategy will not work.");
    } else {
        hostnameHash = ByteBuffer.allocate(4).putInt(hostname.hashCode()).array();
    }
}
项目:wngn-jms-kafka    文件:ContextNameKeyingStrategy.java   
@Override
public void setContext(Context context) {
    super.setContext(context);
    final String hostname = context.getProperty(CoreConstants.CONTEXT_NAME_KEY);
    if (hostname == null) {
        addError("Hostname could not be found in context. HostNamePartitioningStrategy will not work.");
    } else {
        contextNameHash = ByteBuffer.allocate(4).putInt(hostname.hashCode()).array();
    }
}
项目:SkyEye    文件:KafkaAppender.java   
@Override
public void setContext(Context context) {
    super.setContext(context);

    this.host = SysUtil.host;
    this.app = context.getName();
}
项目:SkyEye    文件:AppHostKeyBuilder.java   
@Override
public void setContext(Context context) {
    super.setContext(context);
    String host = context.getProperty(CoreConstants.HOSTNAME_KEY);
    String app = context.getName();
    appHost = ByteBuffer.allocate(4).putInt(new StringBuilder(app).append(host).toString().hashCode()).array();
}
项目:logzio-logback-appender    文件:BaseLogbackAppenderTest.java   
protected Logger createLogger(String token, String type, String loggerName, Integer drainTimeout,
                              boolean addHostname,boolean line, String additionalFields) {

    logger.info("Creating logger {}. token={}, type={}, drainTimeout={}, addHostname={}, line={}, additionalFields={} ",
            loggerName, token, type, drainTimeout, addHostname, line, additionalFields);

    ch.qos.logback.classic.Logger logbackLogger =  (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(loggerName);
    Context logbackContext = logbackLogger.getLoggerContext();
    LogzioLogbackAppender logzioLogbackAppender = new LogzioLogbackAppender();
    logzioLogbackAppender.setContext(logbackContext);
    logzioLogbackAppender.setToken(token);
    logzioLogbackAppender.setLogzioType(type);
    logzioLogbackAppender.setDebug(true);
    logzioLogbackAppender.setLine(line);
    logzioLogbackAppender.setLogzioUrl("http://" + mockListener.getHost() + ":" + mockListener.getPort());
    logzioLogbackAppender.setAddHostname(addHostname);
    if (drainTimeout != null) {
        logzioLogbackAppender.setDrainTimeoutSec(drainTimeout);
    }
    if (additionalFields != null) {
        logzioLogbackAppender.setAdditionalFields(additionalFields);
    }
    logzioLogbackAppender.start();
    assertThat(logzioLogbackAppender.isStarted()).isTrue();
    logbackLogger.addAppender(logzioLogbackAppender);
    logbackLogger.setAdditive(false);
    return logbackLogger;
}
项目:bartleby    文件:StatusUtil.java   
/**
 * Returns true if the StatusManager associated with the context passed
 * as parameter has one or more StatusListener instances registered. Returns
 * false otherwise.
 *
 * @param context
 * @return true if one or more StatusListeners registered, false otherwise
 * @since 1.0.8
 */
static public boolean contextHasStatusListener(Context context) {
  StatusManager sm = context.getStatusManager();
  if(sm == null)
    return false;
  List<StatusListener> listeners = sm.getCopyOfStatusListenerList();
  if(listeners == null || listeners.size() == 0)
    return false;
  else
    return true;
}
项目:bartleby    文件:ConverterUtil.java   
public static <E> void setContextForConverters(Context context, Converter<E> head) {
  Converter<E> c = head;
  while (c != null) {
    if (c instanceof ContextAware) {
      ((ContextAware) c).setContext(context);
    }
    c = c.getNext();
  }
}
项目:bartleby    文件:FileNamePattern.java   
public FileNamePattern(String patternArg, Context contextArg) {
  // the pattern is slashified
  setPattern(FileFilterUtil.slashify(patternArg));
  setContext(contextArg);
  parse();
  ConverterUtil.startConverters(this.headTokenConverter);
}
项目:bartleby    文件:ShutdownHookBase.java   
/**
 * Default method for stopping the Logback context
 */
protected void stop() {
  addInfo("Logback context being closed via shutdown hook");

  Context hookContext = getContext();
  if (hookContext instanceof ContextBase) {
    ContextBase context = (ContextBase) hookContext;
    context.stop();
  }
}
项目:bartleby    文件:ContextAwareImpl.java   
public void setContext(Context context) {
  if (this.context == null) {
    this.context = context;
  } else if (this.context != context) {
    throw new IllegalStateException("Context has been already set");
  }
}
项目:bartleby    文件:ContextAwareBase.java   
public void setContext(Context context) {
  if (this.context == null) {
    this.context = context;
  } else if (this.context != context) {
    throw new IllegalStateException("Context has been already set");
  }
}
项目:bartleby    文件:Interpreter.java   
public Interpreter(Context context, RuleStore rs, ElementPath initialElementPath) {
  this.cai = new CAI_WithLocatorSupport(context, this);
  ruleStore = rs;
  interpretationContext = new InterpretationContext(context, this);
  implicitActions = new ArrayList<ImplicitAction>(3);
  this.elementPath = initialElementPath;
  actionListStack = new Stack<List<Action>>();
  eventPlayer = new EventPlayer(this);
}
项目:bartleby    文件:InterpretationContext.java   
public InterpretationContext(Context context, Interpreter joranInterpreter) {
  this.context = context;
  this.joranInterpreter = joranInterpreter;
  objectStack = new Stack<Object>();
  objectMap = new HashMap<String, Object>(5);
  propertiesMap = new HashMap<String, String>(5);
}
项目:bartleby    文件:ConfigurationWatchListUtil.java   
public static void setMainWatchURL(Context context, URL url) {
  ConfigurationWatchList cwl = getConfigurationWatchList(context);
  if (cwl == null) {
    cwl = new ConfigurationWatchList();
    cwl.setContext(context);
    context.putObject(CoreConstants.CONFIGURATION_WATCH_LIST, cwl);
  } else {
    cwl.clear();
  }
  setConfigurationWatchListResetFlag(context, true);
  cwl.setMainURL(url);
}
项目:bartleby    文件:ConfigurationWatchListUtil.java   
public static URL getMainWatchURL(Context context) {
  ConfigurationWatchList cwl = getConfigurationWatchList(context);
  if (cwl == null) {
    return null;
  } else {
    return cwl.getMainURL();
  }
}
项目:bartleby    文件:ConfigurationWatchListUtil.java   
public static void addToWatchList(Context context, URL url) {
  ConfigurationWatchList cwl = getConfigurationWatchList(context);
  if (cwl == null) {
    addWarn(context, "Null ConfigurationWatchList. Cannot add " + url);
  } else {
    addInfo(context, "Adding [" + url + "] to configuration watch list.");
    cwl.addToWatchList(url);
  }
}
项目:bartleby    文件:ConfigurationWatchListUtil.java   
public static boolean wasConfigurationWatchListReset(Context context) {
  Object o = context.getObject(CoreConstants.CONFIGURATION_WATCH_LIST_RESET);
  if (o == null)
    return false;
  else {
    return ((Boolean) o).booleanValue();
  }
}
项目:bartleby    文件:ConfigurationWatchListUtil.java   
static void addStatus(Context context, Status s) {
  if (context == null) {
    System.out.println("Null context in " + ConfigurationWatchList.class.getName());
    return;
  }
  StatusManager sm = context.getStatusManager();
  if (sm == null) return;
  sm.add(s);
}
项目:bartleby    文件:StatusPrinter.java   
/**
* Print context's status data with a timestamp higher than the threshold.
* @param context
*/
public static void print(Context context, long threshold) {
  if (context == null) {
    throw new IllegalArgumentException("Context argument cannot be null");
  }

  StatusManager sm = context.getStatusManager();
  if (sm == null) {
    ps.println("WARN: Context named \"" + context.getName()
        + "\" has no status manager");
  } else {
    print(sm, threshold);
  }
}
项目:bartleby    文件:AbstractPatternLayoutBaseTest.java   
@Test
public void testUnStarted() {
  PatternLayoutBase<E> plb = getPatternLayoutBase();
  Context context = new ContextBase();
  plb.setContext(context);
  String s = plb.doLayout(getEventObject());
  assertEquals("", s);
  StatusManager sm = context.getStatusManager();
  StatusPrinter.print(sm);
}
项目:bartleby    文件:AbstractPatternLayoutBaseTest.java   
@Test
public void testStarted() {
  PatternLayoutBase<E> plb = getPatternLayoutBase();
  Context context = new ContextBase();
  plb.setContext(context);
  String s = plb.doLayout(getEventObject());
  assertEquals("", s);
  StatusManager sm = context.getStatusManager();
  StatusPrinter.print(sm);
}
项目:bartleby    文件:AbstractPatternLayoutBaseTest.java   
@Test
public void testNullPattern() {
  //System.out.println("testNullPattern");
  PatternLayoutBase<E> plb = getPatternLayoutBase();
  Context context = new ContextBase();
  plb.setContext(context);
  plb.setPattern(null);
  plb.start();
  String s = plb.doLayout(getEventObject());
  assertEquals("", s);
  StatusChecker checker = new StatusChecker(context.getStatusManager());
  //StatusPrinter.print(context);
  checker.assertContainsMatch("Empty or null pattern.");
}
项目:bartleby    文件:AbstractPatternLayoutBaseTest.java   
@Test
public void testEmptyPattern() {
  //System.out.println("testNullPattern");
  PatternLayoutBase<E> plb = getPatternLayoutBase();
  Context context = new ContextBase();
  plb.setContext(context);
  plb.setPattern("");
  plb.start();
  String s = plb.doLayout(getEventObject());
  assertEquals("", s);
  StatusChecker checker = new StatusChecker(context.getStatusManager());
  //StatusPrinter.print(context);
  checker.assertContainsMatch("Empty or null pattern.");
}
项目:bartleby    文件:SamplePatternLayoutTest.java   
@Test
public void testOK() {
  PatternLayoutBase<Object> plb = getPatternLayoutBase();
  Context context = new ContextBase();
  plb.setContext(context);
  plb.setPattern("x%OTT");
  plb.start();
  String s = plb.doLayout(new Object());
  //System.out.println(s);

  //StatusManager sm = context.getStatusManager();
  //StatusPrinter.print(sm);
  assertEquals("x123", s);
}
项目:bartleby    文件:SamplePatternLayoutTest.java   
@Test
public void testEscapeClosingParentheses() {
  PatternLayoutBase<Object> plb = getPatternLayoutBase();
  Context context = new ContextBase();
  plb.setContext(context);
  plb.setPattern("x(%OTT\\)y");
  plb.start();
  String s = plb.doLayout(new Object());
  assertEquals("x(123)y", s);
}
项目:bartleby    文件:SamplePatternLayoutTest.java   
@Test
public void testEscapeBothParentheses() {
  PatternLayoutBase<Object> plb = getPatternLayoutBase();
  Context context = new ContextBase();
  plb.setContext(context);
  plb.setPattern("x\\(%OTT\\)y");
  plb.start();
  String s = plb.doLayout(new Object());
  assertEquals("x(123)y", s);
}