简体   繁体   English

从tomcat日志中提取SQL查询

[英]Extracting sql queries from tomcat logs

I need to develop a JAVA application by following an object oriented design pattern for extracting sql queries from Tomcat logs. 我需要通过遵循用于从Tomcat日志中提取sql查询的面向对象设计模式来开发JAVA应用程序。 I have the log file. 我有日志文件。 I need to extract sql queries from that file. 我需要从该文件中提取SQL查询。 I am new to this field. 我是这个领域的新手。 What are the basics i need to know to do this or give some good references. 我需要了解哪些基本知识或提供一些参考。

As i cant share the whole log file , i am sharing some part of it: 由于我无法共享整个日志文件,因此我正在共享其中的一部分:

(Dao.java:execute:73) SELECT ORDER_STATUS_TYPE_ID, NAME FROM ORD.ORDER_STATUS_TYPE [DEBUG] [RMI TCP Connection(5)-127.0.0.1 05 Feb 2018 15:00:10] (TraceInterceptor.java:writeToLog:26) []Leaving TypeDaoImpl.getOrderStatusType(): 1062ms: [com.xyz.abc.api.is.model.type.OrderStatusType@2ac9ecc3, com.xyz.abc.api.is.model.type.OrderStatusType@76777395, com.xyz.abc.api.is.model.type.OrderStatusType@6ad8ddb7, com.xyz.abc.api.is.model.type.OrderStatusType@6738868f, com.xyz.abc.api.is.model.type.OrderStatusType@272c15f, (Dao.java:execute:73)从ORD.ORDER_STATUS_TYPE中选择ORDER_STATUS_TYPE_ID,名称[DEBUG] [RMI TCP Connection(5)-127.0.0.1 2018年2月5日15:00:10](TraceInterceptor.java:writeToLog:26)[ ]离开TypeDaoImpl.getOrderStatusType():1062ms:[com.xyz.abc.api.is.model.type.OrderStatusType @ 2ac9ecc3,com.xyz.abc.api.is.model.type.OrderStatusType @ 76777395,com.xyz .abc.api.is.model.type.OrderStatusType @ 6ad8ddb7,com.xyz.abc.api.is.model.type.OrderStatusType@6738868f,com.xyz.abc.api.is.model.type.OrderStatusType@272c15f ,

I am posting an answer assuming you cannot edit the DAO.java . 我正在发布一个答案,假设您无法编辑DAO.java If you can edit DAO.java introduce some characters like ||<< to mark the starting and end of a printing query. 如果可以编辑DAO.java引入||<<类的字符来标记打印查询的开始和结束。 That way you won't have make an exhaustive list of all the SQL commands. 这样,您将不会详尽列出所有SQL命令。

public static void main(String[] args) throws IOException {
        long start = System.currentTimeMillis();
        FileInputStream inputStream = null;
        Scanner sc = null;
        try {
            inputStream = new FileInputStream("C:\\Users\\absin\\Desktop\\catalina.out");
            sc = new Scanner(inputStream, "UTF-8");
            while (sc.hasNextLine()) {
                String line = sc.nextLine();
                if (line.toLowerCase().contains("select ") || line.toLowerCase().contains("insert ")
                        || line.toLowerCase().contains("update ")) {
                    // DO something with the found log entry
                    System.out.println(line);
                }
            }
            // note that Scanner suppresses exceptions
            if (sc.ioException() != null) {
                throw sc.ioException();
            }
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
            if (sc != null) {
                sc.close();
            }
        }
        System.out.println("Time taken : " + (System.currentTimeMillis() - start) + " milliseconds");
    }

It prints a lot of log lines with query, and takes ~ 12 seconds on a 40 mb log file. 它在查询时会打印很多日志行,并且在40 mb的日志文件上大约需要12秒。 You can also use regex, if you are familiar with them. 如果您熟悉正则表达式,也可以使用它们。

Your question suggests that you already have a log file which contains the SQL. 您的问题表明您已经有一个包含SQL的日志文件。 Although there are much better and faster ways to parse this file using different operating system tools but since you are constrained with using Java application, you can try using regular expression to parse the log files and get the queries. 尽管使用不同的操作系统工具解析此文件的方法好得多,但也快得多,但是由于使用Java应用程序受到限制,因此您可以尝试使用正则表达式解析日志文件并获取查询。 Some help with code can be found here . 这里可以找到一些代码帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM