Refine search
public static void main(String... args) { // Add the Stackdriver Logging handler LoggingHandler.addHandler(LOGGER, new LoggingHandler()); // log using the logger LOGGER.warning("test warning"); } }
loggingOptions = options != null ? options : LoggingOptions.getDefaultInstance(); LoggingConfig config = new LoggingConfig(getClass().getName()); setFilter(config.getFilter()); setFormatter(config.getFormatter()); Level level = config.getLogLevel(); setLevel(level); baseLevel = level.equals(Level.ALL) ? Level.FINEST : level; flushLevel = config.getFlushLevel(); }; getLogging().setFlushSeverity(severityFor(flushLevel)); getLogging().setWriteSynchronicity(config.getSynchronicity()); reportError(null, ex, ErrorManager.OPEN_FAILURE); throw ex;
@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); } } }
/** * Sets minimum logging level to log immediately and flush any pending writes. * * @param flushLevel minimum log level to trigger flush */ public void setFlushLevel(Level flushLevel) { this.flushLevel = flushLevel; getLogging().setFlushSeverity(severityFor(flushLevel)); }
@Override public void flush() { try { getLogging().flush(); } catch (Exception ex) { getErrorManager().error(null, ex, ErrorManager.FLUSH_FAILURE); } }
@Test public void testClose() throws Exception { expect(options.getProjectId()).andReturn(PROJECT).anyTimes(); expect(options.getService()).andReturn(logging); logging.setFlushSeverity(Severity.ERROR); expectLastCall().once(); logging.setWriteSynchronicity(Synchronicity.ASYNC); expectLastCall().once(); logging.write(ImmutableList.of(FINEST_ENTRY), DEFAULT_OPTIONS); expectLastCall().once(); logging.close(); expectLastCall().once(); replay(options, logging); Handler handler = new LoggingHandler(LOG_NAME, options, DEFAULT_RESOURCE); handler.setLevel(Level.ALL); handler.setFormatter(new TestFormatter()); handler.publish(newLogRecord(Level.FINEST, MESSAGE)); handler.close(); handler.close(); } }
public void testFlushLevel() { expect(options.getProjectId()).andReturn(PROJECT).anyTimes(); expect(options.getService()).andReturn(logging); logging.setFlushSeverity(Severity.ERROR); expectLastCall().once(); logging.setWriteSynchronicity(Synchronicity.ASYNC); expectLastCall().once(); logging.setFlushSeverity(Severity.WARNING); expectLastCall().once(); logging.write( ImmutableList.of( FINEST_ENTRY, FINER_ENTRY, FINE_ENTRY, CONFIG_ENTRY, INFO_ENTRY, WARNING_ENTRY), DEFAULT_OPTIONS); expectLastCall().once(); replay(options, logging); LoggingHandler handler = new LoggingHandler(LOG_NAME, options, DEFAULT_RESOURCE); handler.setLevel(Level.ALL); handler.setFlushLevel(Level.WARNING); handler.setFormatter(new TestFormatter()); handler.publish(newLogRecord(Level.FINEST, MESSAGE)); handler.publish(newLogRecord(Level.FINER, MESSAGE)); handler.publish(newLogRecord(Level.FINE, MESSAGE)); handler.publish(newLogRecord(Level.CONFIG, MESSAGE)); handler.publish(newLogRecord(Level.INFO, MESSAGE)); handler.publish(newLogRecord(Level.WARNING, MESSAGE)); }
@Test public void testSyncWrite() { expect(options.getProjectId()).andReturn(PROJECT).anyTimes(); expect(options.getService()).andReturn(logging); LogEntry entry = LogEntry.newBuilder(Payload.StringPayload.of(MESSAGE)) .setSeverity(Severity.DEBUG) .addLabel("levelName", "FINEST") .addLabel("levelValue", String.valueOf(Level.FINEST.intValue())) .setTimestamp(123456789L) .build(); logging.setFlushSeverity(Severity.ERROR); expectLastCall().once(); logging.setWriteSynchronicity(Synchronicity.ASYNC); expectLastCall().once(); logging.setWriteSynchronicity(Synchronicity.SYNC); expectLastCall().once(); logging.write(ImmutableList.of(entry), DEFAULT_OPTIONS); expectLastCall().once(); replay(options, logging); LoggingHandler handler = new LoggingHandler(LOG_NAME, options, DEFAULT_RESOURCE); handler.setLevel(Level.ALL); handler.setSynchronicity(Synchronicity.SYNC); handler.setFormatter(new TestFormatter()); LogRecord record = new LogRecord(Level.FINEST, MESSAGE); record.setMillis(123456789L); handler.publish(record); }
@Test public void testAddHandler() { expect(options.getProjectId()).andReturn(PROJECT).anyTimes(); expect(options.getService()).andReturn(logging); logging.setFlushSeverity(Severity.ERROR); expectLastCall().once(); logging.setWriteSynchronicity(Synchronicity.ASYNC); expectLastCall().andVoid(); logging.write(ImmutableList.of(FINEST_ENTRY), DEFAULT_OPTIONS); expectLastCall().once(); replay(options, logging); LoggingHandler handler = new LoggingHandler(LOG_NAME, options, DEFAULT_RESOURCE) { @Override public void close() { // Make close NOOP to avoid mock close exception } }; handler.setLevel(Level.ALL); handler.setFormatter(new TestFormatter()); Logger logger = Logger.getLogger(getClass().getName()); logger.setLevel(Level.ALL); LoggingHandler.addHandler(logger, handler); logger.log(newLogRecord(Level.FINEST, MESSAGE)); }
@Test public void testLoggingHandler() throws InterruptedException { String logId = formatForTest("test-logging-handler"); LoggingOptions options = logging().getOptions(); LogName logName = ProjectLogName.of(options.getProjectId(), logId); LoggingHandler handler = new LoggingHandler(logId, options); handler.setLevel(Level.INFO); Logger logger = Logger.getLogger(getClass().getName()); logger.addHandler(handler); String filter = createEqualityFilter("logName", logName); Iterator<LogEntry> iterator = logging().listLogEntries(EntryListOption.filter(filter)).iterateAll().iterator(); while (!iterator.hasNext()) { Thread.sleep(500L); iterator = logging().listLogEntries(EntryListOption.filter(filter)).iterateAll().iterator();
@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); }
/** * Sets synchronicity of logging writes. By default, writes are asynchronous. * * @param synchronicity {@link Synchronicity} */ public void setSynchronicity(Synchronicity synchronicity) { getLogging().setWriteSynchronicity(synchronicity); }
/** Get the flush log level. */ public Synchronicity getSynchronicity() { return getLogging().getWriteSynchronicity(); }
private LogEntry logEntryFor(LogRecord record) throws Exception { String payload = getFormatter().format(record); Level level = record.getLevel(); LogEntry.Builder builder = LogEntry.newBuilder(Payload.StringPayload.of(payload)) .setTimestamp(record.getMillis()) .setSeverity(severityFor(level)); if (!baseLevel.equals(level)) { builder .addLabel("levelName", level.getName()) .addLabel("levelValue", String.valueOf(level.intValue())); } for (LoggingEnhancer enhancer : enhancers) { enhancer.enhanceLogEntry(builder); } return builder.build(); }
@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 testSyncLoggingHandler() throws InterruptedException { String logId = formatForTest("test-sync-logging-handler"); LoggingOptions options = logging().getOptions(); LogName logName = ProjectLogName.of(options.getProjectId(), logId); MonitoredResource resource = "zone", "us-central1-a")); LoggingHandler handler = new LoggingHandler(logId, options, resource); handler.setLevel(Level.WARNING); handler.setSynchronicity(Synchronicity.SYNC); Logger logger = Logger.getLogger(getClass().getName()); logger.addHandler(handler); String filter = createEqualityFilter("logName", logName); Iterator<LogEntry> iterator = logging().listLogEntries(EntryListOption.filter(filter)).iterateAll().iterator(); while (!iterator.hasNext()) { Thread.sleep(500L); iterator = logging().listLogEntries(EntryListOption.filter(filter)).iterateAll().iterator();
/** * Sets minimum logging level to log immediately and flush any pending writes. * * @param flushLevel minimum log level to trigger flush */ public void setFlushLevel(Level flushLevel) { this.flushLevel = flushLevel; getLogging().setFlushSeverity(severityFor(flushLevel)); }
@Override public void flush() { try { getLogging().flush(); } catch (Exception ex) { getErrorManager().error(null, ex, ErrorManager.FLUSH_FAILURE); } }
/** * Sets synchronicity of logging writes. By default, writes are asynchronous. * * @param synchronicity {@link Synchronicity} */ public void setSynchronicity(Synchronicity synchronicity) { getLogging().setWriteSynchronicity(synchronicity); }
/** Get the flush log level. */ public Synchronicity getSynchronicity() { return getLogging().getWriteSynchronicity(); }