@Override protected Class<?> findClass(final String name) throws ClassNotFoundException { final String path = name.replace('.', '/').concat(".class"); final URL resource = super.getResource(path); if (resource == null) { throw new ClassNotFoundException(name); } try { final URLConnection uc = resource.openConnection(); final int len = uc.getContentLength(); final InputStream in = new BufferedInputStream(uc.getInputStream()); final byte[] bytecode = new byte[len]; try { IOUtils.readFully(in, bytecode); } finally { Closer.closeSilently(in); } return defineClass(name, bytecode, 0, bytecode.length); } catch (final IOException e) { Throwables.rethrow(e); return null; // unreachable } }
@Override public void run() { System.out.println("Log4j UDP Server started."); this.thread = Thread.currentThread(); final byte[] bytes = new byte[4096]; final DatagramPacket packet = new DatagramPacket(bytes, bytes.length); try { while (!shutdown) { socket.receive(packet); final String str = new String(packet.getData(), 0, packet.getLength()); messageList.add(str); } } catch (final Exception e) { if (!shutdown) { Throwables.rethrow(e); } } System.out.println("Log4j UDP server stopped."); }
@Override public void run() { this.thread = Thread.currentThread(); final byte[] bytes = new byte[4096]; final DatagramPacket packet = new DatagramPacket(bytes, bytes.length); try { while (!shutdown) { latch.countDown(); sock.receive(packet); ++count; final LogEvent event = objectMapper.readValue(packet.getData(), Log4jLogEvent.class); queue.add(event); } } catch (final Throwable e) { e.printStackTrace(); if (!shutdown) { Throwables.rethrow(e); } } }
@Override public void run() { try { try (final Socket socket = serverSocket.accept()) { if (socket != null) { final InputStream is = socket.getInputStream(); while (!shutdown) { final MappingIterator<LogEvent> mappingIterator = objectMapper.readerFor(Log4jLogEvent.class).readValues(is); while (mappingIterator.hasNextValue()) { queue.add(mappingIterator.nextValue()); ++count; } } } } } catch (final EOFException eof) { // Socket is closed. } catch (final Exception e) { if (!shutdown) { Throwables.rethrow(e); } } }
@Override public void append(LogEvent event) { List<LogItem> logItems = new ArrayList<LogItem>(); LogItem item = new LogItem(); logItems.add(item); item.SetTime((int) (event.getTimeMillis() / 1000)); item.PushBack("time", this.dateFormat.format(new Date(event.getTimeMillis()))); item.PushBack("level", event.getLevel().toString()); item.PushBack("thread", event.getThreadName()); StackTraceElement source = event.getSource(); if (source == null && (!event.isIncludeLocation())) { event.setIncludeLocation(true); source = event.getSource(); event.setIncludeLocation(false); } item.PushBack("location", source == null ? "Unknown(Unknown Source)" : source.toString()); String message = event.getMessage().getFormattedMessage(); Throwable throwable = event.getThrown(); if (throwable != null) { for (String s : Throwables.toStringList(throwable)) { message += System.getProperty("line.separator") + s; } } item.PushBack("message", message); Map<String, String> properties = event.getContextMap(); if (properties.size() > 0) { Object[] keys = properties.keySet().toArray(); Arrays.sort(keys); for (int i = 0; i < keys.length; i++) { item.PushBack(keys[i].toString(), properties.get(keys[i].toString())); } } producer.send(this.projectName, this.logstore, this.topic, null, logItems, new LoghubAppenderCallback(LOGGER, this.projectName, this.logstore, this.topic, null, logItems)); }
/** * <p>Performs the formatting by applying the rules to the * specified calendar.</p> * * @param calendar the calendar to format * @param buf the buffer to format into * @param <B> the Appendable class type, usually StringBuilder or StringBuffer. * @return the specified string buffer */ private <B extends Appendable> B applyRules(final Calendar calendar, final B buf) { try { for (final Rule rule : mRules) { rule.appendTo(buf, calendar); } } catch (final IOException ioe) { Throwables.rethrow(ioe); } return buf; }
@Override public void writeXmlConfiguration(final OutputStream output) throws IOException { try { final XMLStreamWriter xmlWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(output); writeXmlConfiguration(xmlWriter); xmlWriter.close(); } catch (final XMLStreamException e) { if (e.getNestedException() instanceof IOException) { throw (IOException)e.getNestedException(); } Throwables.rethrow(e); } }
@Override public String toXmlConfiguration() { final StringWriter sw = new StringWriter(); try { final XMLStreamWriter xmlWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(sw); writeXmlConfiguration(xmlWriter); xmlWriter.close(); } catch (final XMLStreamException e) { Throwables.rethrow(e); } return sw.toString(); }
@Override public void enqueueEvent(final LogEvent event, final AsyncLoggerConfig asyncLoggerConfig) { // LOG4J2-639: catch NPE if disruptor field was set to null after our check above try { final LogEvent logEvent = prepareEvent(event); enqueue(logEvent, asyncLoggerConfig); } catch (final NullPointerException npe) { // Note: NPE prevents us from adding a log event to the disruptor after it was shut down, // which could cause the publishEvent method to hang and never return. LOGGER.warn("Ignoring log event after log4j was shut down: {} [{}] {}", event.getLevel(), event.getLoggerName(), event.getMessage().getFormattedMessage() + (event.getThrown() == null ? "" : Throwables.toStringList(event.getThrown()))); } }
public boolean tryPublish(final RingBufferLogEventTranslator translator) { try { return disruptor.getRingBuffer().tryPublishEvent(translator); } catch (final NullPointerException npe) { // LOG4J2-639: catch NPE if disruptor field was set to null in stop() LOGGER.warn("[{}] Ignoring log event after log4j was shut down: {} [{}] {}", contextName, translator.level, translator.loggerName, translator.message.getFormattedMessage() + (translator.thrown == null ? "" : Throwables.toStringList(translator.thrown))); return false; } }
void enqueueLogMessageInfo(final RingBufferLogEventTranslator translator) { try { // Note: we deliberately access the volatile disruptor field afresh here. // Avoiding this and using an older reference could result in adding a log event to the disruptor after it // was shut down, which could cause the publishEvent method to hang and never return. disruptor.publishEvent(translator); } catch (final NullPointerException npe) { // LOG4J2-639: catch NPE if disruptor field was set to null in stop() LOGGER.warn("[{}] Ignoring log event after log4j was shut down: {} [{}] {}", contextName, translator.level, translator.loggerName, translator.message.getFormattedMessage() + (translator.thrown == null ? "" : Throwables.toStringList(translator.thrown))); } }
private void runTest(final LoggerContextRule rule, final Statement statement) { try { rule.apply(statement, Description .createTestDescription(getClass(), Thread.currentThread().getStackTrace()[1].getMethodName())) .evaluate(); } catch (final Throwable e) { Throwables.rethrow(e); } }
@Override public void run() { final Thread thread = Thread.currentThread(); try { writer(lock, logEventCount, thread.getName(), createOnDemand, true); } catch (final Exception e) { exceptionRef[0] = e; Throwables.rethrow(e); } }
public static void main(final String[] args) { if (args.length != 3) { System.out.println("Required arguments 'id', 'count' and 'lock' not provided"); System.exit(-1); } final String id = args[0]; final int count = Integer.parseInt(args[1]); if (count <= 0) { System.out.println("Invalid count value: " + args[1]); System.exit(-1); } final boolean lock = Boolean.parseBoolean(args[2]); final boolean createOnDemand = Boolean.parseBoolean(args[2]); // System.out.println("Got arguments " + id + ", " + count + ", " + lock); try { writer(lock, count, id, createOnDemand, true); // thread.sleep(50); } catch (final Exception e) { Throwables.rethrow(e); } }
private DataSource createMockDataSource() { try { final DataSource dataSource = mock(DataSource.class); given(dataSource.getConnection()).willAnswer(new Answer<Connection>() { @Override public Connection answer(final InvocationOnMock invocation) throws Throwable { return jdbcRule.getConnectionSource().getConnection(); } }); return dataSource; } catch (final SQLException e) { Throwables.rethrow(e); throw new InternalError("unreachable"); } }