Java源码示例:org.jboss.logmanager.Level

示例1
private void log(Level level, Object message, Throwable t, Object... params) {
    if (!logger.isLoggable(level)) {
        return;
    }
    String msg = (message == null) ? "NULL" : message.toString();
    LogRecord record = new LogRecord(level, msg);
    record.setLoggerName(logger.getName());
    if (t != null) {
        record.setThrown(t);
    } else if (params != null && params.length != 0 && params[params.length - 1] instanceof Throwable) {
        // The exception may be the last parameters (SLF4J uses this convention).
        // As the logger can be used in a SLF4J style, we need to check
        record.setThrown((Throwable) params[params.length - 1]);
    }
    record.setSourceClassName(null);
    record.setParameters(params);
    logger.log(record);
}
 
示例2
@Test
public void testFormat() throws Exception {
    final JsonFormatter formatter = new JsonFormatter();
    formatter.setPrintDetails(true);
    ExtLogRecord record = createLogRecord("Test formatted %s", "message");
    compare(record, formatter);

    record = createLogRecord("Test Message");
    compare(record, formatter);

    record = createLogRecord(Level.ERROR, "Test formatted %s", "message");
    record.setLoggerName("org.jboss.logmanager.ext.test");
    record.setMillis(System.currentTimeMillis());
    final Throwable t = new RuntimeException("Test cause exception");
    final Throwable dup = new IllegalStateException("Duplicate");
    t.addSuppressed(dup);
    final Throwable cause = new RuntimeException("Test Exception", t);
    dup.addSuppressed(cause);
    cause.addSuppressed(new IllegalArgumentException("Suppressed"));
    cause.addSuppressed(dup);
    record.setThrown(cause);
    record.putMdc("testMdcKey", "testMdcValue");
    record.setNdc("testNdc");
    formatter.setExceptionOutputType(JsonFormatter.ExceptionOutputType.DETAILED_AND_FORMATTED);
    compare(record, formatter);
}
 
示例3
@Test
public void testLogstashFormat() throws Exception {
    KEY_OVERRIDES.put(Key.TIMESTAMP, "@timestamp");
    final LogstashFormatter formatter = new LogstashFormatter();
    formatter.setPrintDetails(true);
    ExtLogRecord record = createLogRecord("Test formatted %s", "message");
    compareLogstash(record, formatter, 1);

    record = createLogRecord("Test Message");
    formatter.setVersion(2);
    compareLogstash(record, formatter, 2);

    record = createLogRecord(Level.ERROR, "Test formatted %s", "message");
    record.setLoggerName("org.jboss.logmanager.ext.test");
    record.setMillis(System.currentTimeMillis());
    record.setThrown(new RuntimeException("Test Exception"));
    record.putMdc("testMdcKey", "testMdcValue");
    record.setNdc("testNdc");
    compareLogstash(record, formatter, 2);
}
 
示例4
@Test
public void testFormat() throws Exception {
    final XmlFormatter formatter = new XmlFormatter();
    formatter.setPrintDetails(true);
    ExtLogRecord record = createLogRecord("Test formatted %s", "message");
    compare(record, formatter);

    record = createLogRecord("Test Message");
    compare(record, formatter);

    record = createLogRecord(Level.ERROR, "Test formatted %s", "message");
    record.setLoggerName("org.jboss.logmanager.ext.test");
    record.setMillis(System.currentTimeMillis());
    final Throwable t = new RuntimeException("Test cause exception");
    final Throwable dup = new IllegalStateException("Duplicate");
    t.addSuppressed(dup);
    final Throwable cause = new RuntimeException("Test Exception", t);
    dup.addSuppressed(cause);
    cause.addSuppressed(new IllegalArgumentException("Suppressed"));
    cause.addSuppressed(dup);
    record.setThrown(cause);
    record.putMdc("testMdcKey", "testMdcValue");
    record.setNdc("testNdc");
    formatter.setExceptionOutputType(JsonFormatter.ExceptionOutputType.DETAILED_AND_FORMATTED);
    compare(record, formatter);
}
 
示例5
@Override
public StdioContext getStdioContext() {
    final LogContext logContext = LogContext.getLogContext();
    final Logger root = logContext.getLogger(CommonAttributes.ROOT_LOGGER_NAME);
    StdioContext stdioContext = root.getAttachment(STDIO_CONTEXT_ATTACHMENT_KEY);
    if (stdioContext == null) {
        stdioContext = StdioContext.create(
                new NullInputStream(),
                new LoggingOutputStream(logContext.getLogger("stdout"), Level.INFO),
                new LoggingOutputStream(logContext.getLogger("stderr"), Level.ERROR)
        );
        final StdioContext appearing = root.attachIfAbsent(STDIO_CONTEXT_ATTACHMENT_KEY, stdioContext);
        if (appearing != null) {
            stdioContext = appearing;
        }
    }
    return stdioContext;
}
 
示例6
@Override
void writeLogItem(String formattedItem) throws IOException {
    boolean reconnect =  isReconnect();
    if (!reconnect) {
        handler.publish(new ExtLogRecord(Level.WARN, formattedItem, SyslogAuditLogHandler.class.getName()));
        errorManager.getAndThrowError();
    } else {
        ControllerLogger.MGMT_OP_LOGGER.attemptingReconnectToSyslog(name, reconnectTimeout);
        try {
            // Reinitialise the delegating syslog handler if required, if we're already connected we don't need to
            // establish a new connection
            if (!connected) {
                stop();
                initialize();
            }
            handler.publish(new ExtLogRecord(Level.WARN, formattedItem, SyslogAuditLogHandler.class.getName()));
            errorManager.getAndThrowError();
            lastErrorTime = -1;
        } catch (Exception e) {
            // A failure has occurred and initialization should be reattempted
            connected = false;
            lastErrorTime = System.currentTimeMillis();
            errorManager.throwAsIoOrRuntimeException(e);
        }
    }
}
 
示例7
private static ExecutionBuilder getExecutionBuilderFromRMIRegistry()
{
    try
    {
        Registry registry = LocateRegistry.getRegistry(PORT);
        ExecutionBuilder executionBuilder = (ExecutionBuilder) registry.lookup(ExecutionBuilder.LOOKUP_NAME);
        executionBuilder.clear();
        return executionBuilder;
    }
    catch (RemoteException | NotBoundException e)
    {
        LOG.log(Level.SEVERE, e.getMessage(), e);
        e.printStackTrace();
    }
    return null;
}
 
示例8
private static void compare(final ExtLogRecord record, final JsonObject json, final Map<String, String> metaData) {
    Assert.assertEquals(record.getLevel(), Level.parse(getString(json, Key.LEVEL)));
    Assert.assertEquals(record.getLoggerClassName(), getString(json, Key.LOGGER_CLASS_NAME));
    Assert.assertEquals(record.getLoggerName(), getString(json, Key.LOGGER_NAME));
    compareMaps(record.getMdcCopy(), getMap(json, Key.MDC));
    Assert.assertEquals(record.getFormattedMessage(), getString(json, Key.MESSAGE));
    Assert.assertEquals(
            new SimpleDateFormat(StructuredFormatter.DEFAULT_DATE_FORMAT).format(new Date(record.getMillis())),
            getString(json, Key.TIMESTAMP));
    Assert.assertEquals(record.getNdc(), getString(json, Key.NDC));
    // Assert.assertEquals(record.getResourceBundle());
    // Assert.assertEquals(record.getResourceBundleName());
    // Assert.assertEquals(record.getResourceKey());
    Assert.assertEquals(record.getSequenceNumber(), getLong(json, Key.SEQUENCE));
    Assert.assertEquals(record.getSourceClassName(), getString(json, Key.SOURCE_CLASS_NAME));
    Assert.assertEquals(record.getSourceFileName(), getString(json, Key.SOURCE_FILE_NAME));
    Assert.assertEquals(record.getSourceLineNumber(), getInt(json, Key.SOURCE_LINE_NUMBER));
    Assert.assertEquals(record.getSourceMethodName(), getString(json, Key.SOURCE_METHOD_NAME));
    Assert.assertEquals(record.getThreadID(), getInt(json, Key.THREAD_ID));
    Assert.assertEquals(record.getThreadName(), getString(json, Key.THREAD_NAME));
    if (metaData != null) {
        for (String key : metaData.keySet()) {
            Assert.assertEquals(metaData.get(key), json.getString(key));
        }
    }
    // TODO (jrp) stack trace should be validated
}
 
示例9
private static void clearLogContext() {
    // Remove the configurator and clear the log context
    final Configurator configurator = EMBEDDED_LOG_CONTEXT.getLogger("").detach(Configurator.ATTACHMENT_KEY);
    // If this was a PropertyConfigurator we can use the LogContextConfiguration API to tear down the LogContext
    if (configurator instanceof PropertyConfigurator) {
        final LogContextConfiguration logContextConfiguration = ((PropertyConfigurator) configurator).getLogContextConfiguration();
        clearLogContext(logContextConfiguration);
    } else if (configurator instanceof LogContextConfiguration) {
        clearLogContext((LogContextConfiguration) configurator);
    } else {
        // Remove all the handlers and close them as well as reset the loggers
        final List<String> loggerNames = Collections.list(EMBEDDED_LOG_CONTEXT.getLoggerNames());
        for (String name : loggerNames) {
            final Logger logger = EMBEDDED_LOG_CONTEXT.getLoggerIfExists(name);
            if (logger != null) {
                final Handler[] handlers = logger.clearHandlers();
                if (handlers != null) {
                    for (Handler handler : handlers) {
                        handler.close();
                    }
                }
                logger.setFilter(null);
                logger.setUseParentFilters(false);
                logger.setUseParentHandlers(true);
                logger.setLevel(Level.INFO);
            }
        }
    }
}
 
示例10
/**
 * The main method.
 *
 * @param args the command-line arguments
 */
public static void main(String[] args) throws IOException {
    MDC.put("process", "host controller");


    // Grab copies of our streams.
    final InputStream in = System.in;
    //final PrintStream out = System.out;
    //final PrintStream err = System.err;

    byte[] authKey = new byte[ProcessController.AUTH_BYTES_ENCODED_LENGTH];
    try {
        StreamUtils.readFully(new Base64InputStream(System.in), authKey);
    } catch (IOException e) {
        STDERR.println(HostControllerLogger.ROOT_LOGGER.failedToReadAuthenticationKey(e));
        fail();
        return;
    }

    // Make sure our original stdio is properly captured.
    try {
        Class.forName(ConsoleHandler.class.getName(), true, ConsoleHandler.class.getClassLoader());
    } catch (Throwable ignored) {
    }

    // Install JBoss Stdio to avoid any nasty crosstalk.
    StdioContext.install();
    final StdioContext context = StdioContext.create(
        new NullInputStream(),
        new LoggingOutputStream(Logger.getLogger("stdout"), Level.INFO),
        new LoggingOutputStream(Logger.getLogger("stderr"), Level.ERROR)
    );
    StdioContext.setStdioContextSelector(new SimpleStdioContextSelector(context));

    create(args, new String(authKey, Charset.forName("US-ASCII")));

    while (in.read() != -1) {}
    exit();
}
 
示例11
/**
 * Attempts to clear the global log context used for embedded servers.
 */
static synchronized void clearLogContext() {
    final LogContext embeddedLogContext = Holder.LOG_CONTEXT;
    // Remove the configurator and clear the log context
    final Configurator configurator = embeddedLogContext.getLogger("").detach(Configurator.ATTACHMENT_KEY);
    // If this was a PropertyConfigurator we can use the LogContextConfiguration API to tear down the LogContext
    if (configurator instanceof PropertyConfigurator) {
        final LogContextConfiguration logContextConfiguration = ((PropertyConfigurator) configurator).getLogContextConfiguration();
        clearLogContext(logContextConfiguration);
    } else if (configurator instanceof LogContextConfiguration) {
        clearLogContext((LogContextConfiguration) configurator);
    } else {
        // Remove all the handlers and close them as well as reset the loggers
        final List<String> loggerNames = Collections.list(embeddedLogContext.getLoggerNames());
        for (String name : loggerNames) {
            final Logger logger = embeddedLogContext.getLoggerIfExists(name);
            if (logger != null) {
                final Handler[] handlers = logger.clearHandlers();
                if (handlers != null) {
                    for (Handler handler : handlers) {
                        handler.close();
                    }
                }
                logger.setFilter(null);
                logger.setUseParentFilters(false);
                logger.setUseParentHandlers(true);
                logger.setLevel(Level.INFO);
            }
        }
    }
}
 
示例12
@Override
public void debug(String message) {
    logger.log(Level.DEBUG, message);
}
 
示例13
@Override
public void error(String message, Throwable t) {
    logger.log(Level.ERROR, message, t);
}
 
示例14
@Override
public boolean isWarnEnabled() {
    return logger.isLoggable(Level.WARN);
}
 
示例15
public boolean isInfoEnabled() {
    return logger.isLoggable(Level.INFO);
}
 
示例16
public boolean isDebugEnabled() {
    return logger.isLoggable(Level.DEBUG);
}
 
示例17
public boolean isTraceEnabled() {
    return logger.isLoggable(Level.TRACE);
}
 
示例18
public void fatal(final Object message) {
    log(Level.FATAL, message);
}
 
示例19
public void fatal(final Object message, final Throwable t) {
    log(Level.FATAL, message, t);
}
 
示例20
public void error(final Object message) {
    log(Level.ERROR, message);
}
 
示例21
@Override
public void error(Object message, Object... params) {
    log(Level.ERROR, message, null, params);
}
 
示例22
public void error(final Object message, final Throwable t) {
    log(Level.ERROR, message, t);
}
 
示例23
@Override
public void error(Object message, Throwable t, Object... params) {
    log(Level.ERROR, message, t, params);
}
 
示例24
public void warn(final Object message) {
    log(Level.WARN, message);
}
 
示例25
@Override
public void warn(Object message, Object... params) {
    log(Level.WARN, message, null, params);
}
 
示例26
public void warn(final Object message, final Throwable t) {
    log(Level.WARN, message, t);
}
 
示例27
@Override
public void warn(Object message, Throwable t, Object... params) {
    log(Level.WARN, message, t, params);
}
 
示例28
public void info(final Object message) {
    log(Level.INFO, message);
}
 
示例29
@Override
public void info(Object message, Object... params) {
    log(Level.INFO, message, null, params);
}
 
示例30
public void info(final Object message, final Throwable t) {
    log(Level.INFO, message, t);
}