public static void setConsoleLevel(Level level) { // get the top Logger: Logger topLogger = java.util.logging.Logger.getLogger(""); // Handler for console (reuse it if it already exists) Handler consoleHandler = null; // see if there is already a console handler for (Handler handler : topLogger.getHandlers()) { if (handler instanceof ConsoleHandler) { // found the console handler consoleHandler = handler; break; } } if (consoleHandler == null) { // there was no console handler found, create a new one consoleHandler = new ConsoleHandler(); topLogger.addHandler(consoleHandler); } // set the console handler level: consoleHandler.setLevel(level); consoleHandler.setFormatter(new SimpleFormatter()); }
public void close() throws SecurityException { Handler t = resolve(); if(t!=null) t.close(); }
/** * Construct a handler. * * @param handler The handler to copy properties from. * @param formatter The formatter to use. * @throws UnsupportedEncodingException if the encoding is not valid. */ public Stdout(final Handler handler, final Formatter formatter) throws UnsupportedEncodingException { super(System.out, formatter); setErrorManager(handler.getErrorManager()); setFilter(handler.getFilter()); setLevel(handler.getLevel()); setEncoding(handler.getEncoding()); }
@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 testPublishCustomResource() { expect(options.getProjectId()).andReturn(PROJECT).anyTimes(); expect(options.getService()).andReturn(logging); logging.setFlushSeverity(Severity.ERROR); expectLastCall().once(); logging.setWriteSynchronicity(Synchronicity.ASYNC); expectLastCall().once(); MonitoredResource resource = MonitoredResource.of("custom", ImmutableMap.<String, String>of()); logging.write( ImmutableList.of(FINEST_ENTRY), WriteOption.logName(LOG_NAME), WriteOption.resource(resource), WriteOption.labels(BASE_SEVERITY_MAP)); expectLastCall().once(); replay(options, logging); Handler handler = new LoggingHandler(LOG_NAME, options, resource); handler.setLevel(Level.ALL); handler.setFormatter(new TestFormatter()); handler.publish(newLogRecord(Level.FINEST, MESSAGE)); }
private static void setupLogger( boolean console, String file, String levelString, boolean fullStackTraces, boolean rotate) { Logger rootLogger = Logger.getLogger(""); for (Handler handler : rootLogger.getHandlers()) { rootLogger.removeHandler(handler); } Handler handler; if (console) { handler = new ConsoleHandler(); } else { handler = new RollingFileHandler(file, rotate); } handler.setFormatter(new LogFormatter(fullStackTraces)); Level level = Level.parse(levelString.toUpperCase()); rootLogger.setLevel(level); handler.setLevel(level); handler.setFilter(record -> record != null && !record.getLoggerName().startsWith("sun")); rootLogger.addHandler(handler); }
@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(); } }
handlers.close(); PARENT_LOGGER.removeHandler(handlers); loggerHandlerFile = null; handler.setFormatter(formatter); handler.setLevel(PARENT_LOGGER.getLevel()); PARENT_LOGGER.setUseParentHandlers(false); PARENT_LOGGER.addHandler(handler);
@Override public void setFormatter(Formatter newFormatter) throws SecurityException { super.setFormatter(newFormatter); Handler t = resolve(); if(t!=null) t.setFormatter(newFormatter); }
private static void setVlog(Logger logger, LogLevel logLevel) { final Level newLevel = logLevel.getLevel(); logger.setLevel(newLevel); do { for (Handler handler : logger.getHandlers()) { Level handlerLevel = handler.getLevel(); if (newLevel.intValue() < handlerLevel.intValue()) { handler.setLevel(newLevel); } } } while (logger.getUseParentHandlers() && (logger = logger.getParent()) != null); }
/** * Call target handler to flush any buffered output. Note that this doesn't * cause this {@code MemoryHandler} to push. */ @Override public void flush() { target.flush(); }
protected static Handler makeLogger(String logName, Logger heliumRecordLog) { CspFormatter formatter = new CspFormatter(); String fileName = LogBase.getLogBaseDir() + logName; if (isLogNameUsePid()) { fileName += ".pid" + PidUtil.getPid(); } Handler handler = null; try { handler = new DateFileLogHandler(fileName + ".%d", 1024 * 1024 * 200, 4, true); handler.setFormatter(formatter); handler.setEncoding(LOG_CHARSET); } catch (IOException e) { e.printStackTrace(); } if (handler != null) { LoggerUtils.disableOtherHandlers(heliumRecordLog, handler); } heliumRecordLog.setLevel(Level.ALL); return handler; } }
/** * Triggers a push action to output all buffered records to the target handler, * and the target handler will publish them. Then the buffer is cleared. */ public void push() { for (int i = cursor; i < size; i++) { if (buffer[i] != null) { target.publish(buffer[i]); } buffer[i] = null; } for (int i = 0; i < cursor; i++) { if (buffer[i] != null) { target.publish(buffer[i]); } buffer[i] = null; } cursor = 0; }
private static Level getBaseLogLevel(Class<? extends Handler> handlerClass) { for (Handler handler : getBaseLogger().getHandlers()) { if (handlerClass.isInstance(handler)) { return handler.getLevel(); } } return Level.OFF; }
@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 testTraceEnhancedLogEntry() { expect(options.getProjectId()).andReturn(PROJECT).anyTimes(); expect(options.getService()).andReturn(logging); MonitoredResource resource = MonitoredResource.of("custom", ImmutableMap.<String, String>of()); logging.setFlushSeverity(Severity.ERROR); expectLastCall().once(); logging.setWriteSynchronicity(Synchronicity.ASYNC); expectLastCall().once(); logging.write( ImmutableList.of(TRACE_ENTRY), WriteOption.logName(LOG_NAME), WriteOption.resource(resource), WriteOption.labels(BASE_SEVERITY_MAP)); expectLastCall().once(); replay(options, logging); LoggingEnhancer enhancer = new TraceLoggingEnhancer(); TraceLoggingEnhancer.setCurrentTraceId("projects/projectId/traces/traceId"); Handler handler = new LoggingHandler(LOG_NAME, options, resource, Collections.singletonList(enhancer)); handler.setLevel(Level.ALL); handler.setFormatter(new TestFormatter()); handler.publish(newLogRecord(Level.FINEST, MESSAGE)); }