Repository: apache/airflow

Issue:
Improve audit log 
### Discussed in https://github.com/apache/airflow/discussions/25638

See the discussion. There are a couple of improvements that can be done:

* add atribute to download the log rather than open it in-browser
* add .log or similar (.txt?) extension
* sort the output
* possibly more

<div type='discussions-op-text'>

<sup>Originally posted by **V0lantis** August 10, 2022</sup>
### Apache Airflow version

2.3.3

### What happened

The audit log link crashes because there is too much data displayed.


### What you think should happen instead

The windows shouldn't crash

### How to reproduce

Display a dag audit log with thousand or millions lines should do the trick

### Operating System
```
NAME="Amazon Linux" VERSION="2" ID="amzn" ID_LIKE="centos rhel fedora" VERSION_ID="2" PRETTY_NAME="Amazon Linux 2" ANSI_COLOR="0;33" CPE_NAME="cpe:2.3:o:amazon:amazon_linux:2" HOME_URL="https://amazonlinux.com/"
```
### Versions of Apache Airflow Providers

```
apache-airflow-providers-amazon==4.0.0
apache-airflow-providers-celery==3.0.0
apache-airflow-providers-cncf-kubernetes==4.1.0
apache-airflow-providers-datadog==3.0.0
apache-airflow-providers-docker==3.0.0
apache-airflow-providers-ftp==3.0.0
apache-airflow-providers-github==2.0.0
apache-airflow-providers-google==8.1.0
apache-airflow-providers-grpc==3.0.0
apache-airflow-providers-hashicorp==3.0.0
apache-airflow-providers-http==3.0.0
apache-airflow-providers-imap==3.0.0
apache-airflow-providers-jira==3.0.0
apache-airflow-providers-mysql==3.0.0
apache-airflow-providers-postgres==5.0.0
apache-airflow-providers-redis==3.0.0
apache-airflow-providers-sftp==3.0.0
apache-airflow-providers-slack==5.0.0
apache-airflow-providers-sqlite==3.0.0
apache-airflow-providers-ssh==3.0.0
apache-airflow-providers-tableau==3.0.0
apache-airflow-providers-zendesk==4.0.0
```

### Deployment

Official Apache Airflow Helm Chart

### Deployment details

k8s

### Anything else

_No response_

### Are you willing to submit PR?

- [X] Yes I am willing to submit a PR!

### Code of Conduct

- [X] I agree to follow this project's [Code of Conduct](https://github.com/apache/airflow/blob/main/CODE_OF_CONDUCT.md)
</div>


Discussion:
@potiuk @uranusjr For dags with a long history of logs, making the logs downloadable doesn't seem that helpful? Can we explore adding more indexes to the log table? fwiw we have so much logs that /log/list/ won't even load.
Sure - if you want to explore and make a PR - you are most welcome.

Resolving patch:
diff --git a/airflow/www/views.py b/airflow/www/views.py
--- a/airflow/www/views.py
+++ b/airflow/www/views.py
@@ -3720,7 +3720,24 @@ def audit_log(self, dag_id: str, session=None):
             excluded_events = {event.strip() for event in excluded_events_raw.split(',')}
             query = query.filter(Log.event.notin_(excluded_events))
 
-        dag_audit_logs = query.all()
+        current_page = request.args.get('page', default=0, type=int)
+        arg_sorting_key = request.args.get('sorting_key', 'dttm')
+        arg_sorting_direction = request.args.get('sorting_direction', default='asc')
+
+        logs_per_page = PAGE_SIZE
+        audit_logs_count = query.count()
+        num_of_pages = int(math.ceil(audit_logs_count / float(logs_per_page)))
+
+        start = current_page * logs_per_page
+        end = start + logs_per_page
+
+        sort_column = Log.__table__.c.get(arg_sorting_key)
+        if sort_column is not None:
+            if arg_sorting_direction == 'desc':
+                sort_column = sort_column.desc()
+            query = query.order_by(sort_column)
+
+        dag_audit_logs = query.offset(start).limit(logs_per_page).all()
         return self.render_template(
             'airflow/dag_audit_log.html',
             dag=dag,
@@ -3728,7 +3745,16 @@ def audit_log(self, dag_id: str, session=None):
             root=request.args.get('root'),
             dag_id=dag_id,
             dag_logs=dag_audit_logs,
+            num_log_from=min(start + 1, audit_logs_count),
+            num_log_to=min(end, audit_logs_count),
+            audit_logs_count=audit_logs_count,
             page_size=PAGE_SIZE,
+            paging=wwwutils.generate_pages(
+                current_page,
+                num_of_pages,
+            ),
+            sorting_key=arg_sorting_key,
+            sorting_direction=arg_sorting_direction,
         )
 
 


Test patch: