void printInvalidPropMessage(String key, String value, Exception e) { String msg = "Invalid property value for " + prefix + ":" + key + "/" + value; errorMan.error(msg, e, ErrorManager.GENERIC_FAILURE); }
/** * Reports an error to the error manager associated with this handler, * {@code ErrorManager} is used for that purpose. No security checks are * done, therefore this is compatible with environments where the caller * is non-privileged. * * @param msg * the error message, may be {@code null}. * @param ex * the associated exception, may be {@code null}. * @param code * an {@code ErrorManager} error code. */ protected void reportError(String msg, Exception ex, int code) { this.errorMan.error(msg, ex, code); }
private void write(String s) { try { this.writer.write(s); } catch (Exception e) { getErrorManager().error("Exception occurred when writing to the output stream", e, ErrorManager.WRITE_FAILURE); } }
/** * Flushes any buffered output. */ @Override public void flush() { if (this.os != null) { try { if (this.writer != null) { this.writer.flush(); } else { this.os.flush(); } } catch (Exception e) { getErrorManager().error("Exception occurred when flushing the output stream", e, ErrorManager.FLUSH_FAILURE); } } }
void findNextGeneration() { super.close(); for (int i = count - 1; i > 0; i--) { if (files[i].exists()) { files[i].delete(); } files[i - 1].renameTo(files[i]); } try { output = new MeasureOutputStream(new BufferedOutputStream( new FileOutputStream(files[0]))); } catch (FileNotFoundException e1) { this.getErrorManager().error("Error opening log file", e1, ErrorManager.OPEN_FAILURE); } setOutputStream(output); }
private void initSocket(String host, String port) throws IOException { // check the validity of the host name if (host == null || host.isEmpty()) { throw new IllegalArgumentException("host == null || host.isEmpty()"); } // check the validity of the port number int p = 0; try { p = Integer.parseInt(port); } catch (NumberFormatException e) { throw new IllegalArgumentException("Illegal port argument"); } if (p <= 0) { throw new IllegalArgumentException("Illegal port argument"); } // establish the network connection try { this.socket = new Socket(host, p); } catch (IOException e) { getErrorManager().error("Failed to establish the network connection", e, ErrorManager.OPEN_FAILURE); throw e; } super.internalSetOutputStream(new BufferedOutputStream(this.socket.getOutputStream())); }
/** * Closes this handler. The network connection to the host is also closed. */ @Override public void close() { try { super.close(); if (this.socket != null) { this.socket.close(); this.socket = null; } } catch (Exception e) { getErrorManager().error("Exception occurred when closing the socket handler", e, ErrorManager.CLOSE_FAILURE); } }
@Override public void flush() { try { getLogging().flush(); } catch (Exception ex) { getErrorManager().error(null, ex, ErrorManager.FLUSH_FAILURE); } }
@Override public void publish(LogRecord record) { // check that the log record should be logged if (!isLoggable(record)) { return; } // HACK warning: this logger doesn't work like normal loggers; the log calls are issued // from another class instead of by itself, so it can't be configured off like normal // loggers. We have to check the source class name instead. if ("io.netty.handler.codec.http2.Http2FrameLogger".equals(record.getSourceClassName())) { return; } LogEntry logEntry; try { logEntry = logEntryFor(record); } catch (Exception ex) { getErrorManager().error(null, ex, ErrorManager.FORMAT_FAILURE); return; } if (logEntry != null) { try { getLogging().write(ImmutableList.of(logEntry), defaultWriteOptions); } catch (Exception ex) { getErrorManager().error(null, ex, ErrorManager.WRITE_FAILURE); } } }
/** * Closes this handler, but the underlying output stream is only closed if * {@code closeStream} is {@code true}. Security is not checked. * * @param closeStream * whether to close the underlying output stream. */ void close(boolean closeStream) { if (this.os != null) { if (this.writerNotInitialized) { initializeWriter(); } write(getFormatter().getTail(this)); try { this.writer.flush(); if (closeStream) { this.writer.close(); this.writer = null; this.os = null; } } catch (Exception e) { getErrorManager().error("Exception occurred when closing the output stream", e, ErrorManager.CLOSE_FAILURE); } } }
@Test public void testReportFormatError() { expect(options.getProjectId()).andReturn(PROJECT).anyTimes(); expect(options.getService()).andReturn(logging); logging.setFlushSeverity(Severity.ERROR); expectLastCall().once(); logging.setWriteSynchronicity(Synchronicity.ASYNC); expectLastCall().once(); replay(options, logging); Formatter formatter = EasyMock.createStrictMock(Formatter.class); RuntimeException ex = new RuntimeException(); ErrorManager errorManager = EasyMock.createStrictMock(ErrorManager.class); errorManager.error(null, ex, ErrorManager.FORMAT_FAILURE); expectLastCall().once(); LogRecord record = newLogRecord(Level.FINEST, MESSAGE); expect(formatter.format(record)).andThrow(ex); replay(errorManager, formatter); Handler handler = new LoggingHandler(LOG_NAME, options, DEFAULT_RESOURCE); handler.setLevel(Level.ALL); handler.setErrorManager(errorManager); handler.setFormatter(formatter); handler.publish(record); verify(errorManager, formatter); }
@Test public void testReportWriteError() { expect(options.getProjectId()).andReturn(PROJECT).anyTimes(); expect(options.getService()).andReturn(logging); RuntimeException ex = new RuntimeException(); logging.setFlushSeverity(Severity.ERROR); expectLastCall().once(); logging.setWriteSynchronicity(Synchronicity.ASYNC); expectLastCall().once(); logging.write(ImmutableList.of(FINEST_ENTRY), DEFAULT_OPTIONS); expectLastCall().andStubThrow(ex); replay(options, logging); ErrorManager errorManager = EasyMock.createStrictMock(ErrorManager.class); errorManager.error(null, ex, ErrorManager.WRITE_FAILURE); expectLastCall().once(); replay(errorManager); Handler handler = new LoggingHandler(LOG_NAME, options, DEFAULT_RESOURCE); handler.setLevel(Level.ALL); handler.setErrorManager(errorManager); handler.setFormatter(new TestFormatter()); handler.publish(newLogRecord(Level.FINEST, MESSAGE)); verify(errorManager); }
@Test public void testReportFlushError() { expect(options.getProjectId()).andReturn(PROJECT).anyTimes(); expect(options.getService()).andReturn(logging); RuntimeException ex = new RuntimeException(); logging.setFlushSeverity(Severity.ERROR); expectLastCall().once(); logging.setWriteSynchronicity(Synchronicity.ASYNC); expectLastCall().once(); logging.write(ImmutableList.of(FINEST_ENTRY), DEFAULT_OPTIONS); expectLastCall().once(); logging.flush(); expectLastCall().andThrow(ex); ErrorManager errorManager = EasyMock.createStrictMock(ErrorManager.class); errorManager.error(null, ex, ErrorManager.FLUSH_FAILURE); expectLastCall().once(); replay(options, logging, errorManager); LogRecord record = newLogRecord(Level.FINEST, MESSAGE); Handler handler = new LoggingHandler(LOG_NAME, options, DEFAULT_RESOURCE); handler.setLevel(Level.ALL); handler.setErrorManager(errorManager); handler.setFormatter(new TestFormatter()); handler.publish(record); handler.flush(); verify(errorManager); }
/** * Protected convenience method to report an error to this Handler's * ErrorManager. This method will prefix all non null error messages with * <tt>Level.SEVERE.getName()</tt>. This allows the receiving error * manager to determine if the <tt>msg</tt> parameter is a simple error * message or a raw email message. * @param msg a descriptive string (may be null) * @param ex an exception (may be null) * @param code an error code defined in ErrorManager */ @Override protected void reportError(String msg, Exception ex, int code) { try { if (msg != null) { errorManager.error(Level.SEVERE.getName() .concat(": ").concat(msg), ex, code); } else { errorManager.error(null, ex, code); } } catch (RuntimeException | LinkageError GLASSFISH_21258) { reportLinkageError(GLASSFISH_21258, code); } }
/** * Protected convenience method to report an error to this Handler's * ErrorManager. This method will prefix all non null error messages with * <tt>Level.SEVERE.getName()</tt>. This allows the receiving error * manager to determine if the <tt>msg</tt> parameter is a simple error * message or a raw email message. * @param msg a descriptive string (may be null) * @param ex an exception (may be null) * @param code an error code defined in ErrorManager */ @Override protected void reportError(String msg, Exception ex, int code) { try { if (msg != null) { errorManager.error(Level.SEVERE.getName() .concat(": ").concat(msg), ex, code); } else { errorManager.error(null, ex, code); } } catch (final RuntimeException GLASSFISH_21258) { reportLinkageError(GLASSFISH_21258, code); } catch (final LinkageError GLASSFISH_21258) { reportLinkageError(GLASSFISH_21258, code); } }
/** * Converts a mime message to a raw string or formats the reason * why message can't be changed to raw string and reports it. * @param msg the mime message. * @param ex the original exception. * @param code the ErrorManager code. * @since JavaMail 1.4.5 */ @SuppressWarnings("UseSpecificCatch") private void reportError(Message msg, Exception ex, int code) { try { try { //Use direct call so we do not prefix raw email. errorManager.error(toRawString(msg), ex, code); } catch (final RuntimeException re) { reportError(toMsgString(re), ex, code); } catch (final Exception e) { reportError(toMsgString(e), ex, code); } } catch (final LinkageError GLASSFISH_21258) { reportLinkageError(GLASSFISH_21258, code); } }
/** * Converts a mime message to a raw string or formats the reason * why message can't be changed to raw string and reports it. * @param msg the mime message. * @param ex the original exception. * @param code the ErrorManager code. * @since JavaMail 1.4.5 */ @SuppressWarnings("UseSpecificCatch") private void reportError(Message msg, Exception ex, int code) { try { try { //Use direct call so we do not prefix raw email. errorManager.error(toRawString(msg), ex, code); } catch (final RuntimeException re) { reportError(toMsgString(re), ex, code); } catch (final Exception e) { reportError(toMsgString(e), ex, code); } } catch (final LinkageError GLASSFISH_21258) { reportLinkageError(GLASSFISH_21258, code); } }
private void move(final ErrorManager errorManager, final Path src, final Path target) { try { Files.move(src, target, StandardCopyOption.REPLACE_EXISTING); } catch (Exception e) { // Report the error, but allow the rotation to continue errorManager.error(String.format("Failed to move file %s to %s.", src, target), e, ErrorManager.GENERIC_FAILURE); } }
private void write(String s) { try { this.writer.write(s); } catch (Exception e) { getErrorManager().error("Exception occurred when writing to the output stream", e, ErrorManager.WRITE_FAILURE); } }