PrefixLogger(final ExtendedLogger logger, final String name, final String prefix) { super(logger, name, null); final String actualPrefix = (prefix == null ? "" : prefix).intern(); final Marker actualMarker; // markers is not thread-safe, so we synchronize access synchronized (markers) { final WeakReference<Marker> marker = markers.get(actualPrefix); final Marker maybeMarker = marker == null ? null : marker.get(); if (maybeMarker == null) { actualMarker = new MarkerManager.Log4jMarker(actualPrefix); markers.put(actualPrefix, new WeakReference<>(actualMarker)); } else { actualMarker = maybeMarker; } } this.marker = actualMarker; }
@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; }
@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); }
@Before public void setUp() { this.messageFactory = new ParameterizedMessageFactory(); this.mockedLogger = mock(ExtendedLogger.class); this.mockedMarker = mock(Marker.class); when(this.mockedLogger.getMessageFactory()).thenReturn(this.messageFactory); when(this.mockedMarker.getName()).thenReturn("MARKER"); }
@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); }
@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; }
/** * 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; }
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); }
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); }
@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); }
@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 }
public static Logger getLogger(String prefix, Logger logger) { return new PrefixLogger((ExtendedLogger)logger, logger.getName(), prefix); }
public FastLogger(final Logger logger) { this((ExtendedLogger) logger, logger.getName(), logger.getMessageFactory()); }
public FastLogger(final ExtendedLogger logger, final String name, final MessageFactory messageFactory) { super(logger, name, messageFactory); }
public ExtendedLogger getExtendedLogger() { return super.logger; }
Log4J2Logger(Logger logger) { super((ExtendedLogger) logger, logger.getName(), logger.getMessageFactory()); }
@Override public ExtendedLogger getLogger(String name) { return getLogger(name, null); }
@Override public ExtendedLogger getLogger(String name, MessageFactory messageFactory) { return loggers.computeIfAbsent(name, key -> new ESLogger(key, messageFactory, (LoggerImpl) LoggerFactory.getLogger(key))); }
public Log4j2Impl(final Logger logger) { Assert.notNull(logger); this.logger = (ExtendedLogger) logger; setLoggerName(logger.getName()); }
public Log4j2Impl(final String loggerName) { Assert.hasText(loggerName); logger = (ExtendedLogger) LogManager.getLogger(loggerName); setLoggerName(loggerName); }
/** * Creates a new file-logger for an addOn. The logger will log to a file with the addOnId as name in the logs folder * of Izou * * @param addOnId the Id of the addOn the logger is created for * @param level level of logger (at what level of log the logger should be activated * @return the new logger */ public synchronized ExtendedLogger createFileLogger(String addOnId, String level) { try { LoggerContext ctx = LogManager.getContext(false); Configuration config = ((org.apache.logging.log4j.core.LoggerContext) ctx).getConfiguration(); //creates a new pattern layout (what determines how the log is formated, i.e. date, thread etc.) Layout layout = PatternLayout.createLayout("%d %-5p [%t] %C{10} (%F:%L) - %m%n", config, null, null, true, false, null, null); //creates a file appender for the logger (so that it knows what file to log to) Appender fileAppender = FileAppender.createAppender("logs" + File.separator + addOnId + ".log", "true", "false", "file", "true", "false", "false", "4000", layout, null, "false", null, config); fileAppender.start(); config.addAppender(fileAppender); //creates also a console appender for the logger (so that the logger also outputs the log in the console) Appender consoleAppender = ConsoleAppender.createAppender(layout, null, "SYSTEM_OUT", "console", null, null); consoleAppender.start(); config.addAppender(consoleAppender); //adds appenders to an array called refs. It will later serve as references to the logger as to what // appenders it has AppenderRef fileRef = AppenderRef.createAppenderRef("file", Level.DEBUG, null); AppenderRef consoleRef = AppenderRef.createAppenderRef("console", Level.DEBUG, null); AppenderRef[] refs = new AppenderRef[]{fileRef, consoleRef}; //creates the logger configurations for the logger, where the appender-references are also added LoggerConfig loggerConfig = LoggerConfig.createLogger("false", Level.DEBUG, addOnId, "true", refs, null, config, null); loggerConfig.addAppender(fileAppender, Level.DEBUG, null); loggerConfig.addAppender(consoleAppender, Level.DEBUG, null); //finally creates the logger and returns it config.addLogger(addOnId, loggerConfig); ((org.apache.logging.log4j.core.LoggerContext) ctx).updateLoggers(); ctx.getLogger(addOnId); ExtendedLogger logger = ctx.getLogger(addOnId); return logger; } catch(Exception e) { fileLogger.error("Unable to create FileLogger",e); return null; } }
ApimanLog4j2LogDelegate(final String name) { logger = (ExtendedLogger) org.apache.logging.log4j.LogManager.getLogger(name, new FormattedMessageFactory()); }
@Override public ExtendedLogger getLogger(final String name) { return getLogger(name, null); }
@Override public ExtendedLogger getLogger(final String name, final MessageFactory messageFactory) { return new TestLogger(name, messageFactory); }
public static ExtendedLogger getLogger(final String name) { return getContext().getLogger(name); }
public Log4jTaglibLogger(final ExtendedLogger logger, final String name, final MessageFactory messageFactory) { super(logger, name, messageFactory); }
protected LoggerOutputStream(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 == null ? FQCN : fqcn; }
protected LoggerWriter(final ExtendedLogger logger, final String fqcn, final Level level, final Marker marker) { this.logger = new CharStreamLogger(logger, level, marker); this.fqcn = fqcn == null ? FQCN : fqcn; }
protected LoggerFilterOutputStream(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 == null ? FQCN : fqcn; }
protected LoggerBufferedInputStream(final InputStream in, final Charset charset, final ExtendedLogger logger, final String fqcn, final Level level, final Marker marker) { super(new LoggerInputStream(in, charset, logger, fqcn == null ? FQCN : fqcn, level, marker)); }
protected LoggerBufferedInputStream(final InputStream in, final Charset charset, final int size, final ExtendedLogger logger, final String fqcn, final Level level, final Marker marker) { super(new LoggerInputStream(in, charset, logger, fqcn == null ? FQCN : fqcn, level, marker), size); }
protected LoggerBufferedReader(final Reader reader, final ExtendedLogger logger, final String fqcn, final Level level, final Marker marker) { super(new LoggerReader(reader, logger, fqcn == null ? FQCN : fqcn, level, marker)); }
protected LoggerBufferedReader(final Reader reader, final int size, final ExtendedLogger logger, final String fqcn, final Level level, final Marker marker) { super(new LoggerReader(reader, logger, fqcn == null ? FQCN : fqcn, level, marker), size); }
protected LoggerInputStream(final InputStream in, final Charset charset, final ExtendedLogger logger, final String fqcn, final Level level, final Marker marker) { super(in); this.logger = new ByteStreamLogger(logger, level, marker, charset); this.fqcn = fqcn == null ? FQCN : fqcn; }
protected LoggerFilterWriter(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 == null ? FQCN : fqcn; }
protected LoggerReader(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 == null ? FQCN : fqcn; }
protected LoggerPrintWriter(final ExtendedLogger logger, final boolean autoFlush, final String fqcn, final Level level, final Marker marker) { super(new LoggerWriter(logger, fqcn == null ? FQCN : fqcn, level, marker), autoFlush); }
protected LoggerPrintWriter(final Writer writer, final boolean autoFlush, final ExtendedLogger logger, final String fqcn, final Level level, final Marker marker) { super(new LoggerFilterWriter(writer, logger, fqcn == null ? FQCN : fqcn, level, marker), autoFlush); }