# VPC firewall rules logging lets you audit, verify, and analyze the
effects of your firewall rules. For example, you can determine whether a
firewall rule designed to deny traffic is functioning as intended.
VPC firewall rules logging is also useful if you need to determine how
many connections are affected by a given firewall rule.

You enable VPC firewall rules logging individually for each firewall rule
whose connections you need to log. VPC firewall rules logging is an option
for any firewall rule, regardless of the action (`allow` or `deny`) or direction
(ingress or egress) of the rule.

VPC firewall rules logging logs traffic to and from
[Compute Engine virtual machine (VM) instances](https://docs.cloud.google.com/compute/docs/instances).
This includes Google Cloud products built on Compute Engine
VMs, such as [Google Kubernetes Engine (GKE)
clusters](https://docs.cloud.google.com/kubernetes-engine/docs/concepts/cluster-architecture) and
[App Engine flexible environment](https://docs.cloud.google.com/appengine/docs/flexible) instances.

When you enable logging for a firewall rule, Google Cloud creates an entry
called a *connection record* each time the rule allows or denies traffic. You
can view these records in [Cloud Logging](https://docs.cloud.google.com/logging), and you can export logs
to any destination that Cloud Logging export supports.

Each connection record contains the source and destination IP addresses, the
protocol and ports, date and time, and a reference to the firewall rule that
applied to the traffic.

VPC firewall rules logging is available for both VPC
firewall rules and hierarchical firewall policies.

> [!NOTE]
> **Note:** After you create a firewall rule or enable logging on an existing rule, you might observe some delay before log entries appear. Logging data takes time to synchronize and fully propagate. Initial log entries might be missing some rule details.

For information about viewing logs, see [Use
VPC firewall rules logging](https://docs.cloud.google.com/firewall/docs/using-firewall-rules-logging).

## Specifications

VPC firewall rules logging has the following specifications:

- VPC firewall rules logging can be enabled for the following:

  - Firewall rules in hierarchical firewall policies, regional system
    firewall policies, global network firewall policies, and regional
    network firewall policies that are associated with a regular
    VPC network.

  - VPC firewall rules in a regular VPC
    network.

  - Firewall rules in regional network firewall policies that
    are associated with an
    [RoCE VPC network](https://docs.cloud.google.com/firewall/docs/firewall-for-roce).

- VPC firewall rules logging doesn't support the following:

  - VPC firewall rules in [Legacy networks](https://docs.cloud.google.com/vpc/docs/legacy).

  - [Implied deny ingress and implied allow egress rules](https://docs.cloud.google.com/firewall/docs/firewalls#default_firewall_rules)
    of a regular VPC network.

  - [Implied allow ingress and egress rules](https://docs.cloud.google.com/firewall/docs/firewall-for-roce#implied_firewall_rules)
    of an RoCE VPC network.

- VPC firewall rules logging only records TCP and UDP connections.
  Although you can
  [create a firewall rule that applies to other protocols](https://docs.cloud.google.com/firewall/docs/firewalls#protocols_and_ports),
  you cannot log their connections.
  If you want to also
  log other protocols, consider using
  [Packet Mirroring](https://docs.cloud.google.com/vpc/docs/packet-mirroring).

- Log entries are written from the perspective of VMs. Log entries
  are only created if a firewall rule has logging enabled and if the rule
  applies to traffic sent to or from the VM. Entries are created according to
  the connection logging limits on a *best effort* basis.

- VPC firewall rules logging creates a log entry only when a connection is
  established. It doesn't log every packet. A UDP connection entry
  remains active as long as packets are exchanged at least once every 10
  minutes. Each subsequent ingress or egress packet resets the idle timer,
  extending the connection for another 10 minutes.
  As a result, a constant stream of UDP traffic generates only one log entry for
  its entire duration. If you need continuous visibility into active, long-lived
  streams without idle periods, use [VPC Flow Logs](https://docs.cloud.google.com/vpc/docs/flow-logs).

- When a firewall rules uses the `apply_security_profile_group` action,
  VPC firewall rules logging generates a log entry for every new connection
  that is intercepted for inspection. This log entry confirms that the
  firewall rule matched the traffic and successfully redirected it to the
  firewall endpoint for deep packet inspection. For more information, see
  [Threat logs](https://docs.cloud.google.com/firewall/docs/threat_logs) and
  [URL filtering logs](https://docs.cloud.google.com/firewall/docs/url-filtering-logs).

- The number of connections that VPC firewall rules logging can log
  per unit of time:

  - Is [based on machine type](https://docs.cloud.google.com/vpc/docs/quota#logging_limits) for regular
    VPC networks.

  - [Depends on the monitoring or logging action of the firewall rule](https://docs.cloud.google.com/firewall/docs/firewall-for-roce#monitoring_and_logging)
    for RoCE VPC networks.

- Changes to firewall rules can be viewed in
  [VPC audit logs](https://docs.cloud.google.com/vpc/docs/audit-logging).

## Limitations

- Logging for the `apply_security_profile_group` action uses session-based
  logic, which differs from the connection-based logs produced by
  standard `allow` or `deny` rules. When enabled, Cloud NGFW
  generates a single firewall rule log entry for the initial session matching
  the rule, even if multiple connections are identified as part of that same
  session. This high-level firewall log is distinct from the detailed
  Layer 7 logs---such as URL filtering or threat logs---that are still generated
  for every inspected connection.

- If you enable logging for a firewall rule that matches existing TCP or UDP
  connections, log entries are not generated for those active connections.
  Logging for these connections begins only after they have been idle for at
  least 10 minutes.

## Logging examples

A log entry is generated each time that a firewall rule with logging enabled applies
to traffic. A given packet flow can generate more than one log entry in total.
However, from the perspective of a given VM, at most only one log entry can be
generated if the firewall rule that applies to it has logging enabled.

The following examples show how firewall logs work.

### Egress deny example

In this example:

- Traffic between VM instances in the `example-net` VPC network in the `example-proj` project is considered.
- The two VM instances are:
  - VM1 in zone `us-west1-a` with IP address `10.10.0.99` in the `west-subnet` (`us-west1` region).
  - VM2 in zone `us-east1-b` with IP address `10.20.0.99` in the `east-subnet` (`us-east1` region).
- Rule A: An egress deny firewall rule has a target of all instances in the network, a destination of `10.20.0.99` (VM2), and applies to TCP port `80`.
  - Logging is enabled for this rule.
- Rule B: An ingress allow firewall rule has a target of all instances in the network, a source of `10.10.0.99` (VM1), and applies to TCP port `80`.
  - Logging is also enabled for this rule.

The following `gcloud` commands can be used to create the firewall rules:

- Rule A: egress deny rule for TCP port `80`, applicable to all instances,
  destination `10.20.0.99`:

  ```
  gcloud compute firewall-rules create rule-a \
      --network example-net \
      --action deny \
      --direction EGRESS \
      --rules tcp:80 \
      --destination-ranges 10.20.0.99/32 \
      --priority 10 \
      --enable-logging
  ```
- Rule B: ingress allow rule for TCP port `80`, applicable to all instances,
  source `10.10.0.99`:

  ```
  gcloud compute firewall-rules create rule-b \
      --network example-net \
      --action allow \
      --direction INGRESS \
      --rules tcp:80 \
      --source-ranges 10.10.0.99/32 \
      --priority 10 \
      --enable-logging
  ```

[](https://docs.cloud.google.com/static/firewall/images/firewall-rules-logs/firewall-rules-logging-1.svg) VM1 to VM2 connection (click to enlarge).

Suppose VM1 attempts to connect to VM2 on TCP port `80`. The following firewall
rules are logged:

- A log entry for rule A from the perspective of VM1 is generated while VM1 attempts to connect to `10.20.0.99` (VM2).
- Because rule A actually blocks the traffic, rule B isn't considered, so there is no log entry for rule B from the perspective of VM2.

The following firewall log record is reported.

| Field | Values |
|---|---|
| connection | src_ip=10.10.0.99 src_port=\[EPHEMERAL_PORT\] dest_ip=10.20.0.99 dest_port=80 protocol=6 |
| disposition | DENIED |
| rule_details | reference = "network:example-net/firewall:rule-a" priority = 10 action = DENY destination_range = 10.20.0.99/32 ip_port_info = tcp:80 direction = egress |
| instance | project_id="example-proj" instance_name=VM1 region=us-west1 zone=us-west1-a |
| vpc | project_id="example-proj" vpc_name=example-net subnetwork_name=west-subnet |
| remote_instance | project_id="example-proj" instance_name=VM2 region=us-east1 zone=us-east1-b |
| remote_vpc | project_id="example-proj" vpc_name=example-net subnetwork_name=east-subnet |
| remote_location | *No information. This field is only used if the destination is outside your VPC network.* |

### Egress allow, ingress allow example

In this example:

- Traffic between VM instances in the `example-net` VPC network in the `example-proj` project is considered.
- The two VM instances are:
  - VM1 in zone `us-west1-a` with IP address `10.10.0.99` in the `west-subnet` (`us-west1` region).
  - VM2 in zone `us-east1-b` with IP address `10.20.0.99` in the `east-subnet` (`us-east1` region).
- Rule A: An egress allow firewall rule has a target of all instances in the network, a destination of `10.20.0.99` (VM2), and applies to TCP port `80`.
  - Logging is enabled for this rule.
- Rule B: An ingress allow firewall rule has a target of all instances in the network, a source of `10.10.0.99` (VM1), and applies to TCP port `80`.
  - Logging is also enabled for this rule.

The following `gcloud` commands can be used to create the two firewall rules:

- Rule A: egress allow rule for TCP port `80`, applicable to all instances,
  destination `10.20.0.99` (VM2):

  ```
  gcloud compute firewall-rules create rule-a \
      --network example-net \
      --action allow \
      --direction EGRESS \
      --rules tcp:80 \
      --destination-ranges 10.20.0.99/32 \
      --priority 10 \
      --enable-logging
  ```
- Rule B: ingress allow rule for TCP port `80`, applicable to all instances,
  source `10.10.0.99` (VM1):

  ```
  gcloud compute firewall-rules create rule-b \
      --network example-net \
      --action allow \
      --direction INGRESS \
      --rules tcp:80 \
      --source-ranges 10.10.0.99/32 \
      --priority 10 \
      --enable-logging
  ```

[](https://docs.cloud.google.com/static/firewall/images/firewall-rules-logs/firewall-rules-logging-2.svg) VM1 to VM2 connection (click to enlarge).

Suppose VM1 attempts to connect to VM2 on TCP port `80`. The following firewall
rules are logged:

- A log entry for rule A from the perspective of VM1 is generated while VM1 connects to `10.20.0.99` (VM2).
- A log entry for rule B from the perspective of VM2 is generated while VM2 allows incoming connections from `10.10.0.99` (VM1).

The following firewall log record is reported by VM1.

| Field | Values |
|---|---|
| connection | src_ip=10.10.0.99 src_port=\[EPHEMERAL_PORT\] dest_ip=10.20.0.99 dest_port=80 protocol=6 |
| disposition | ALLOWED |
| rule_details | reference = "network:example-net/firewall:rule-a" priority = 10 action = ALLOW destination_range = 10.20.0.99/32 ip_port_info = tcp:80 direction = egress |
| instance | project_id="example-proj" instance_name=VM1 region=us-west1 zone=us-west1-a |
| vpc | project_id="example-proj" vpc_name=example-net subnetwork_name=west-subnet |
| remote_instance | project_id="example-proj" instance_name=VM2 region=us-east1 zone=us-east1-b |
| remote_vpc | project_id="example-proj" vpc_name=example-net subnetwork_name=east-subnet |
| remote_location | *No information. This field is only used if the destination is outside your VPC network.* |

The following firewall log record is reported by VM2.

| Field | Values |
|---|---|
| connection | src_ip=10.10.0.99 src_port=\[EPHEMERAL_PORT\] dest_ip=10.20.0.99 dest_port=80 protocol=6 |
| disposition | ALLOWED |
| rule_details | reference = "network:example-net/firewall:rule-b" priority = 10 action = ALLOW source_range = 10.10.0.99/32 ip_port_info = tcp:80 direction = ingress |
| instance | project_id="example-proj" instance_name=VM2 region=us-east1 zone=us-east1-b |
| vpc | project_id="example-proj" vpc_name=example-net subnetwork_name=east-subnet |
| remote_instance | project_id="example-proj" instance_name=VM1 region=us-west1 zone=us-west1-a |
| remote_vpc | project_id="example-proj" vpc_name=example-net subnetwork_name=west-subnet |
| remote_location | *No information. This field is only used if the destination is outside your VPC network.* |

### Internet ingress example

In this example:

- Traffic from a system outside the `example-net` VPC network to a VM instance in that network is considered. The network is in the `example-proj` project.
- The system on the internet has IP address `203.0.113.114`.
- VM1 in zone `us-west1-a` has IP address `10.10.0.99` in the `west-subnet` (`us-west1` region).
- Rule C: An ingress allow firewall rule has a target of all instances in the network, a source of any IP address (`0.0.0.0/0`), and applies to TCP port `80`.
  - Logging is enabled for this rule.
- Rule D: An egress deny firewall rule has a target of all instances in the network, a destination of any IP address (`0.0.0.0/0`), and applies to all protocols.
  - Logging is also enabled for this rule.

The following `gcloud` commands can be used to create the firewall rules:

- Rule C: ingress allow rule for TCP port `80`, applicable to all instances,
  any source:

  ```
  gcloud compute firewall-rules create rule-c \
      --network example-net \
      --action allow \
      --direction INGRESS \
      --rules tcp:80 \
      --source-ranges 0.0.0.0/0 \
      --priority 10 \
      --enable-logging
  ```
- Rule D: egress deny rule for all protocols, applicable to all instances,
  any destination:

  ```
  gcloud compute firewall-rules create rule-d \
      --network example-net \
      --action deny \
      --direction EGRESS \
      --rules all \
      --destination-ranges 0.0.0.0/0 \
      --priority 10 \
      --enable-logging
  ```

[](https://docs.cloud.google.com/static/firewall/images/firewall-rules-logs/firewall-rules-logging-3.svg) Internet to VM connection (click to enlarge).

Suppose the system with IP address `203.0.113.114` attempts to connect to VM1
on TCP port `80`. The following happens:

- A log entry for rule C from the perspective of VM1 is generated while VM1 accepts traffic from `203.0.113.114`.
- Despite rule D, VM1 is allowed to reply to the incoming request because Google Cloud firewall rules are stateful. If the incoming request is allowed, established responses cannot be blocked by any kind of egress rule.
- Because rule D doesn't apply, it isn't considered, so there is no log entry for rule D.

The following firewall log record is reported.

| Field | Values |
|---|---|
| connection | src_ip=203.0.113.114 src_port=\[EPHEMERAL_PORT\] dest_ip=10.10.0.99 dest_port=80 protocol=6 |
| disposition | ALLOWED |
| rule_details | reference = "network:my-vpc/firewall:rule-c" priority = 10 action = ALLOW source_range = 0.0.0.0/0 ip_port_info = tcp:80 direction = ingress |
| instance | project_id="example-proj" instance_name=VM1 region=us-west1 zone=us-west1-a |
| vpc | project_id="example-proj" vpc_name=example-net subnetwork_name=west-subnet |
| remote_location | continent country region city |

## Firewall log format

Subject to the [specifications](https://docs.cloud.google.com/firewall/docs/firewall-rules-logging#specifications),
a log entry is created in Cloud Logging for each firewall rule that has
logging enabled if that rule applies to traffic to or from a VM instance. Log
records are included in the JSON payload field of a Logging
[LogEntry](https://docs.cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry).

Log records contain base fields, which are the core fields of every log record,
and metadata fields that add additional information. You can control whether
metadata fields are included. If you omit them, you can save on storage costs.

Some log fields support values that are also fields. These fields can have more
than one piece of data in a given field. For example, the `connection` field is
of the `IpConnection` format, which contains the source and destination IP
address and port, plus the protocol, in a single field. These fields are
described in the following tables.

| Field | Description | Field type: base or optional metadata |
|---|---|---|
| connection | [IpConnection](https://docs.cloud.google.com/firewall/docs/firewall-rules-logging#ipconnection) 5-Tuple describing the source and destination IP address, source and destination port, and IP protocol of this connection. | Base |
| disposition | string Indicates whether the connection was `ALLOWED` or `DENIED`. | Base |
| rule_details | [RuleDetails](https://docs.cloud.google.com/firewall/docs/firewall-rules-logging#ruledetails) Details of the rule that was applied to this connection. |   |
| rule_details | `rule_details.reference` field | Base |
| rule_details | Other rule detail fields | Metadata |
| instance | [InstanceDetails](https://docs.cloud.google.com/firewall/docs/firewall-rules-logging#instancedetails) VM instance details. In a Shared VPC configuration, `project_id` corresponds to that of the service project. | Metadata |
| load_balancer_details | [LoadBalancingDetails](https://docs.cloud.google.com/firewall/docs/firewall-rules-logging#loadbalancingdetails) Details of the internal Application Load Balancer or internal proxy Network Load Balancer to which the firewall rule applies. When the target of a firewall rule is one of these load balancers, the `instance` field is omitted. | Metadata |
| vpc | [VpcDetails](https://docs.cloud.google.com/firewall/docs/firewall-rules-logging#vpcdetails) VPC network details. In a Shared VPC configuration, `project_id` corresponds to that of the host project. | Metadata |
| remote_instance | [InstanceDetails](https://docs.cloud.google.com/firewall/docs/firewall-rules-logging#instancedetails) If the remote endpoint of the connection was a VM located in the Compute Engine, this field is populated with VM instance details. | Metadata |
| remote_vpc | [VpcDetails](https://docs.cloud.google.com/firewall/docs/firewall-rules-logging#vpcdetails) If the remote endpoint of the connection was a VM that is located in a VPC network, this field is populated with the network details. | Metadata |
| remote_location | [GeographicDetails](https://docs.cloud.google.com/firewall/docs/firewall-rules-logging#geographicdetails) If the remote endpoint of the connection was external to the VPC network, this field is populated with available location metadata. | Metadata |

### IpConnection

| Field | Type | Description |
|---|---|---|
| src_ip | string | Source IP address. If the source is a Compute Engine VM, `src_ip` is either the primary internal IP address or an address in an alias IP range of the VM's network interface. The external IP address is not shown. Logging shows the IP address of the VM as the VM sees it on the packet header, the same as if you ran TCP dump on the VM. |
| src_port | integer | Source port |
| dest_ip | string | Destination IP address. If the destination is a Google Cloud VM, `dest_ip` is either the primary internal IP address or an address in an alias IP range of the VM's network interface. The external IP address is not shown even if it was used in making the connection. |
| dest_port | integer | Destination port |
| protocol | integer | [IP protocol](https://www.iana.org/assignments/protocol-numbers/protocol-numbers.xhtml) of the connection |

### RuleDetails

| Field | Type | Description |
|---|---|---|
| reference | string | Reference to the firewall rule; format: `"network:{network name}/firewall:{firewall_name}"` |
| priority | integer | The priority for the firewall rule. |
| action | string | ALLOW or DENY |
| source_network_context | string | The network context for the traffic that an ingress rule applies to. |
| source_networks\[ \] | string | List of VPC networks when the source network context parameter is `VPC_NETWORKS`. |
| destination_network_context | string | The network context for the traffic that an egress rule applies to. |
| source_range\[ \] | string | List of source ranges that the firewall rule applies to. |
| destination_range\[ \] | string | List of destination ranges that the firewall rule applies to. |
| ip_port_info\[ \] | [IpPortDetails](https://docs.cloud.google.com/firewall/docs/firewall-rules-logging#IpPortDetails) | List of IP protocols and applicable port ranges for rules. |
| direction | string | The direction that the firewall rule applies to (ingress or egress). |
| source_tag\[ \] | string | List of all the source tags that the firewall rule applies to. |
| target_tag\[ \] | string | List of all the target tags that the firewall rule applies to. |
| source_service_account\[ \] | string | List of all the source service accounts that the firewall rule applies to. |
| target_service_account\[ \] | string | List of all the target service accounts that the firewall rule applies to. |
| source_region_code\[ \] | string | List of all the source country codes that the firewall rule applies to. |
| destination_region_code\[ \] | string | List of all the destination country codes that the firewall rule applies to. |
| source_fqdn\[ \] | string | List of all the source domain names that the firewall rule applies to. |
| destination_fqdn\[ \] | string | List of all the destination domain names that the firewall rule applies to. |
| source_threat_intelligence\[ \] | string | List of all the source Google Threat Intelligence list names that the firewall rule applies to. |
| destination_threat_intelligence\[ \] | string | List of all the destination Google Threat Intelligence list names that the firewall rule applies to. |
| source_address_groups\[ \] | string | List of all the source address groups that the firewall rule applies to. |
| destination_address_groups\[ \] | string | List of all the destination address groups that the firewall rule applies to. |

### IpPortDetails

| Field | Type | Description |
|---|---|---|
| ip_protocol | string | IP protocol that the firewall rule applies to. "ALL" if applies to all protocols. |
| port_range\[ \] | string | List of applicable port ranges for rules; for example, `8080-9090`. |

### InstanceDetails

| Field | Type | Description |
|---|---|---|
| project_id | string | ID of the project containing the VM |
| vm_name | string | Instance name of the VM |
| region | string | Region of the VM |
| zone | string | Zone of the VM |

### LoadBalancingDetails

| Field | Type | Description |
|---|---|---|
| forwarding_rule_project_id | string | Google Cloud project ID that contains the forwarding rule. |
| type | string | Load balancer type: `APPLICATION_LOAD_BALANCER` indicates an internal Application Load Balancer. `PROXY_NETWORK_LOAD_BALANCER` indicates an internal proxy Network Load Balancer. |
| scheme | string | Load balancer scheme, `INTERNAL_MANAGED`. |
| url_map_name | string | Name of the URL map. Only populated if the `type` is `APPLICATION_LOAD_BALANCER`. |
| forwarding_rule_name | string | Name of the forwarding rule. |

### VpcDetails

| Field | Type | Description |
|---|---|---|
| project_id | string | ID of the project containing the network |
| vpc_name | string | Network on which the VM is operating |
| subnetwork_name | string | Subnet on which the VM is operating |

### GeographicDetails

| Field | Type | Description |
|---|---|---|
| continent | string | Continent for external endpoints |
| country | string | Country for external endpoints |
| region | string | Region for external endpoints |
| city | string | City for external endpoints |

## What's next

- To set up logging and view logs, see [Use VPC firewall rules logging](https://docs.cloud.google.com/firewall/docs/using-firewall-rules-logging).
- To get insights about how your firewall rules are being used, see [Firewall Insights](https://docs.cloud.google.com/network-intelligence-center/docs/firewall-insights/concepts/overview).
- To store, search, analyze, monitor, and alert on log data and events, see [Cloud Logging](https://docs.cloud.google.com/logging/docs).
- To route log entries, see [Route logs to supported destinations](https://docs.cloud.google.com/logging/docs/export/configure_export_v2).