码迷,mamicode.com
首页 > 编程语言 > 详细

使用jar包格式化Docker 容器日志

时间:2019-01-17 12:54:16      阅读:320      评论:0      收藏:0      [点我收藏+]

标签:下载地址   日志格式   iter   get   ring   throws   int   project   apache   

前面使用JS格式化textarea中的日志内容,但局限于JS语言性能,在日志内容较多时效率无法接受,建议日志内容大于5000行时转投本java程序,文末提供jar包下载。

LogsFormat.java

package com.geostar.gfstack.docker.util;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.apache.commons.io.IOUtils;

import java.io.*;

/**
 * @author 王睿
 * @date 2019-01-17 10:54
 */
public class LogsFormat {

    public static void main(String[] args) throws IOException {
        if (args.length == 0) {
            System.err.println("请输入第一个参数:日志文件路径");
            return;
        }
        String fileName = args[0];
        InputStream is = LogsFormat.class.getResourceAsStream("/templates/DockerLogsTemplate.html");
        String html = IOUtils.toString(is, "UTF-8");
        File file = new File(fileName);
        String text = IOUtils.toString(new FileInputStream(file), "UTF-8");
        String[] arr = text.split("\n");
        JsonParser parser = new JsonParser();
        String time, log, stream;
        StringBuilder sb = new StringBuilder(1024 * 10);
        for (int i = 0; i < arr.length; i++) {
            try {
                JsonObject jsonObject = parser.parse(arr[i]).getAsJsonObject();
                time = jsonObject.get("time").getAsString();
                log = jsonObject.get("log").getAsString();
                stream = jsonObject.get("stream").getAsString();
                sb.append("<tr");
                if ("stderr".equals(stream)) {
                    sb.append(" class=‘stderr‘");
                }
                sb.append("><td>");
                sb.append(i + 1);
                sb.append("</td><td>");
                sb.append(time);
                sb.append("</td><td>");
                sb.append(log);
                sb.append("</td></tr>");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        html = html.replace("<![data]>", sb.toString());
        File outFile = new File(file.getPath() + ".html");
        OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(outFile), "UTF-8");
        out.write(html);
        out.close();
        System.out.println("Docker日志格式化成功,请查看:" + outFile.getAbsoluteFile());
    }
}

 

pom.xml

<?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>geostack</groupId>
    <artifactId>docker-logs-format</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.5</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.6</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.1.1</version>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>com.geostar.gfstack.docker.util.LogsFormat</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <compilerVersion>1.8</compilerVersion>
                    <skip>true</skip>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

执行 mvn clean compile assembly:single 构建可执行jar包即可使用,执行jar包时Docker原始日志文件作为第一个参数,在同目录下即可生成同名html文件,使用浏览器打开查看即可。

jar包下载地址:https://files.cnblogs.com/files/nihaorz/docker-logs-format-1.0-SNAPSHOT-jar-with-dependencies.jar.zip

使用jar包格式化Docker 容器日志

标签:下载地址   日志格式   iter   get   ring   throws   int   project   apache   

原文地址:https://www.cnblogs.com/nihaorz/p/10281555.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!