org.apache.logging.log4j.spi.ExtendedLogger Java Examples

The following examples show how to use org.apache.logging.log4j.spi.ExtendedLogger. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Log4j2Jira1688Test.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@Test
public void testLog4j2Only() throws InterruptedException {
    final org.apache.logging.log4j.Logger log4JLogger = LogManager.getLogger(this.getClass());
    final int limit = 11; // more than unrolled varargs
    final Object[] args = createArray(limit);
    final Object[] originalArgs = Arrays.copyOf(args, args.length);

    listAppender.countDownLatch = new CountDownLatch(1);
    ((ExtendedLogger)log4JLogger).logIfEnabled("test", Level.ERROR, null, "test {}", args);

    listAppender.countDownLatch.await(1, TimeUnit.SECONDS);
    Assert.assertArrayEquals(Arrays.toString(args), originalArgs, args);

    ((ExtendedLogger)log4JLogger).logIfEnabled("test", Level.ERROR, null, "test {}", args);
    Assert.assertArrayEquals(Arrays.toString(args), originalArgs, args);
}
 
Example #2
Source File: Log4j2Jira1688AsyncTest.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@Test
public void testLog4j2Only() throws InterruptedException {
    final org.apache.logging.log4j.Logger log4JLogger = LogManager.getLogger(this.getClass());
    final int limit = 11; // more than unrolled varargs
    final Object[] args = createArray(limit);
    final Object[] originalArgs = Arrays.copyOf(args, args.length);

    listAppender.countDownLatch = new CountDownLatch(1);
    ((ExtendedLogger)log4JLogger).logIfEnabled("test", Level.ERROR, null, "test {}", args);

    listAppender.countDownLatch.await(1, TimeUnit.SECONDS);
    Assert.assertArrayEquals(Arrays.toString(args), originalArgs, args);

    ((ExtendedLogger)log4JLogger).logIfEnabled("test", Level.ERROR, null, "test {}", args);
    Assert.assertArrayEquals(Arrays.toString(args), originalArgs, args);
}
 
Example #3
Source File: Log4jTaglibLoggerContext.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@Override
public Log4jTaglibLogger getLogger(final String name, final MessageFactory messageFactory) {
    // Note: This is the only method where we add entries to the 'loggerRegistry' ivar.
    Log4jTaglibLogger logger = this.loggerRegistry.getLogger(name, messageFactory);
    if (logger != null) {
        AbstractLogger.checkMessageFactory(logger, messageFactory);
        return logger;
    }

    synchronized (this.loggerRegistry) {
        logger = this.loggerRegistry.getLogger(name, messageFactory);
        if (logger == null) {
            final LoggerContext context = LogManager.getContext(false);
            final ExtendedLogger original = messageFactory == null ?
                    context.getLogger(name) : context.getLogger(name, messageFactory);
            // wrap a logger from an underlying implementation
            logger = new Log4jTaglibLogger(original, name, original.getMessageFactory());
            this.loggerRegistry.putIfAbsent(name, messageFactory, logger);
        }
    }

    return logger;
}
 
Example #4
Source File: PrefixLogger.java    From crate with Apache License 2.0 6 votes vote down vote up
/**
 * Construct a prefix logger with the specified name and prefix.
 *
 * @param logger the extended logger to wrap
 * @param name   the name of this prefix logger
 * @param prefix the prefix for this prefix logger
 */
PrefixLogger(final ExtendedLogger logger, final String name, final String prefix) {
    super(logger, name, null);

    final String actualPrefix = (prefix == null ? "" : prefix);
    final Marker actualMarker;
    // markers is not thread-safe, so we synchronize access
    synchronized (MARKERS) {
        final Marker maybeMarker = MARKERS.get(actualPrefix);
        if (maybeMarker == null) {
            actualMarker = new MarkerManager.Log4jMarker(actualPrefix);
            /*
             * We must create a new instance here as otherwise the marker will hold a reference to the key in the weak hash map; as
             * those references are held strongly, this would give a strong reference back to the key preventing them from ever being
             * collected. This also guarantees that no other strong reference can be held to the prefix anywhere.
             */
            // noinspection RedundantStringConstructorCall
            MARKERS.put(new String(actualPrefix), actualMarker);
        } else {
            actualMarker = maybeMarker;
        }
    }
    this.marker = actualMarker;
}
 
Example #5
Source File: TestLoggerContext.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Override
 public ExtendedLogger getLogger(final String name) {
     final ExtendedLogger extendedLogger = map.get(name);
     if (extendedLogger != null) {
return extendedLogger;
     }
     final ExtendedLogger logger = new TestLogger(name);
     map.put(name, logger);
     return logger;
 }
 
Example #6
Source File: Category.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
private void maybeLog(final String fqcn, final org.apache.logging.log4j.Level level,
        final Object message, final Throwable throwable) {
    if (logger.isEnabled(level)) {
        if (logger instanceof ExtendedLogger) {
            ((ExtendedLogger) logger).logMessage(fqcn, level, null, new ObjectMessage(message), throwable);
        } else {
            logger.log(level, message, throwable);
        }
    }
}
 
Example #7
Source File: Category.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
public void forcedLog(final String fqcn, final Priority level, final Object message, final Throwable t) {
    final org.apache.logging.log4j.Level lvl = org.apache.logging.log4j.Level.toLevel(level.toString());
    ObjectRenderer renderer = get(message.getClass());
    final Message msg = message instanceof Message ? (Message) message : renderer != null ?
        new RenderedMessage(renderer, message) : new ObjectMessage(message);
    if (logger instanceof ExtendedLogger) {
        ((ExtendedLogger) logger).logMessage(fqcn, lvl, null, new ObjectMessage(message), t);
    } else {
        logger.log(lvl, msg, t);
    }
}
 
Example #8
Source File: Category.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
private void maybeLog(final String fqcn, final org.apache.logging.log4j.Level level,
        final Object message, final Throwable throwable) {
    if (logger.isEnabled(level)) {
        @SuppressWarnings("unchecked")
        Message msg = message instanceof Map ? new MapMessage((Map) message) : new ObjectMessage(message);
        if (logger instanceof ExtendedLogger) {
            ((ExtendedLogger) logger).logMessage(fqcn, level, null, msg, throwable);
        } else {
            logger.log(level, msg, throwable);
        }
    }
}
 
Example #9
Source File: IoBuilder.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs a new IoBuilder for the given Logger. This method is provided for extensibility of this builder
 * class. The static factory methods should be used normally.
 *
 * @param logger the {@link ExtendedLogger} to wrap
 */
protected IoBuilder(final Logger logger) {
    if (!(logger instanceof ExtendedLogger)) {
        throw new UnsupportedOperationException("The provided Logger [" + String.valueOf(logger) +
            "] does not implement " + ExtendedLogger.class.getName());
    }
    this.logger = (ExtendedLogger) logger;
}
 
Example #10
Source File: ByteStreamLogger.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
public ByteStreamLogger(final ExtendedLogger logger, final Level level, final Marker marker, final Charset charset) {
    this.logger = logger;
    this.level = level == null ? logger.getLevel() : level;
    this.marker = marker;
    this.reader = new InputStreamReader(new ByteBufferInputStream(),
        charset == null ? Charset.defaultCharset() : charset);
}
 
Example #11
Source File: SimpleLoggerContext.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Override
public ExtendedLogger getLogger(final String name, final MessageFactory messageFactory) {
    // Note: This is the only method where we add entries to the 'loggerRegistry' ivar.
    final ExtendedLogger extendedLogger = loggerRegistry.getLogger(name, messageFactory);
    if (extendedLogger != null) {
        AbstractLogger.checkMessageFactory(extendedLogger, messageFactory);
        return extendedLogger;
    }
    final SimpleLogger simpleLogger = new SimpleLogger(name, defaultLevel, showLogName, showShortName, showDateTime,
            showContextMap, dateTimeFormat, messageFactory, props, stream);
    loggerRegistry.putIfAbsent(name, messageFactory, simpleLogger);
    return loggerRegistry.getLogger(name, messageFactory);
}
 
Example #12
Source File: ApiLogger.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
ApiLogger(final ExtendedLogger logger) {
    super(logger.getName(), null);
    final Level javaLevel = LevelTranslator.toJavaLevel(logger.getLevel());
    // "java.util.logging.LoggingPermission" "control"
    AccessController.doPrivileged(new PrivilegedAction<Object>() {
        @Override
        public Object run() {
            ApiLogger.super.setLevel(javaLevel);
            return null;
        }
    });
    this.logger = new WrappedLogger(logger);
}
 
Example #13
Source File: Log4jBridgeHandler.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Override
public void publish(LogRecord record) {
    if (record == null) {    // silently ignore null records
        return;
    }

    org.apache.logging.log4j.Logger log4jLogger = getLog4jLogger(record);
    String msg = julFormatter.formatMessage(record);    // use JUL's implementation to get real msg
    /* log4j allows nulls:
    if (msg == null) {
        // JUL allows nulls, but other log system may not
        msg = "<null log msg>";
    } */
    org.apache.logging.log4j.Level log4jLevel = LevelTranslator.toLevel(record.getLevel());
    Throwable thrown = record.getThrown();
    if (log4jLogger instanceof ExtendedLogger) {
        // relevant for location information
        try {
            ((ExtendedLogger) log4jLogger).logIfEnabled(FQCN, log4jLevel, null, msg, thrown);
        } catch (NoClassDefFoundError e) {
            // sometimes there are problems with log4j.ExtendedStackTraceElement, so try a workaround
            log4jLogger.warn("Log4jBridgeHandler: ignored exception when calling 'ExtendedLogger': {}", e.toString());
            log4jLogger.log(log4jLevel, msg, thrown);
        }
    } else {
        log4jLogger.log(log4jLevel, msg, thrown);
    }
}
 
Example #14
Source File: PrefixPluginLogger.java    From elasticsearch-analysis-hanlp with Apache License 2.0 5 votes vote down vote up
PrefixPluginLogger(ExtendedLogger logger, String name, String prefix) {
    super(logger, name, null);
    String actualPrefix = prefix == null ? "" : prefix;
    MarkerManager.Log4jMarker actualMarker;
    synchronized (MARKERS) {
        MarkerManager.Log4jMarker maybeMarker = (MarkerManager.Log4jMarker)MARKERS.get(actualPrefix);
        if (maybeMarker == null) {
            actualMarker = new MarkerManager.Log4jMarker(actualPrefix);
            MARKERS.put(actualPrefix, actualMarker);
        } else {
            actualMarker = maybeMarker;
        }
    }
    this.marker = actualMarker;
}
 
Example #15
Source File: ESLoggerFactory.java    From crate with Apache License 2.0 5 votes vote down vote up
static Logger getLogger(String prefix, Logger logger) {
    /*
     * In a followup we'll throw an exception if prefix is null or empty
     * redirecting folks to LogManager.getLogger.
     *
     * This and more is tracked in https://github.com/elastic/elasticsearch/issues/32174
     */
    if (prefix == null || prefix.length() == 0) {
        return logger;
    }
    return new PrefixLogger((ExtendedLogger)logger, logger.getName(), prefix);
}
 
Example #16
Source File: SLF4JLoggerContext.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Override
public ExtendedLogger getLogger(final String name, final MessageFactory messageFactory) {
    // FIXME according to LOG4J2-1180, the below line should be:
    // FIXME if (!loggerRegistry.hasLogger(name, messageFactory)) {
    if (!loggerRegistry.hasLogger(name)) {
        // FIXME: should be loggerRegistry.putIfAbsent(name, messageFactory,
        loggerRegistry.putIfAbsent(name, null,
                new SLF4JLogger(name, messageFactory, LoggerFactory.getLogger(name)));
    }
    // FIXME should be return loggerRegistry.getLogger(name, messageFactory);
    return loggerRegistry.getLogger(name);

    // TODO applying the above fixes causes (log4j-to-slf4j) LoggerTest to fail
}
 
Example #17
Source File: SLF4JLoggerContext.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Override
public ExtendedLogger getLogger(final String name) {
    if (!loggerRegistry.hasLogger(name)) {
        loggerRegistry.putIfAbsent(name, null, new SLF4JLogger(name, LoggerFactory.getLogger(name)));
    }
    return loggerRegistry.getLogger(name);
}
 
Example #18
Source File: InternalFilterOutputStream.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
public InternalFilterOutputStream(final OutputStream out, final Charset charset, final ExtendedLogger logger,
                                   final String fqcn, final Level level, final Marker marker) {
    super(out);
    this.logger = new ByteStreamLogger(logger, level, marker, charset);
    this.fqcn = fqcn;
}
 
Example #19
Source File: CharStreamLogger.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
public CharStreamLogger(final ExtendedLogger logger, final Level level, final Marker marker) {
    this.logger = logger;
    this.level = level == null ? logger.getLevel() : level;
    this.marker = marker;
}
 
Example #20
Source File: WrappedLogger.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
WrappedLogger(final ExtendedLogger logger) {
    super(logger, logger.getName(), logger.getMessageFactory());
}
 
Example #21
Source File: InternalFilterWriter.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
public InternalFilterWriter(final Writer out, final ExtendedLogger logger, final String fqcn, final Level level,
                             final Marker marker) {
    super(out);
    this.logger = new CharStreamLogger(logger, level, marker);
    this.fqcn = fqcn;
}
 
Example #22
Source File: InternalBufferedInputStream.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
public InternalBufferedInputStream(final InputStream in, final Charset charset, final int size,
                                    final ExtendedLogger logger, final String fqcn, final Level level,
                                    final Marker marker) {
    super(new InternalInputStream(in, charset, logger, fqcn == null ? FQCN : fqcn, level, marker), size);
}
 
Example #23
Source File: InternalBufferedInputStream.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
public InternalBufferedInputStream(final InputStream in, final Charset charset, final ExtendedLogger logger,
                                    final String fqcn, final Level level, final Marker marker) {
    super(new InternalInputStream(in, charset, logger, fqcn == null ? FQCN : fqcn, level, marker));
}
 
Example #24
Source File: LoggerFilterWriter.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
protected LoggerFilterWriter(final Writer out, final ExtendedLogger logger, final String fqcn, final Level level,
                             final Marker marker) {
    super(out);
    this.logger = new InternalFilterWriter(out, logger, fqcn == null ? FQCN : fqcn, level, marker);
}
 
Example #25
Source File: InternalPrintStream.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
public InternalPrintStream(final OutputStream out, final boolean autoFlush, final Charset charset,
                            final ExtendedLogger logger, final String fqcn, final Level level, final Marker marker)
    throws UnsupportedEncodingException {
    super(new InternalFilterOutputStream(out, ensureNonNull(charset), logger, fqcn, level,
        marker), autoFlush, ensureNonNull(charset).name());
}
 
Example #26
Source File: InternalPrintStream.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
public InternalPrintStream(final ExtendedLogger logger, final boolean autoFlush, final Charset charset,
                            final String fqcn, final Level level, final Marker marker)
    throws UnsupportedEncodingException {
    super(new InternalOutputStream(logger, level, marker, ensureNonNull(charset), fqcn),
        autoFlush, ensureNonNull(charset).name());
}
 
Example #27
Source File: InternalPrintWriter.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
public InternalPrintWriter(final Writer writer, final boolean autoFlush, final ExtendedLogger logger,
                            final String fqcn, final Level level, final Marker marker) {
    super(new InternalFilterWriter(writer, logger, fqcn, level, marker), autoFlush);
}
 
Example #28
Source File: InternalPrintWriter.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
public InternalPrintWriter(final ExtendedLogger logger, final boolean autoFlush, final String fqcn,
                            final Level level, final Marker marker) {
    super(new InternalWriter(logger, fqcn, level, marker), autoFlush);
}
 
Example #29
Source File: InternalLoggerReader.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
public InternalLoggerReader(final Reader reader, final ExtendedLogger logger, final String fqcn, final Level level,
                       final Marker marker) {
    super(reader);
    this.logger = new CharStreamLogger(logger, level, marker);
    this.fqcn = fqcn;
}
 
Example #30
Source File: InternalOutputStream.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
public InternalOutputStream(final ExtendedLogger logger, final Level level, final Marker marker,
                             final Charset charset, final String fqcn) {
    this.logger = new ByteStreamLogger(logger, level, marker, charset);
    this.fqcn = fqcn;
}