Bash 로깅
컬러 지원, 이모지 아이콘 및 CI/CD 통합을 포함한 bash 스크립트용 구조화된 로깅 패턴입니다.
기본 로깅 함수
간단하고 이식 가능한 로깅 구현:
# 색상 정의
declare -A colors=(
[green]=$'\033[0;32m'
[red]=$'\033[0;31m'
[yellow]=$'\033[1;33m'
[blue]=$'\033[0;34m'
[reset]=$'\033[0m'
)
declare -A emojis=(
[success]='✅'
[error]='❌'
[warning]='⚠️'
[info]='ℹ️'
[debug]='🐛'
)
# 로깅 함수
print_success() {
printf '%b %b%b%b\n' "${emojis[success]}" "${colors[green]}" "$*" "${colors[reset]}"
}
print_error() {
printf '%b %b%b%b\n' "${emojis[error]}" "${colors[red]}" "$*" "${colors[reset]}" >&2
}
print_warning() {
printf '%b %b%b%b\n' "${emojis[warning]}" "${colors[yellow]}" "$*" "${colors[reset]}"
}
print_info() {
printf '%b %b\n' "${emojis[info]}" "$*"
}
print_debug() {
[[ -n "${DEBUG:-}" ]] && printf '%b %b%b%b\n' "${emojis[debug]}" "${colors[blue]}" "$*" "${colors[reset]}"
}
로그 레벨
설정 가능한 로그 레벨을 구현합니다.
TTY 감지
비대화형 환경에서 색상을 비활성화:
setup_colors() {
if [[ -t 1 ]] && [[ -z "${NO_COLOR:-}" ]]; then
# 터미널이 색상 지원
COLOR_RED=$'\033[0;31m'
COLOR_GREEN=$'\033[0;32m'
COLOR_YELLOW=$'\033[1;33m'
COLOR_BLUE=$'\033[0;34m'
COLOR_RESET=$'\033[0m'
else
# 색상 미지원
COLOR_RED=''
COLOR_GREEN=''
COLOR_YELLOW=''
COLOR_BLUE=''
COLOR_RESET=''
fi
}
# CI 환경은 종종 색상을 지원합니다.
detect_color_support() {
if [[ -n "${CI:-}" ]] || [[ -n "${GITLAB_CI:-}" ]] || [[ -n "${GITHUB_ACTIONS:-}" ]]; then
return 0 # CI 환경, 색상 활성화
elif [[ -t 1 ]]; then
return 0 # 터미널, 색상 활성화
else
return 1 # 색상 미지원
fi
}
GitLab CI 접을 수 있는 섹션
GitLab CI에서 접을 수 있는 로그 섹션 생성:
section_start() {
local section_key="\${1:-section}"
local section_header="\${2:-$section_key}"
local collapsed="\${3:-true}"
if [[ -n "${GITLAB_CI:-}" ]]; then
printf "\e[0Ksection_start:%s:%s[collapsed=%s]\r\e[0K%s\n" \
"$(date +%s)" "$section_key" "$collapsed" "$section_header"
else
printf '\n=== %s ===\n' "$section_header"
fi
}
section_end() {
local section_key="\${1:-section}"
if [[ -n "${GITLAB_CI:-}" ]]; then
printf "\e[0Ksection_end:%s:%s\r\e[0K" "$(date +%s)" "$section_key"
else
printf '\n'
fi
}
# 사용법
section_start "build" "애플리케이션 빌드"
make build
section_end "build"
GitHub Actions 그룹화
진행 표시기
구조화된 단계 로깅
Shlocksmith 로깅 라이브러리
완전한 CI 통합을 위한 포괄적인 로깅을 원한다면 shlocksmith 로깅 라이브러리를 사용하십시오.
기능
- 20개 이상의 로그 레벨 함수 (log_info, log_error, log_warning 등)
- 단계 기반 로깅 (log_step_start, log_step_pass, log_step_fail)
- TTY 감지를 통한 색상 및 이모지 지원
- GitLab CI 섹션 관리
- TUI용 박스 드로잉 문자
- 키-값 쌍 포맷팅
사용 가능한 함수
# 기본 로깅
log_info "정보 메시지"
log_warning "경고 메시지"
log_error "오류 메시지"
log_debug "디버그 메시지" # DEBUG가 설정된 경우에만 표시
log_notice "알림 메시지"
log_fatal "치명적 오류" # 스크립트 종료
# 단계 기반 로깅
log_start "프로세스 이름"
log_step_start "단계 설명"
log_step_pass "성공 메시지"
log_step_fail "실패 메시지"
log_step_skip "건너뜀 메시지"
log_step_done "완료 메시지"
log_done "프로세스 완료"
# 결과
log_pass "테스트 통과"
log_fail "테스트 실패"
log_result "결과 상세"
log_success "성공 메시지"
# CI 섹션
section_start "section_id" "섹션 제목"
section_end "section_id"
사용법
#!/usr/bin/env bash
source /path/to/log_functions.sh
log_start "배포 프로세스"
section_start "deps" "의존성 설치"
log_step_start "패키지 설치 중"
if apt-get install -y package; then
log_step_pass "패키지 설치 완료"
else
log_step_fail "패키지 설치 실패"
fi
section_end "deps"
log_done "배포 완료"
추가 리소스
스크립트
- log_functions.sh - 전체 shlocksmith 로깅 라이브러리
색상 참조
| 코드 | 색상 |
|---|---|
\033[0;30m | 검정 |
\033[0;31m | 빨강 |
\033[0;32m | 초록 |
\033[0;33m | 노랑 |
\033[0;34m | 파랑 |
\033[0;35m | 자주 |
\033[0;36m | 청록 |
\033[0;37m | 흰색 |
\033[1;XXm | 굵은 변형 |
\033[0m | 초기화 |