@Override public void setFormatter(Formatter newFormatter) throws SecurityException { super.setFormatter(newFormatter); Handler t = resolve(); if(t!=null) t.setFormatter(newFormatter); }
@Override public void setFormatter(Formatter newFormatter) { super.setFormatter(newFormatter); if (handler != null) { handler.setFormatter(newFormatter); } }
public static void main(String[] args) { Logger logger = Logger.getLogger("MyLog"); FileHandler fh; try { // This block configure the logger with handler and formatter fh = new FileHandler("C:/temp/test/MyLogFile.log"); logger.addHandler(fh); SimpleFormatter formatter = new SimpleFormatter(); fh.setFormatter(formatter); // the following statement is used to log any messages logger.info("My first log"); } catch (SecurityException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } logger.info("Hi How r u?"); }
public static Logger setConsoleHandler() { Logger logger = Logger.getLogger(""); Handler [] hs = logger.getHandlers(); for (int i = 0; i < hs.length; i++) { Handler h = hs[0]; if (h instanceof ConsoleHandler) { h.setFormatter(new OneLineSimpleLogger()); } } return logger; }
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()); }
logger.setLevel(Level.ALL); } else { handler.setFormatter(new Formatter() { @Override public String format(LogRecord record) {
public static void main(String[] args) { Client client = new Client(); // Set the logger to use our all-on-one-line formatter. Logger l = Logger.getLogger(""); Handler[] hs = l.getHandlers(); for (int i = 0; i < hs.length; i++) { Handler h = hs[0]; if (h instanceof ConsoleHandler) { h.setFormatter(client.new OneLineSimpleLogger()); } } try { client.execute(args); } catch (Exception e) { e.printStackTrace(); } }
/** * Adds a console-log handler writing to the given file. * * @param logfile the file path */ public void startFile(String logfile) { File parent = new File(logfile).getParentFile(); if (!parent.isDirectory() && !parent.mkdirs()) { logger.warning("Could not create log folder: " + parent); } Handler fileHandler = new RotatingFileHandler(logfile); FILE_DATE = server.getConsoleLogDateFormat(); fileHandler.setFormatter(new DateOutputFormatter(FILE_DATE, false)); logger.addHandler(fileHandler); }
public static void main(String[] args){ JmeFormatter formatter = new JmeFormatter(); Handler consoleHandler = new ConsoleHandler(); consoleHandler.setFormatter(formatter); Logger.getLogger("").removeHandler(Logger.getLogger("").getHandlers()[0]); Logger.getLogger("").addHandler(consoleHandler); createCanvas(appClass); try { Thread.sleep(500); } catch (InterruptedException ex) { } SwingUtilities.invokeLater(new Runnable(){ public void run(){ JPopupMenu.setDefaultLightWeightPopupEnabled(false); createFrame(); currentPanel.add(canvas, BorderLayout.CENTER); frame.pack(); startApp(); frame.setLocationRelativeTo(null); frame.setVisible(true); } }); }
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); }
/** * Get a logger to a distinguished file, job.log in the job's * directory, into which job-specific events may be reported. * * @return Logger writing to the job-specific log */ public Logger getJobLogger() { if(jobLogger == null) { jobLogger = Logger.getLogger(getShortName()); try { mainJobLogHandler = new FileHandler(getJobLog().getAbsolutePath(),true); mainJobLogHandler.setFormatter(new JobLogFormatter()); jobLogger.addHandler(mainJobLogHandler); } catch (SecurityException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } jobLogger.setLevel(Level.INFO); } return jobLogger; }
@Test(groups = {"currentold"}) public void logger() { Logger logger = Logger.getLogger(""); for (Handler handler : logger.getHandlers()) { handler.setLevel(Level.WARNING); handler.setFormatter(new org.testng.log.TextFormatter()); } logger.setLevel(Level.SEVERE); } }
/** * Start the context, catching and reporting any BeansExceptions. */ protected synchronized void startContext() { try { ac.start(); // job log file covering just this launch getJobLogger().removeHandler(currentLaunchJobLogHandler); File f = new File(ac.getCurrentLaunchDir(), "job.log"); currentLaunchJobLogHandler = new FileHandler(f.getAbsolutePath(), true); currentLaunchJobLogHandler.setFormatter(new JobLogFormatter()); getJobLogger().addHandler(currentLaunchJobLogHandler); } catch (BeansException be) { doTeardown(); beansException(be); } catch (Exception e) { LOGGER.log(Level.SEVERE,e.getClass().getSimpleName()+": "+e.getMessage(),e); try { doTeardown(); } catch (Exception e2) { e2.printStackTrace(System.err); } } }
/** * Starts the console. * * @param jline whether the console should use JLine */ public void startConsole(boolean jline) { this.jline = jline; if (jline) { setupColors(); } sender = new ColoredCommandSender(); CONSOLE_DATE = server.getConsoleDateFormat(); for (Handler handler : logger.getHandlers()) { if (handler.getClass() == FancyConsoleHandler.class) { handler.setFormatter(new DateOutputFormatter(CONSOLE_DATE, true)); } } CONSOLE_PROMPT = server.getConsolePrompt(); Thread thread = new ConsoleCommandThread(); thread.setName("ConsoleCommandThread"); thread.setDaemon(true); thread.start(); }
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; } }
@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 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)); }
@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)); }
@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(); } }