askIT

Generate random log file contents with Java


Download

document-save.png Download java_random_log_generator


Code

package com.nettport.randomloggenerator;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Random;

public class Main {

    /**
    * Main
    * @param args
    */

    public static void main(String[] args) {

        int x = 1;
        while (x < 8000) {
            String logLine = generateLogLine();
            writeToTextFile("log.txt", logLine);
            System.out.println("[" + x + "] " + logLine);
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            x++;
        }
    }

    /**
    * Generates a log line of the format:
    * %{timestamp} | %{level} | %{subsystem} | %{route} | %{class} | %{message}
    * @return
    */

    public static String generateLogLine(){
        // 1. Generate timestamp
        // 2018-01-05 13:29:49,988
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss,SSS");
        LocalDateTime localDateTime = LocalDateTime.now();
        String timestamp = dtf.format(localDateTime);


        // 2. Level
        String[] levelArray = {"TRACE","DEBUG","INFO","WARN","ERROR","FATAL","NONE"};
        int levelArraySize = levelArray.length;
        int randomLevelArrayNumber = new Random().nextInt(levelArraySize);


        // 3. Subsystem
        String[] subsystemArray = {"features-2-thread-1",
                "activator-1-thread-1",
                "FelixStartLevel",
                "System3"};
        int subsystemArraySize = subsystemArray.length;
        int randomSubsystemArrayNumber = new Random().nextInt(subsystemArraySize);


        // 4. Route
        String[] routeArray = {"route1",
                "route2",
                "myRoute",
                "new-route"};
        int routeArraySize = routeArray.length;
        int randomRouteArrayNumber = new Random().nextInt(routeArraySize);

        // 5. Class
        String[] classArray = {"MyClass.java",
                "NewClass.java",
                "RandomClass.java",
                "Loinclass.java"};
        int randomClassArrayNumber = new Random().nextInt(classArray.length);


        // 6. Message
        String[] messageArray = {"Command registration delayed for bundle org.apache.karaf.shell.ssh/4.2.0.700_fuse-000002. Missing dependencies: [org.apache.sshd.server.SshServer]",
                "BundleEvent STOPPED - org.apache.sshd.core",
                "Command registration delayed for bundle org.apache.karaf.shell.ssh/4.2.0.700_fuse-000002. Missing dependencies: [org.apache.sshd.server.SshServer]",
                "(SecurityUtils.java:415) : register(EdDSA) not registered - enabled=true, supported=false"};
        int randomMessageArrayNumber = new Random().nextInt(messageArray.length);

        // Print
        String logLine = timestamp;
        logLine = logLine + " | ";
        logLine = logLine + levelArray[randomLevelArrayNumber];
        logLine = logLine + " | ";
        logLine = logLine + subsystemArray[randomSubsystemArrayNumber];
        logLine = logLine + " | ";
        logLine = logLine + routeArray[randomRouteArrayNumber];
        logLine = logLine + " | ";
        logLine = logLine + classArray[randomClassArrayNumber];
        logLine = logLine + " | ";
        logLine = logLine + messageArray[randomMessageArrayNumber];

        return logLine;
    } // generateLogLine

    /**
    * Write to text file
    * @param filePath
    * @param data
    */

    public static void writeToTextFile(String filePath, String data) {
        try(FileWriter fw = new FileWriter(filePath, true);
            BufferedWriter bw = new BufferedWriter(fw);
            PrintWriter out = new PrintWriter(bw))
        {
            out.println(data);
        } catch (IOException e) {
            //exception handling left as an exercise for the reader
        }
    } // writeToTextFile

}

Add comment

avatar_blank_60.png

Comment:

Name:

Email:

Comments

No comments yet. You can be the first one to comment. Just write your comment in the form and click on the submit button.