---
title: "AUDIT_LOG_FILTER_SET_FILTER - OceanBase 数据库 V4.4.2 | OceanBase 文档中心"
description: "AUDIT_LOG_FILTER_SET_FILTER 描述 该函数（表达式）用于创建过滤器。 功能适用性 该内容仅适用于 OceanBase 数据库企业版，OceanBase 数据库社区版暂不支持审计功能。 语法 AUDIT_LOG_FILTER_SET_FILTER(&#x27;filter_name&#x27;, &#x27;definit…"
---
切换语言

- 中文站 - 简体中文
- International - English
- 日本站 - 日本語

文档反馈![](https://mdn.alipayobjects.com/huamei_22khvb/afts/img/A*P8CuR4UJ_FkAAAAAAAAAAAAADiGDAQ/original) OceanBase 数据库分布式版 - V 4.4.2 LTS

# AUDIT_LOG_FILTER_SET_FILTER

更新时间：2026-07-16 20:01:47

[编辑](https://github.com/oceanbase/oceanbase-doc/edit/V4.4.2/zh-CN/700.reference/500.sql-reference/100.sql-syntax/200.common-tenant-of-mysql-mode/400.functions-of-mysql-mode/970.audit-functions-of-mysql-mode/400.audit-log-filter-set-filter-of-mysql-mode.md)  

## 描述

该函数（表达式）用于创建过滤器。

#### 功能适用性

该内容仅适用于 OceanBase 数据库企业版，OceanBase 数据库社区版暂不支持审计功能。

## 语法

```sql
AUDIT_LOG_FILTER_SET_FILTER('filter_name', 'definition_of_filters');

```

## 说明

### 参数解释

- `filter_name`：用于指定过滤器名称。

  #### 说明

  `AUDIT_LOG_FILTER_SET_FILTER` 是 `CREATE OR REPLACE` 语义，对已存在的对象执行 DDL 会覆盖。
 - `definition_of_filters`：用于定义审计过滤器的具体配置，采用 JSON 格式进行表述。在事件类型（Class）之上，可配置事件类型细分（Event Type）、事件字段（Event Field）及字段条件间的 `AND` / `OR` 组合：

     - 事件类型细分（Event Type）：用于对指定事件类型进行进一步细分和过滤。在 JSON 字符串中，通过 `class` 下的 `event` 下的 `name` 参数指定相关字段，如`{"class": [{"name": "connection", "event": [{"name": "disconnect"}]}]}`。
     - 事件字段（Event Field）过滤：事件字段用于对指定事件类型进行进一步细分和过滤。可通过设置 `field` 参数，实现对 connection、general 和 table_access 三类事件的具体字段（field）进行筛选。在 JSON 字符串中，通过 `event` 下的 `log` 参数指定相关字段，如 `"event": [{"name": "read", "log": {"field": {"name": "table_name", "value": "t1"}}}]`。
     - 逻辑组合：支持通过 `AND` / `OR` 对多个 field 进行逻辑组合。

#### 说明

事件类型细分、事件字段过滤和逻辑组合运算从 V4.4.2 BP1 版本开始支持，V4.4.2 版本仅支持事件类型过滤。

事件类型细分支持的子类型：

| 事件类型（Class） | 说明 | 可细分的子类型（Event Type） |
| --- | --- | --- |
| connection | 包含登录登出语句 | - connect：建立连接 - disconnect：断开连接 |
| general | 包含全部非 DML 语句 | - status：会话状态 |
| table_access | 仅可用于 DML | - read：审计读操作 - insert：具体会审计写操作 - delete：具体会审计删除操作 - update：具体会审计更新操作 |

事件字段过滤支持的字段：

包含 `connection`、`general`、`table_access` 三个事件类型，每个事件类型下支持多个事件字段，详见下表：

| 事件类型 | 事件字段（Event Field） | 数据类型 | 说明 |
| --- | --- | --- | --- |
| connection | connection_id | Int | 连接唯一标识 |
| connection | user | String | 需要审计的登录用户名 |
| connection | database | String | 连接时指定的数据库名 |
| connection | ip | String | 连接指定的用户使用的客户端 IP 地址 |
| connection | server_ip | String | 连接的 OBServer 的服务端 IP 地址 |
| connection | server_host | String | 连接的 OBServer 的服务端主机名 |
| general | general_user | String | 审计指定的用户名 |
| general | general_sql_command | String | 审计指定的具体 SQL 类型。当前可覆盖所有非 DML 语句类型，DDL 语句如 `CREATE_TABLE`、`ALTER_USER`、`ALTER_SYSTEM_SET_PARAMETER` 等；其他 SQL 语句如 `SELECT`、`INSERT`、`UPDATE`、`DELETE` 等。 |
| table_access | table_database | String | 事件所属的数据库名称 |
| table_access | table_name | String | 事件相关的表。支持两种用法：   - 仅指定表名时，不区分数据库，将审计所有数据库中同名的表； - 与 table_database 字段配合，通过下方 `AND` / `OR` 逻辑组合，可精准指定某库下的特定表。 |

**查看 `general_sql_command` 支持的完整 SQL 类型，请参考文末相关文档**。

#### `log` 字段的层级、差异与默认行为

日志记录行为取决于 `log` 的取值，以及是否指定了 `class` 或 `event` 项。

| 层级 | 取值 | 说明 | 未指定时行为 |
| --- | --- | --- | --- |
| `filter.log` | true/false | 过滤器总开关，控制默认记录/不记录。 | 若未指定 `class` 或 `event`，默认按 `true` 处理。 |
| `filter.class[].log` | true/false/JSON 字符串 | 针对单个 `class` 条目进行局部开关或条件过滤。 | 可显式配置；未配置时按上层规则处理。 |
| `filter.class[].event[].log` | true/false/JSON 字符串 | 针对单个 `event` 条目进行局部开关或条件过滤。 | 可显式配置；未配置时按上层规则处理。 |

其他说明：

- 表格中**层级**一列是路径表示法的简写，其中：
     - `filter.log` 表示 `filter` 对象下的 `log` 属性，例如 `{"filter": {"log": true}}`。
     - `filter.class[].log` 表示 `filter` 对象下的 `class` 数组中的单个元素的 `log` 属性，例如 `{"filter": {"class": [{"log": true}]}}`。
     - `filter.class[].event[].log` 表示 `filter` 对象下的 `class` 数组中的单个元素的 `event` 数组中的单个元素的 `log` 属性，例如 `{"filter":{"class":[{"name":"table_access","event":[{"name":"read","log":true}]}]}}`。
 - 文档中 “JSON 外层 `log`” 与 “`class`/`event` 层 `log`” 不是重复配置：前者是总开关，后者是局部覆盖或条件过滤。

### 常用过滤器 JSON 写法

在 `class` 基础上可按上表继续扩展细分事件类型、事件字段及逻辑组合。以下为几种常用写法：

- 记录所有事件。

  ```sql
  { "filter": { "log": true } }

  ```
 - 不记录所有事件。

  ```sql
  { "filter": { "log": false } }

  ```
 - 只记录登录登出（仅 `connection` class）。

  ```sql
  { "filter": { "log": true, "class": [ { "name": "connection" } ] } }

  ```
 - 显式列出全部 class（与记录所有事件等价的一种写法）。

  ```sql
  {
    "filter": {
      "log": true,
      "class": [
        { "name": "connection" },
        { "name": "general" },
        { "name": "table_access" }
      ]
    }
  }

  ```

  也可将同层多项合并为数组（等价写法）：在过滤器定义的同一层级上，若对某一类项多次出现且仅取值不同（例如多个 `class` 元素各自只有不同的 `name`），可将这些取值合并进同一项的数组中，只保留一个对象：

  ```sql
  {
    "filter": {
      "log": true,
      "class": [
        { "name": [ "connection", "general", "table_access" ] }
      ]
    }
  }

  ```

#### 事件类型细分（Event Type）

- 在 `connection` 下只保留 `connect`（不包含 `disconnect`）。

  ```sql
  {
    "filter": {
      "log": true,
      "class": [ { "name": "connection", "event": [ { "name": "connect" } ] } ]
    }
  }

  ```
 - `event` 数组中的单个事件条目可显式包含 `log`，用于指定是否记录满足该条目条件的事件。下例在同一 `table_access` 下选中多种事件类型，并对各类型分别指定是否写入审计：

  ```sql
  {
    "filter": {
      "log": true,
      "class": [
        {
          "name": "table_access",
          "event": [
            { "name": "read", "log": false },
            { "name": "insert", "log": true },
            { "name": "delete", "log": true },
            { "name": "update", "log": true }
          ]
        }
      ]
    }
  }

  ```

#### 事件字段（Event Field）与逻辑组合

- 记录 `connection` 的 `connect` 和 `disconnect` 事件，以及 `table_access` 的 `insert`、`delete` 和 `update` 事件。

  ```sql
  {
    "filter": {
      "log": true,
      "class": [
        {
          "name": "connection",
          "event": [
            { "name": "connect" },
            { "name": "disconnect" }
          ]
        },
        { "name": "general" },
        {
          "name": "table_access",
          "event": [
            { "name": "insert" },
            { "name": "delete" },
            { "name": "update" }
          ]
        }
      ]
    }
  }

  ```
 - 逻辑运算符 `and`、`or` 可嵌套构造复杂条件。下列过滤器在 `general` / `status` 事件上，仅在 `log` 条件内记录满足两组 `and` 之一的事件：第一组为 `general_command.str` 等于 `Query` 且 `general_command.length` 等于 `5`；第二组为 `general_command.str` 等于 `Execute` 且 `general_command.length` 等于 `7`。

  ```sql
  {
    "filter": {
      "class": {
        "name": "general",
        "event": {
          "name": "status",
          "log": {
            "or": [
              {
                "and": [
                  { "field": { "name": "general_command.str",    "value": "Query" } },
                  { "field": { "name": "general_command.length", "value": 5 } }
                ]
              },
              {
                "and": [
                  { "field": { "name": "general_command.str",    "value": "Execute" } },
                  { "field": { "name": "general_command.length", "value": 7 } }
                ]
              }
            ]
          }
        }
      }
    }
  }

  ```

### 返回值

表达式输入必须是字符串常量，输出是字符串类型。

- 当 DDL 执行成功时，表达式返回 `OK`。
 - 当 DDL 执行失败时，`SELECT` 语句仍然执行成功，表达式的输出结果为错误信息。

## 示例

- 创建记录所有事件的过滤器 `log_all`。

  ```sql
  SELECT AUDIT_LOG_FILTER_SET_FILTER('log_all', '{ "filter": { "log": true } }');

  ```

  返回结果如下：

  ```shell
  +-------------------------------------------------------------------------+
  | AUDIT_LOG_FILTER_SET_FILTER('log_all', '{ "filter": { "log": true } }') |
  +-------------------------------------------------------------------------+
  | OK                                                                      |
  +-------------------------------------------------------------------------+
  1 row in set

  ```
 - 当 DDL 失败时，`SELECT` 语句仍然执行成功，表达式的输出结果为错误信息。

  ```sql
  SELECT AUDIT_LOG_FILTER_SET_FILTER('log_err', '1');

  ```

  返回结果如下：

  ```shell
  +---------------------------------------------+
  | AUDIT_LOG_FILTER_SET_FILTER('log_err', '1') |
  +---------------------------------------------+
  | ERROR: JSON parsing error.                  |
  +---------------------------------------------+
  1 row in set

  ```

## 相关文档

- `general_sql_command` 语句类型清单：[附录：general_sql_command 支持的完整 SQL 类型](https://www.oceanbase.com/docs/common-oceanbase-database-cn-1000000005906383)
 - [开启安全审计](https://www.oceanbase.com/docs/common-oceanbase-database-cn-1000000005283311)

 上一篇 下一篇 ![有帮助](https://gw.alipayobjects.com/mdn/ob_asset/afts/img/A*y6ocSqN8cqsAAAAAAAAAAAAAARQnAQ)![无帮助](https://gw.alipayobjects.com/mdn/ob_asset/afts/img/A*BG9IQJyLHF8AAAAAAAAAAAAAARQnAQ)![反馈](https://gw.alipayobjects.com/mdn/ob_asset/afts/img/A*eTWdQKCRKHwAAAAAAAAAAAAAARQnAQ)[AI](https://www.oceanbase.com/obi) 咨询热线
