Search in sources :

Example 1 with GelfLogAppender

use of biz.paluch.logging.gelf.log4j2.GelfLogAppender 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)

Aggregations

GelfLogAppender (biz.paluch.logging.gelf.log4j2.GelfLogAppender)1 GelfLogField (biz.paluch.logging.gelf.log4j2.GelfLogField)1 LoggerContext (org.apache.logging.log4j.core.LoggerContext)1 Configuration (org.apache.logging.log4j.core.config.Configuration)1