Search in sources :

Example 41 with Configuration

use of org.apache.logging.log4j.core.config.Configuration in project vertigo by KleeGroup.

the class SocketLoggerAnalyticsConnectorPlugin method createLogger.

private static Logger createLogger(final String loggerName, final String hostName, final int port) {
    // If it doesn't exist we create it with the right appender
    final Logger logger = LogManager.getLogger(loggerName);
    // we create appender
    final SocketAppender appender = SocketAppender.newBuilder().withName("socketAnalytics").withLayout(SerializedLayout.createLayout()).withHost(hostName).withPort(port).withReconnectDelayMillis(// we make only one try
    0).build();
    appender.start();
    // on ne close pas : car ca stop le context
    final LoggerContext context = LoggerContext.getContext(false);
    final Configuration config = context.getConfiguration();
    config.getLoggerConfig(loggerName).addAppender(appender, null, null);
    Configurator.setLevel(loggerName, Level.INFO);
    return logger;
}
Also used : Configuration(org.apache.logging.log4j.core.config.Configuration) Logger(org.apache.logging.log4j.Logger) LoggerContext(org.apache.logging.log4j.core.LoggerContext) SocketAppender(org.apache.logging.log4j.core.appender.SocketAppender)

Example 42 with Configuration

use of org.apache.logging.log4j.core.config.Configuration in project mule by mulesoft.

the class LoggerContextConfigurer method configureMonitor.

private void configureMonitor(MuleLoggerContext context) {
    Configuration configuration = context.getConfiguration();
    File configFile = null;
    if (context.getConfigFile() != null) {
        configFile = new File(context.getConfigFile().getPath());
    } else if (!StringUtils.isEmpty(configuration.getName())) {
        configFile = new File(configuration.getName());
    }
    if (configFile != null && configuration instanceof Reconfigurable) {
        configuration.getWatchManager().setIntervalSeconds(DEFAULT_MONITOR_INTERVAL_SECS);
        FileWatcher watcher = new ConfiguratonFileWatcher((Reconfigurable) configuration, getListeners(configuration));
        configuration.getWatchManager().watchFile(configFile, watcher);
    }
}
Also used : FileWatcher(org.apache.logging.log4j.core.util.FileWatcher) ConfiguratonFileWatcher(org.apache.logging.log4j.core.config.ConfiguratonFileWatcher) Configuration(org.apache.logging.log4j.core.config.Configuration) AbstractConfiguration(org.apache.logging.log4j.core.config.AbstractConfiguration) File(java.io.File) ConfiguratonFileWatcher(org.apache.logging.log4j.core.config.ConfiguratonFileWatcher) Reconfigurable(org.apache.logging.log4j.core.config.Reconfigurable)

Example 43 with Configuration

use of org.apache.logging.log4j.core.config.Configuration in project xian by happyyangyuan.

the class GelfLog4j2Init method addAppender.

/**
 * add the gelf-log4j2 appender into log4j2 context.
 */
private void addAppender() {
    final LoggerContext context = LoggerContext.getContext(false);
    final Configuration defaultConfig = context.getConfiguration();
    String gelfInputUrl = EnvUtil.isLan() ? XianConfig.get("gelfInputLanUrl") : XianConfig.get("gelfInputInternetUrl");
    System.out.println("gelfInputUrl=" + gelfInputUrl);
    int gelfInputPort = XianConfig.getIntValue("gelfInputPort");
    if (StringUtil.isEmpty(gelfInputUrl) || gelfInputPort <= 0) {
        System.out.println("Gelf input url or port is not properly configured. No log will be sent to gelf logger server.");
        return;
    }
    final GelfLogAppender appender = /* WriterAppender.createAppender(layout, null, writer, writerName, false, true)*/
    GelfLogAppender.createAppender(defaultConfig, "gelf", LevelRangeFilter.createFilter(Level.OFF, Level.INFO, Filter.Result.ACCEPT, null), new GelfLogField[] { new GelfLogField("environment", EnvUtil.getEnv(), null, null), new GelfLogField("application", EnvUtil.getApplication(), null, null), new GelfLogField("pid", JavaPIDUtil.getProcessName(), null, null), new GelfLogField("nodeId", LocalNodeManager.LOCAL_NODE_ID, null, null), new GelfLogField("thread", null, null, PatternLayout.newBuilder().withAlwaysWriteExceptions(false).withPattern("%t").build()), new GelfLogField("logger", null, null, PatternLayout.newBuilder().withAlwaysWriteExceptions(false).withPattern("%c").build()), new GelfLogField("msgId", null, "msgId", null) }, null, null, gelfInputUrl, null, gelfInputPort + "", null, "false", null, null, "gelf-log4j2", // only works when extractStackTrace is true
    "true", null, null, null, true, "%p %m%n", MessageFormatEnum.json);
    if (appender == null)
        throw new RuntimeException("gelf-log4j2 fails to initialize.");
    appender.start();
    defaultConfig.addAppender(appender);
    updateLoggers(appender, defaultConfig);
}
Also used : Configuration(org.apache.logging.log4j.core.config.Configuration) GelfLogAppender(biz.paluch.logging.gelf.log4j2.GelfLogAppender) GelfLogField(biz.paluch.logging.gelf.log4j2.GelfLogField) LoggerContext(org.apache.logging.log4j.core.LoggerContext)

Example 44 with Configuration

use of org.apache.logging.log4j.core.config.Configuration in project meghanada-server by mopemope.

the class Main method main.

public static void main(String[] args) throws ParseException, IOException {
    final String version = getVersion();
    System.setProperty("meghanada-server.version", version);
    final Options options = buildOptions();
    final CommandLineParser parser = new DefaultParser();
    final CommandLine cmd = parser.parse(options, args);
    if (cmd.hasOption("h")) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("meghanada server : " + version, options);
        return;
    }
    if (cmd.hasOption("version")) {
        System.out.println(version);
        return;
    }
    Runtime.getRuntime().addShutdownHook(new Thread(() -> {
        log.info("shutdown server");
        Config.showMemory();
    }));
    System.setProperty("home", Config.getInstalledPath().getParentFile().getCanonicalPath());
    addFileAppender();
    if (cmd.hasOption("v")) {
        final LoggerContext context = (LoggerContext) LogManager.getContext(false);
        final Configuration configuration = context.getConfiguration();
        final LoggerConfig loggerConfig = configuration.getLoggerConfig(LogManager.ROOT_LOGGER_NAME);
        loggerConfig.setLevel(Level.DEBUG);
        context.updateLoggers();
        System.setProperty("log-level", "DEBUG");
        log.debug("set verbose flag(DEBUG)");
    }
    if (cmd.hasOption("vv")) {
        final LoggerContext context = (LoggerContext) LogManager.getContext(false);
        final Configuration configuration = context.getConfiguration();
        final LoggerConfig loggerConfig = configuration.getLoggerConfig(LogManager.ROOT_LOGGER_NAME);
        loggerConfig.setLevel(Level.TRACE);
        context.updateLoggers();
        System.setProperty("log-level", "TRACE");
        log.debug("set verbose flag(TRACE)");
    }
    if (cmd.hasOption("clear-cache")) {
        System.setProperty("clear-cache-on-start", "true");
    }
    if (cmd.hasOption("gradle-version")) {
        final String gradleVersion = cmd.getOptionValue("gradle-version", "");
        if (!version.isEmpty()) {
            System.setProperty("meghanada.gradle-version", gradleVersion);
        }
    }
    String port = "55555";
    String projectRoot = "./";
    String fmt = "sexp";
    if (cmd.hasOption("p")) {
        port = cmd.getOptionValue("p", port);
    }
    if (cmd.hasOption("r")) {
        projectRoot = cmd.getOptionValue("r", projectRoot);
    }
    if (cmd.hasOption("output")) {
        fmt = cmd.getOptionValue("output", fmt);
    }
    log.debug("set port:{}, projectRoot:{}, output:{}", port, projectRoot, fmt);
    final int portInt = Integer.parseInt(port);
    log.info("Meghanada-Server Version:{}", version);
    final Server server = createServer("localhost", portInt, projectRoot, fmt);
    server.startServer();
}
Also used : HelpFormatter(org.apache.commons.cli.HelpFormatter) Options(org.apache.commons.cli.Options) CommandLine(org.apache.commons.cli.CommandLine) Configuration(org.apache.logging.log4j.core.config.Configuration) EmacsServer(meghanada.server.emacs.EmacsServer) Server(meghanada.server.Server) CommandLineParser(org.apache.commons.cli.CommandLineParser) LoggerContext(org.apache.logging.log4j.core.LoggerContext) DefaultParser(org.apache.commons.cli.DefaultParser) LoggerConfig(org.apache.logging.log4j.core.config.LoggerConfig)

Example 45 with Configuration

use of org.apache.logging.log4j.core.config.Configuration in project meghanada-server by mopemope.

the class Main method addFileAppender.

private static void addFileAppender() throws IOException {
    final String tempDir = System.getProperty("java.io.tmpdir");
    final File logFile = new File(tempDir, "meghanada_server.log");
    final LoggerContext context = (LoggerContext) LogManager.getContext(false);
    final Configuration configuration = context.getConfiguration();
    final LoggerConfig loggerConfig = configuration.getLoggerConfig(LogManager.ROOT_LOGGER_NAME);
    final FileAppender fileAppender = FileAppender.newBuilder().withName("file").withLayout(PatternLayout.newBuilder().withPattern("[%d][%-5.-5p][%-14.-14c{1}:%4L] %-22.-22M - %m%n").build()).withFileName(logFile.getCanonicalPath()).build();
    configuration.addAppender(fileAppender);
    loggerConfig.addAppender(fileAppender, Level.ERROR, null);
    context.updateLoggers();
}
Also used : FileAppender(org.apache.logging.log4j.core.appender.FileAppender) Configuration(org.apache.logging.log4j.core.config.Configuration) File(java.io.File) LoggerContext(org.apache.logging.log4j.core.LoggerContext) LoggerConfig(org.apache.logging.log4j.core.config.LoggerConfig)

Aggregations

Configuration (org.apache.logging.log4j.core.config.Configuration)210 LoggerContext (org.apache.logging.log4j.core.LoggerContext)120 LoggerConfig (org.apache.logging.log4j.core.config.LoggerConfig)71 Test (org.junit.Test)54 Appender (org.apache.logging.log4j.core.Appender)50 DefaultConfiguration (org.apache.logging.log4j.core.config.DefaultConfiguration)37 Test (org.junit.jupiter.api.Test)29 FileAppender (org.apache.logging.log4j.core.appender.FileAppender)22 File (java.io.File)21 RollingFileAppender (org.apache.logging.log4j.core.appender.RollingFileAppender)20 Path (java.nio.file.Path)14 Map (java.util.Map)14 ArrayList (java.util.ArrayList)13 Level (org.apache.logging.log4j.Level)13 ListAppender (org.apache.log4j.ListAppender)12 ConsoleAppender (org.apache.logging.log4j.core.appender.ConsoleAppender)12 ConfigurationSource (org.apache.logging.log4j.core.config.ConfigurationSource)12 PatternLayout (org.apache.logging.log4j.core.layout.PatternLayout)11 AbstractLogger (org.apache.logging.log4j.spi.AbstractLogger)11 InputStream (java.io.InputStream)10