Provide Best Programming Tutorials

Setup Graylog on local machine and sent log messages to it from Java Application

Overview

This tutorial will show you how to set up Graylog on you local machine and sending the log to it from a Java Application.

Technologies used

  • Graylog
  • Docker
  • Spring Framework
  • Log4j2

Steps

  1. Setup Graylog on your local machine by using docker-compose up the same folder where docker-compose.yaml locates
  2. Write a Java application and integrate Log4j2 in it to send log messages to Graylog instance

Conponent

docker-compose.yaml

version: '2'
services:
  # MongoDB: https://hub.docker.com/_/mongo/
  mongodb:
    image: mongo:3
  # Elasticsearch: https://www.elastic.co/guide/en/elasticsearch/reference/5.6/docker.html
  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:5.6.3
    environment:
      - http.host=0.0.0.0
      - transport.host=localhost
      - network.host=0.0.0.0
      # Disable X-Pack security: https://www.elastic.co/guide/en/elasticsearch/reference/5.6/security-settings.html#general-security-settings
      - xpack.security.enabled=false
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
    ulimits:
      memlock:
        soft: -1
        hard: -1
    mem_limit: 1g
  # Graylog: https://hub.docker.com/r/graylog/graylog/
  graylog:
    image: graylog/graylog:2.4.6-1
    environment:
      # CHANGE ME!
      - GRAYLOG_PASSWORD_SECRET=somepasswordpepper
      # Password: admin
      - GRAYLOG_ROOT_PASSWORD_SHA2=8c6976e5b5410415bde908bd4dee15dfb167a9c873fc4bb8a81f6f2ab448a918
      - GRAYLOG_WEB_ENDPOINT_URI=http://127.0.0.1:9000/api
    links:
      - mongodb:mongo
      - elasticsearch
    depends_on:
      - mongodb
      - elasticsearch
    ports:
      # Graylog web interface and REST API
      - 9000:9000
      # Syslog TCP
      - 514:514
      # Syslog UDP
      - 514:514/udp
      # GELF TCP
      - 12201:12201
      # GELF UDP
      - 12201:12201/udp

Log4j2.xml

<Configuration monitorInterval="300" status="WARN">
  <properties>
    <property name="LOG_HOME">/tmp/xbatch_log</property>
    <property name="FILE_NAME">batchlog</property>
  </properties>
  <Appenders>
    <Console name="Console" target="SYSTEM_OUT">
      <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} -- %msg%n"/>
    </Console>
    <GELF name="gelfAppender" server="127.0.0.1" port="12201" hostName="X-Batch" protocol="UDP">
      <KeyValuePair key="environment" value="DEV"/>
      <KeyValuePair key="application" value="X-Batch"/>
      <KeyValuePair key="additionalFields" value="{'environment': 'TEST', 'application': 'MyAPP'}"/>
    </GELF>
  </Appenders>

  <Loggers>
    <Logger additivity="false" level="error" name="com.daimler.xbatch.gssn.step">
      <AppenderRef ref="gelfAppender"/>
    </Logger>
    <Root level="info">
      <AppenderRef ref="Console"/>
      <AppenderRef ref="gelfAppender"/>
    </Root>
  </Loggers>
</Configuration>

pom.xml

Notice we should exclude the default log compoent

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
      <exclusions>
        <exclusion>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
      </exclusions>
    </dependency>

Full pom.xml is as below:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.andrew-programming</groupId>
  <artifactId>graylog-demo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>graylog-demo</name>
  <description>Demo project for Spring Boot</description>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.4.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
      <exclusions>
        <exclusion>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-devtools</artifactId>
      <scope>runtime</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.graylog2.log4j2</groupId>
      <artifactId>log4j2-gelf</artifactId>
      <version>1.3.1</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-log4j2</artifactId>
      <version>2.0.4.RELEASE</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
</project>

GraylogDemoApplication.java

package com.andrewprogramming.graylogdemo;


import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class GraylogDemoApplication {

  public static void main(String[] args) {
    Logger logger = LogManager.getLogger(GraylogDemoApplication.class);
    logger.info("Application Start Running...");
    logger.debug("Application Start Running...");
    logger.error("Application Start Running...");
    logger.warn("Application Start Running...");

    SpringApplication.run(GraylogDemoApplication.class, args);
  }
}

Screenshot

Java Application

Graylog

Run docker-compose up in the same folder where docker-compose.yaml locates.

Open the browser and type http://localhost:9000 to login Graylog.

The password & username default is admin/admin

You may need a moment to let Graylog warm up

Click Sign in and choose input from the dashboard

Choose GELF UDP from the dropdown list and click the Launch new input button

Give this input a title as you want and choose the node then click save button

By default there only have one node to choose

Then a new Local inputs is shown below:

Running the application and checkout the message in Graylog.

Click Show received messages button

there you go! You can see the log message sending from Java Application.

Source code

Github

AndrewProgramming_GraylogDemo-master

Leave a Reply

Close Menu