如何将来自不同用户的日志附加到SQLite表中的同一文本文件中?

时间:2015-08-26 17:06:57

标签: java sqlite logging log4j2

我正在使用log4j2来创建一个审计跟踪文件,该文件存储用户执行的每个操作的日志。

所有这些用户都连接到SQLite数据库表。目前,我的所有日​​志都存储在~HEAD_0文件中,但我希望它存储在数据库中,以便每个用户的日志都附加到同一个audit.log文件中。

我的audit.log如下(这与官方log4j网站简单的fileAppender示例基本相同):

log4j2.xml

1 个答案:

答案 0 :(得分:0)

我得到了它的工作。所以,我目前的log4j2.xml没有任何相关的变化,但现在是:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyApp" packages="">
  <Appenders>
    <File name="auditFile" fileName="./audit.log">
      <PatternLayout>
        <Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
      </PatternLayout>
    </File>

  </Appenders>
  <Loggers>
    <Root level="error">
      <AppenderRef ref="auditFile"/>
    </Root>
    <Root level="info">
      <AppenderRef ref="auditFile"/>
    </Root>
  </Loggers>
</Configuration>

为了让它工作,这是我为我的应用程序编写的代码(为了记录,我只是调用saveLog(String context)方法):

public void saveLog(String context) throws ClassNotFoundException, IOException {
        Class.forName(classForName);
        Connection connection = null;
        logger.info(context);

        try
        {
          // create a database connection
          connection = DriverManager.getConnection(connectionPath); 
          //connectionPath is the path to my database

          Statement statement = connection.createStatement();
          statement.setQueryTimeout(30);  // set timeout to 30 sec.

          File file = new File("audit.log"); 
          FileInputStream fis = new FileInputStream(file);

          PreparedStatement ps = connection.prepareStatement("UPDATE OR REPLACE shell SET audit=? WHERE name='shell.txt'"); 
          //shell is the name of my sqlite table

          ps.setBinaryStream(1, fis, (int)file.length());

          ps.executeUpdate();
          ps.close();
          fis.close();


          System.out.println("SQL save done");
        }
    catch(SQLException e)
    {
      // if the error message is "out of memory", 
      // it probably means no database file is found
      System.err.println(e.getMessage());
    }
    finally
    {
      try
      {
        if(connection != null)
          connection.close();
      }

      catch(SQLException e)
      {
        // connection close failed.
        System.err.println(e);
      }
    }
}

每次加载我的应用程序时,这是我用来确保每个用户使用相同audit.log文件的代码:

public void loadLog() throws ClassNotFoundException, SQLException, IOException {

        Class.forName(classForName);
        Connection conn = DriverManager.getConnection(connectionPath);

        String sql = "SELECT audit FROM shell";

        PreparedStatement stmt = conn.prepareStatement(sql);
        ResultSet resultSet = stmt.executeQuery();

        while (resultSet.next()) {
          File audit = new File(fileOutputPath2);
          FileOutputStream fos = new FileOutputStream(audit);

          byte[] buffer = new byte[1];
          InputStream is = resultSet.getBinaryStream(1);

          while (is.read(buffer) > 0) {
            fos.write(buffer);
          }
          fos.close();
        }
        conn.close();

        System.out.println("SQL Load Done");
    }