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

Posted Nihaorz

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用jar包格式化Docker 容器日志相关的知识,希望对你有一定的参考价值。

前面使用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.*;
import java.net.InetAddress;

/**
 * @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();
            }
        }
        InetAddress addr = InetAddress.getLocalHost();
        String hostName = addr.getHostName() + "(IP:" + addr.getHostAddress() + ")";
        html = html.replace("<![data]>", sb.toString())
                .replace("<![log-file-path]>", file.getAbsolutePath())
                .replace("<![log-file-host]>", hostName);
        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>

完整源码请移步至https://github.com/wangrui027/docker-logs-format

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

jar包下载地址:https://github.com/wangrui027/docker-logs-format/blob/master/docker-logs-format-1.0-SNAPSHOT-jar-with-dependencies.jar

以上是关于使用jar包格式化Docker 容器日志的主要内容,如果未能解决你的问题,请参考以下文章

Docker:容器管理(启动参数,查看容器和日志,进入和修改容器)

docker容器日志存储多久

docker镜像jar包放在外面还是里面

Springboot应用使用Docker部署

jar包打成docker镜像部署

『中级篇』docker之java容器运行外置springboot-jar(番外篇)(79)