@Override public void clearEvents() { buffer.clear(); }
private static void tryCall(RunnableWithException c) { try { c.run(); } catch (Exception e) { // Ignore } }
@Override public Iterable<PaxLoggingEvent> getEvents(int maxNum) { return buffer.getElements(maxNum); }
protected void doStart() throws Exception { ConfigurationAdmin configurationAdmin = getTrackedService(ConfigurationAdmin.class); if (configurationAdmin == null) { return; } int size = getInt("size", 500); String pattern = getString("pattern", "%d{ABSOLUTE} | %-5.5p | %-16.16t | %-32.32c{1} | %-32.32C %4L | %m%n"); String errorColor = getString("errorColor", "31"); String warnColor = getString("warnColor", "35"); String infoColor = getString("infoColor", "36"); String debugColor = getString("debugColor", "39"); String traceColor = getString("traceColor", "39"); LogEventFormatterImpl formatter = new LogEventFormatterImpl(); formatter.setPattern(pattern); formatter.setColor(PaxLogger.LEVEL_ERROR, errorColor); formatter.setColor(PaxLogger.LEVEL_WARNING, warnColor); formatter.setColor(PaxLogger.LEVEL_INFO, infoColor); formatter.setColor(PaxLogger.LEVEL_DEBUG, debugColor); formatter.setColor(PaxLogger.LEVEL_TRACE, traceColor); register(LogEventFormatter.class, formatter); LogServiceImpl logService = new LogServiceImpl(configurationAdmin, size); Hashtable<String, Object> props = new Hashtable<>(); props.put("org.ops4j.pax.logging.appender.name", "VmLogAppender"); register(PaxAppender.class, logService, props); register(LogService.class, logService); LogMBeanImpl securityMBean = new LogMBeanImpl(logService); registerMBean(securityMBean, "type=log"); }
private LogServiceInternal getDelegate(Dictionary<String, Object> config) { if (config.get("log4j.rootLogger") != null) { return new LogServiceLog4j1Impl(config); } else if (config.get("log4j2.rootLogger.level") != null) { return new LogServiceLog4j2Impl(config); } else if (config.get("org.ops4j.pax.logging.log4j2.config.file") != null) { String file = config.get("org.ops4j.pax.logging.log4j2.config.file").toString(); if (file.endsWith(".xml")) { return new LogServiceLog4j2XmlImpl(file); } else { throw new IllegalStateException("Unsupported Log4j2 configuration type: " + file); } } else { throw new IllegalStateException("Unrecognized configuration"); } }
public void setLevel(String logger, String level) { // make sure both uppercase and lowercase levels are supported level = level.toUpperCase(); // check if the level is valid Level lvl = Level.valueOf(level); // Default logger if (logger == null) { logger = LogServiceInternal.ROOT_LOGGER; } // Verify if (lvl == Level.DEFAULT && LogServiceInternal.ROOT_LOGGER.equals(logger)) { throw new IllegalStateException("Can not unset the ROOT logger"); } // Get config Configuration cfg = getConfiguration(); Dictionary<String, Object> props = cfg.getProperties(); // Update getDelegate(props).setLevel(logger, level); // Save try { cfg.update(props); } catch (IOException e) { throw new RuntimeException("Error writing log config to config admin", e); } }
public synchronized Iterable<T> getElements() { return getElements(size()); }
public synchronized void add(T element) { if (null == element) { throw new NullPointerException("Attempted to add null object to buffer"); } if (full) { increaseStart(); } elements[end] = element; increaseEnd(); }
public Map<String, String> getLevel(String logger) { Configuration cfg; try { cfg = configAdmin.getConfiguration(CONFIGURATION_PID, null); } catch (IOException e) { throw new RuntimeException("Error retrieving Log information from config admin", e); } if (logger == null) { logger = LogServiceInternal.ROOT_LOGGER; } return getDelegate(cfg.getProperties()).getLevel(logger); }
@Override public synchronized void doAppend(PaxLoggingEvent event) { event.getProperties(); // ensure MDC properties are copied KarafLogEvent eventCopy = new KarafLogEvent(event); this.buffer.add(eventCopy); for (PaxAppender appender : appenders) { try { appender.doAppend(eventCopy); } catch (Throwable t) { // Ignore } } }
static Document loadConfig(String id, InputStream is) throws ParserConfigurationException, SAXException, IOException { final InputSource source = new InputSource(is); source.setPublicId(id); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); factory.setValidating(false); factory.setExpandEntityReferences(false); setFeature(factory, XMLConstants.FEATURE_SECURE_PROCESSING, true); setFeature(factory, "http://xml.org/sax/features/external-general-entities", false); setFeature(factory, "http://xml.org/sax/features/external-parameter-entities", false); setFeature(factory, "http://apache.org/xml/features/nonvalidating/load-external-dtd", false); setFeature(factory, "http://apache.org/xml/features/xinclude/fixup-base-uris", true); setFeature(factory, "http://apache.org/xml/features/xinclude/fixup-language", true); tryCall(() -> factory.setXIncludeAware(true)); DocumentBuilder documentBuilder = factory.newDocumentBuilder(); return documentBuilder.parse(source); }
@Override public PaxLoggingEvent getLastException(String logger) { PaxLoggingEvent throwableEvent = null; Iterable<PaxLoggingEvent> le = getEvents(); for (PaxLoggingEvent event : le) { // if this is an exception, and the log is the same as the requested log, // then save this exception and continue iterating from oldest to newest if ((event.getThrowableStrRep() != null) &&(logger != null) &&(checkIfFromRequestedLog(event, logger))) { throwableEvent = event; // Do not break, as we iterate from the oldest to the newest event } else if ((event.getThrowableStrRep() != null)&&(logger == null)) { // now check if there has been no log passed in, and if this is an exception // then save this exception and continue iterating from oldest to newest throwableEvent = event; } } return throwableEvent; }
public void setLevel(String level) { setLevel(LogServiceInternal.ROOT_LOGGER, level); }
public LogServiceImpl(ConfigurationAdmin configAdmin, int size) { this.configAdmin = configAdmin; this.appenders = new CopyOnWriteArrayList<>(); this.buffer = new CircularBuffer<>(size, PaxLoggingEvent.class); }
private static void setFeature(DocumentBuilderFactory factory, String name, boolean b) { tryCall(() -> factory.setFeature(name, b)); }
public String getLevel() { return getLevel(LogServiceInternal.ROOT_LOGGER).get(LogServiceInternal.ROOT_LOGGER); }
static Document loadConfig(Path path) throws Exception { try (InputStream is = Files.newInputStream(path)) { return loadConfig(path.toString(), is); } }
public synchronized Iterable<T> getElements(int nb) { int s = size(); nb = Math.min(Math.max(0, nb), s); List<T> result = new ArrayList<T>(); for (int i = 0; i < nb; i++) { result.add(elements[(i + s - nb + start) % maxElements]); } return result; }
public CircularBuffer(int size, Class<?> type) { if (size <= 0) { throw new IllegalArgumentException("The size must be greater than 0"); } this.type = type; maxElements = size; clear(); }
@Override public Iterable<PaxLoggingEvent> getEvents() { return buffer.getElements(); }