Search in sources :

Example 16 with Formatter

use of java.util.logging.Formatter in project ignite by apache.

the class GridCommandHandlerDefragmentationTest method createCommandHandler.

/**
 */
private CommandHandler createCommandHandler(ListeningTestLogger testLog) {
    Logger log = CommandHandler.initLogger(null);
    log.addHandler(new StreamHandler(System.out, new Formatter() {

        /**
         * {@inheritDoc}
         */
        @Override
        public String format(LogRecord record) {
            String msg = record.getMessage();
            testLog.info(msg);
            return msg + "\n";
        }
    }));
    return new CommandHandler(log);
}
Also used : LogRecord(java.util.logging.LogRecord) Formatter(java.util.logging.Formatter) StreamHandler(java.util.logging.StreamHandler) CommandHandler(org.apache.ignite.internal.commandline.CommandHandler) ListeningTestLogger(org.apache.ignite.testframework.ListeningTestLogger) Logger(java.util.logging.Logger)

Example 17 with Formatter

use of java.util.logging.Formatter in project buck by facebook.

the class JavaUtilsLoggingBuildListener method ensureLogFileIsWritten.

public static void ensureLogFileIsWritten(ProjectFilesystem filesystem) {
    if (!filesystem.exists(filesystem.getBuckPaths().getScratchDir())) {
        try {
            filesystem.mkdirs(filesystem.getBuckPaths().getScratchDir());
        } catch (IOException e) {
            throw new HumanReadableException(e, "Unable to create output directory: %s: %s: %s", filesystem.getBuckPaths().getScratchDir(), e.getClass(), e.getMessage());
        }
    }
    try {
        FileHandler handler = new FileHandler(filesystem.resolve(filesystem.getBuckPaths().getScratchDir()).resolve("build.log").toString(), /* append */
        false);
        Formatter formatter = new BuildEventFormatter();
        handler.setFormatter(formatter);
        LOG.setUseParentHandlers(false);
        LOG.addHandler(handler);
        LOG.setLevel(LEVEL);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : HumanReadableException(com.facebook.buck.util.HumanReadableException) Formatter(java.util.logging.Formatter) IOException(java.io.IOException) FileHandler(java.util.logging.FileHandler)

Example 18 with Formatter

use of java.util.logging.Formatter in project cogtool by cogtool.

the class CogTool method enableLogging.

public static void enableLogging(boolean value) {
    if (value) {
        logger.setLevel(Level.ALL);
        for (Handler h : logger.getHandlers()) {
            if (h instanceof FileHandler) {
                return;
            }
        }
        FileHandler fh = null;
        try {
            fh = new FileHandler((CogToolPref.LOG_DIRECTORY.getString() + LOG_FILE_PATTERN), LOG_FILE_MAX_SIZE, LOG_FILES_MAX_COUNT, true);
        } catch (IOException ex) {
            logger.warning("Couldn't create log file: " + ex);
            return;
        }
        fh.setFormatter(new Formatter() {

            @Override
            public String format(LogRecord r) {
                long ms = r.getMillis();
                return String.format("%tF %tT.%tL\t%s\n", ms, ms, ms, r.getMessage());
            }
        });
        logger.addHandler(fh);
    } else {
        logger.setLevel(DEFAULT_LOG_LEVEL);
        for (Handler h : logger.getHandlers()) {
            if (!(h instanceof ConsoleHandler)) {
                logger.removeHandler(h);
            }
        }
    }
}
Also used : LogRecord(java.util.logging.LogRecord) Formatter(java.util.logging.Formatter) FileHandler(java.util.logging.FileHandler) RcvrExceptionHandler(edu.cmu.cs.hcii.cogtool.ui.RcvrExceptionHandler) ConsoleHandler(java.util.logging.ConsoleHandler) Handler(java.util.logging.Handler) IOException(java.io.IOException) ConsoleHandler(java.util.logging.ConsoleHandler) FileHandler(java.util.logging.FileHandler)

Example 19 with Formatter

use of java.util.logging.Formatter in project j2objc by google.

the class MemoryHandlerTest method testClose.

/*
	public void testSecurity() {
		SecurityManager currentManager = System.getSecurityManager();
		System.setSecurityManager(securityManager);
		try {
			try {
				handler.close();
				fail("should throw security exception");
			} catch (SecurityException e) {
			}
			try {
				handler.setPushLevel(Level.CONFIG);
				fail("should throw security exception");
			} catch (SecurityException e) {
			}
			handler.flush();
			handler.push();
			handler.getPushLevel();
			handler.isLoggable(new LogRecord(Level.ALL, "message"));
			handler.publish(new LogRecord(Level.ALL, "message"));
		} finally {
			System.setSecurityManager(currentManager);
		}

	}
	*/
public void testClose() {
    Filter filter = handler.getFilter();
    Formatter formatter = handler.getFormatter();
    writer.getBuffer().setLength(0);
    handler.close();
    assertEquals(writer.toString(), "close");
    assertEquals(handler.getFilter(), filter);
    assertEquals(handler.getFormatter(), formatter);
    assertNull(handler.getEncoding());
    assertNotNull(handler.getErrorManager());
    assertEquals(handler.getLevel(), Level.OFF);
    assertEquals(handler.getPushLevel(), Level.WARNING);
    assertFalse(handler.isLoggable(new LogRecord(Level.SEVERE, "test")));
}
Also used : Filter(java.util.logging.Filter) LogRecord(java.util.logging.LogRecord) SimpleFormatter(java.util.logging.SimpleFormatter) Formatter(java.util.logging.Formatter)

Example 20 with Formatter

use of java.util.logging.Formatter in project j2objc by google.

the class HandlerTest method testGetSetFormatter_Normal.

/*
	 * Test setFilter with insufficient privilege.
	 *
	public void testSetFilter_InsufficientPrivilege() throws Exception {
		MockHandler h = new MockHandler();
		SecurityManager oldMan = System.getSecurityManager();
		System.setSecurityManager(new MockSecurityManager());

		// set null
		try {

			h.setFilter(null);
			fail("Should throw SecurityException!");
		} catch (SecurityException e) {
		} finally {
			System.setSecurityManager(oldMan);
		}
		// set a normal value
		System.setSecurityManager(new MockSecurityManager());
		try {

			h.setFilter(new MockFilter());
			fail("Should throw SecurityException!");
		} catch (SecurityException e) {
		} finally {
			System.setSecurityManager(oldMan);
		}
	}
	*/
/*
	 * Test getFormatter & setFormatter methods with non-null value.
	 */
public void testGetSetFormatter_Normal() throws Exception {
    MockHandler h = new MockHandler();
    Formatter f = new SimpleFormatter();
    h.setFormatter(f);
    assertSame(f, h.getFormatter());
}
Also used : SimpleFormatter(java.util.logging.SimpleFormatter) Formatter(java.util.logging.Formatter) SimpleFormatter(java.util.logging.SimpleFormatter)

Aggregations

Formatter (java.util.logging.Formatter)44 LogRecord (java.util.logging.LogRecord)18 File (java.io.File)13 SimpleFormatter (java.util.logging.SimpleFormatter)12 IOException (java.io.IOException)11 Logger (java.util.logging.Logger)11 FileHandler (java.util.logging.FileHandler)10 Handler (java.util.logging.Handler)9 Date (java.util.Date)6 Test (org.junit.Test)6 SimpleDateFormat (java.text.SimpleDateFormat)5 ConsoleHandler (java.util.logging.ConsoleHandler)5 Config (edu.neu.ccs.pyramid.configuration.Config)4 JSONLogFormatter (fish.payara.enterprise.server.logging.JSONLogFormatter)4 Level (java.util.logging.Level)4 StreamHandler (java.util.logging.StreamHandler)4 Pair (edu.neu.ccs.pyramid.util.Pair)3 DateFormat (java.text.DateFormat)3 ErrorManager (java.util.logging.ErrorManager)3 Filter (java.util.logging.Filter)3