Search in sources :

Example 1 with SimpleLogger

use of org.apache.logging.log4j.simple.SimpleLogger in project pyroscope-java by pyroscope-io.

the class PyroscopeAgent method premain.

public static void premain(final String agentArgs, final Instrumentation inst) {
    final Config config;
    final Logger logger;
    try {
        Profiler.init();
        config = Config.build();
        logger = new SimpleLogger("PyroscopeAgent", config.logLevel, false, true, true, false, "yyyy-MM-dd HH:mm:ss.SSS", ParameterizedNoReferenceMessageFactory.INSTANCE, new PropertiesUtil(new Properties()), System.err);
    } catch (final Throwable e) {
        PreConfigLogger.LOGGER.error("Error starting profiler", e);
        return;
    }
    logger.debug("Config {}", config);
    final OverfillQueue<Snapshot> pushQueue = new OverfillQueue<>(config.pushQueueCapacity);
    try {
        final Profiler profiler = new Profiler(logger, config.profilingEvent, config.profilingAlloc, config.profilingLock, config.profilingInterval, config.format);
        final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() {

            public Thread newThread(Runnable r) {
                Thread t = Executors.defaultThreadFactory().newThread(r);
                t.setDaemon(true);
                return t;
            }
        });
        profiler.start();
        final Runnable dumpProfile = () -> {
            try {
                pushQueue.put(profiler.dump());
            } catch (final InterruptedException ignored) {
            // It's fine to swallow InterruptedException here and exit.
            // It's a cue to end the work and exit and we have nothing to clean up.
            }
        };
        executor.scheduleAtFixedRate(dumpProfile, config.uploadInterval.toMillis(), config.uploadInterval.toMillis(), TimeUnit.MILLISECONDS);
        final Thread uploaderThread = new Thread(new Uploader(logger, pushQueue, config));
        uploaderThread.setDaemon(true);
        uploaderThread.start();
    } catch (final Throwable e) {
        logger.error("Error starting profiler", e);
    }
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Config(io.pyroscope.javaagent.config.Config) PropertiesUtil(org.apache.logging.log4j.util.PropertiesUtil) Logger(org.apache.logging.log4j.Logger) SimpleLogger(org.apache.logging.log4j.simple.SimpleLogger) Properties(java.util.Properties) SimpleLogger(org.apache.logging.log4j.simple.SimpleLogger)

Example 2 with SimpleLogger

use of org.apache.logging.log4j.simple.SimpleLogger in project Javacord by BtoBastian.

the class LoggerUtil method getLogger.

/**
 * Get or create a logger with the given name.
 *
 * @param name The name of the logger.
 * @return The logger with the given name.
 */
public static Logger getLogger(String name) {
    AtomicBoolean logWarning = new AtomicBoolean(false);
    initialized.updateAndGet(initialized -> {
        if (!initialized && !ProviderUtil.hasProviders()) {
            noLogger.set(true);
            logWarning.set(true);
        }
        return true;
    });
    if (noLogger.get()) {
        return loggers.computeIfAbsent(name, key -> {
            Level level = FallbackLoggerConfiguration.isTraceEnabled() ? Level.TRACE : (FallbackLoggerConfiguration.isDebugEnabled() ? Level.DEBUG : Level.INFO);
            Logger logger = new SimpleLogger(name, level, true, false, true, true, "yyyy-MM-dd HH:mm:ss.SSSZ", null, new PropertiesUtil(new Properties()), System.out);
            if (logWarning.get()) {
                logger.info("No Log4j2 compatible logger was found. Using default Javacord implementation!");
            }
            return new PrivacyProtectionLogger(logger);
        });
    } else {
        return new PrivacyProtectionLogger(LogManager.getLogger(name));
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) PropertiesUtil(org.apache.logging.log4j.util.PropertiesUtil) Level(org.apache.logging.log4j.Level) Logger(org.apache.logging.log4j.Logger) SimpleLogger(org.apache.logging.log4j.simple.SimpleLogger) Properties(java.util.Properties) SimpleLogger(org.apache.logging.log4j.simple.SimpleLogger)

Example 3 with SimpleLogger

use of org.apache.logging.log4j.simple.SimpleLogger in project james by tomtom-international.

the class Log4j2PublisherTest method shouldLogEvent.

@Test
public void shouldLogEvent() {
    SimpleLogger logger = (SimpleLogger) LogManager.getLogger(LOGGER_NAME);
    final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    logger.setStream(new PrintStream(byteArrayOutputStream));
    createPublisher().publish(new Event(Collections.singletonMap("key", "value"), Instant.EPOCH));
    assertThat(byteArrayOutputStream.toString().trim()).isEqualTo("INFO logger @created=\"1970-01-01T00:00:00Z\" key=\"value\" type=\"james\"");
}
Also used : PrintStream(java.io.PrintStream) Event(com.tomtom.james.common.api.publisher.Event) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SimpleLogger(org.apache.logging.log4j.simple.SimpleLogger) Test(org.junit.jupiter.api.Test)

Example 4 with SimpleLogger

use of org.apache.logging.log4j.simple.SimpleLogger in project Javacord by Javacord.

the class LoggerUtil method getLogger.

/**
 * Get or create a logger with the given name.
 *
 * @param name The name of the logger.
 * @return The logger with the given name.
 */
public static Logger getLogger(String name) {
    AtomicBoolean logWarning = new AtomicBoolean(false);
    initialized.updateAndGet(initialized -> {
        if (!initialized && !ProviderUtil.hasProviders()) {
            noLogger.set(true);
            logWarning.set(true);
        }
        return true;
    });
    if (noLogger.get()) {
        return loggers.computeIfAbsent(name, key -> {
            Level level = FallbackLoggerConfiguration.isTraceEnabled() ? Level.TRACE : (FallbackLoggerConfiguration.isDebugEnabled() ? Level.DEBUG : Level.INFO);
            Logger logger = new SimpleLogger(name, level, true, false, true, true, "yyyy-MM-dd HH:mm:ss.SSSZ", null, new PropertiesUtil(new Properties()), System.out);
            if (logWarning.get()) {
                logger.info("No Log4j2 compatible logger was found. Using default Javacord implementation!");
            }
            return new PrivacyProtectionLogger(logger);
        });
    } else {
        return new PrivacyProtectionLogger(LogManager.getLogger(name));
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) PropertiesUtil(org.apache.logging.log4j.util.PropertiesUtil) Level(org.apache.logging.log4j.Level) Logger(org.apache.logging.log4j.Logger) SimpleLogger(org.apache.logging.log4j.simple.SimpleLogger) Properties(java.util.Properties) SimpleLogger(org.apache.logging.log4j.simple.SimpleLogger)

Aggregations

SimpleLogger (org.apache.logging.log4j.simple.SimpleLogger)4 Properties (java.util.Properties)3 Logger (org.apache.logging.log4j.Logger)3 PropertiesUtil (org.apache.logging.log4j.util.PropertiesUtil)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 Level (org.apache.logging.log4j.Level)2 Event (com.tomtom.james.common.api.publisher.Event)1 Config (io.pyroscope.javaagent.config.Config)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 PrintStream (java.io.PrintStream)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 ThreadFactory (java.util.concurrent.ThreadFactory)1 Test (org.junit.jupiter.api.Test)1