로그 관리
Apache Airflow를 사용하면 dag가 실행될 때 마다 위와 같이 로그가 남는다.
한 폴더당 20KB 정도 되는데 하루에 10개씩 생기며 다른 대그가 추가될 수도 있기에 조금만 지나도 메모리를 많이 차지할 것이다.
때문에 로그를 14일치만 저장해두고 삭제하는 dag를 따로 작성하였다.
import os
import shutil
from datetime import datetime, timedelta, timezone
from airflow import DAG
from airflow.operators.python import PythonOperator
def arrange_old_logs():
log_dir = '/opt/airflow/logs'
# 14일치 로그 파일 계산
cutoff_date = (datetime.now(timezone.utc) - timedelta(days=14)).date()
delete_count = 0
# dag_id : logs 아래 각 폴더명
# dag_path : logs 아래 각 폴더의 경로
# run_id : dag_id=~~ 폴더 아래의 각 폴더명
# run_path : run_id로 되어있는 로그를 담고있는 파일 경로
# run_time_str : 런타임 시간 문자열
for dag_id in os.listdir(log_dir):
dag_path = os.path.join(log_dir, dag_id)
if os.path.isdir(dag_path):
for run_id in os.listdir(dag_path):
run_path = os.path.join(dag_path, run_id)
if os.path.isdir(run_path):
run_time_str = run_id.split('__')[-1]
try:
run_time = datetime.fromisoformat(run_time_str).date()
# 지금으로부터 14일 전 보다 더 이전 파일들 제거
if run_time < cutoff_date:
shutil.rmtree(run_path)
delete_count += 1
print(f"Deleted logs: {run_path}")
except ValueError:
print(f"Skipping: {run_path}, unable to parse date from {run_time_str}")
print(f"Total logs deleted: {delete_count}")
default_args = {
'owner': 'joonghyeon',
'depends_on_past': False,
'start_date': datetime(2025, 6, 23),
'retries': 1,
'retry_delay': timedelta(minutes=5),
}
with DAG(
dag_id='arrangeLogs',
default_args=default_args,
schedule='0 17 * * 1-5', # 매일 새벽 2시에 실행
catchup=False,
max_active_runs=1,
tags=['log', 'arrange', 'cleanup']
) as dag:
arrange_task = PythonOperator(
task_id='arrange_old_logs',
python_callable=arrange_old_logs,
)
arrange_task
추가로 dag가 많아진다면 실패한 task에 대한 로그는 남겨두는 기능을 추가할 예정이다.
(지금은 매일 Airflow UI로 확인 중이다.)
'프로젝트' 카테고리의 다른 글
노션 - 구글 Sheet 연동 (4) (0) | 2025.08.19 |
---|---|
노션 - 구글 Sheet 연동 (3) (2) | 2025.08.18 |
노션 - 구글 Sheet 연동 (2) (1) | 2025.07.23 |
노션 - 구글 Sheet 연동 (1) (1) | 2025.07.21 |