Search in sources :

Example 1 with GelfJsonAppender

use of org.graylog2.log.GelfJsonAppender in project xian by happyyangyuan.

the class GelfLog4j1Init method addGelfAppender.

private static void addGelfAppender() {
    GelfAppender appender = new GelfJsonAppender();
    appender.setName(GELF_APPENDER_NAME);
    if (EnvUtil.isLan()) {
        appender.setGraylogHost(XianConfig.get("gelfInputLanUrl"));
    } else {
        appender.setGraylogHost(XianConfig.get("gelfInputInternetUrl"));
    }
    appender.setGraylogPort(XianConfig.getIntValue("gelfInputInternetUrl"));
    appender.setFacility("gelf-java");
    appender.setLayout(new PatternLayout() {

        {
            setConversionPattern("%p %m");
        }
    });
    appender.setExtractStacktrace(true);
    appender.setIncludeLocation(false);
    JSONObject additionalFields = new JSONObject() {

        {
            put("environment", EnvUtil.getEnv());
            put("application", EnvUtil.getApplication());
            put("pid", JavaPIDUtil.getProcessName());
            put("nodeId", LocalNodeManager.LOCAL_NODE_ID);
        }
    };
    appender.setAddExtendedInformation(true);
    appender.setAdditionalFields(additionalFields.toJSONString());
    appender.setOriginHost(JavaPIDUtil.getHostname());
    // 让以上设置生效
    appender.activateOptions();
    Logger.getRootLogger().addAppender(appender);
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) PatternLayout(org.apache.log4j.PatternLayout) GelfAppender(org.graylog2.log.GelfAppender) GelfJsonAppender(org.graylog2.log.GelfJsonAppender)

Aggregations

JSONObject (com.alibaba.fastjson.JSONObject)1 PatternLayout (org.apache.log4j.PatternLayout)1 GelfAppender (org.graylog2.log.GelfAppender)1 GelfJsonAppender (org.graylog2.log.GelfJsonAppender)1