zoukankan      html  css  js  c++  java
  • 使用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("
    ");
            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

  • 相关阅读:
    How to load custom styles at runtime (不会翻译,只有抄了 )
    更多FMK 的还是看万一的吧
    Custom Grid Columns
    样式和说明文档
    LiveBindings --- 把对象之间的属性绑定起来
    LiveBindings如何绑定一个对象(转)
    Delphi LiveBinds组件
    记录一偏,因为我不会翻译,
    Delphi XE4 For IOS之部署问题
    Delphi XE5 android 捕获几个事件
  • 原文地址:https://www.cnblogs.com/nihaorz/p/10281555.html
Copyright © 2011-2022 走看看