Skip to content

상수와 오류

DD 는 로그 레벨 제어, 포맷팅, 오류 처리를 위한 풍부한 상수와 오류 타입을 정의합니다.

로그 레벨

go
type LogLevel int8 // 로그 레벨 타입
상수설명
LevelDebug0디버그 레벨
LevelInfo1정보 레벨 (기본)
LevelWarn2경고 레벨
LevelError3오류 레벨
LevelFatal4치명적 레벨

LogLevelString() string 메서드 ("DEBUG"/"INFO"/"WARN"/"ERROR"/"FATAL" 반환, 알 수 없는 값은 "UNKNOWN") 와 IsValid() bool 메서드 (레벨이 LevelDebug~LevelFatal 유효 범위 내인지 판단) 를 구현합니다.

로그 형식

go
type LogFormat int8 // 출력 형식 타입
상수설명
FormatText0텍스트 형식
FormatJSON1JSON 형식

LogFormatString() string 메서드 ("text"/"json" 반환, 알 수 없는 값은 "unknown") 를 구현합니다.

필드 검증 모드

go
type FieldValidationMode int // 필드 키 검증 모드
상수설명
FieldValidationNone0검증 비활성화 (기본)
FieldValidationWarn1검증 실패 시 경고하되 여전히 수락
FieldValidationStrict2엄격 모드, 검증 실패 시 오류 기록

필드 명명 규칙

go
type FieldNamingConvention int // 필드 키 명명 규칙
상수설명
NamingConventionAny0모든 형식 수락 (기본)
NamingConventionSnakeCase1snake_case(예: user_id)
NamingConventionCamelCase2camelCase(예: userId)
NamingConventionPascalCase3PascalCase(예: UserId)
NamingConventionKebabCase4kebab-case(예: user-id)

해시 알고리즘

go
type HashAlgorithm int // 무결성 서명 해시 알고리즘
상수설명
HashAlgorithmSHA2560SHA-256 알고리즘 (무결성 서명에 사용)

기본값

상수설명
DefaultTimeFormat"2006-01-02T15:04:05Z07:00"ISO 8601 시간 형식
DefaultLogPath"logs/app.log"기본 로그 파일 경로
DefaultMaxSizeMB100기본 파일 크기 제한 (MB)
DefaultMaxBackups10기본 백업 수
DefaultMaxAge30 * 24 * time.Hour기본 보존 일수 (30 일)

컨텍스트 키

상수타입
ContextKeyTraceIDContextKey"trace_id"
ContextKeySpanIDContextKey"span_id"
ContextKeyRequestIDContextKey"request_id"

오류 코드

LoggerError.Code 필드는 머신 판독 가능한 오류 코드 문자열을 포함하여 오류 타입을 세밀하게 매칭하는 데 사용됩니다. 오류 코드는 내부 구현 세부사항이며, 센티넬 오류로 매칭할 것을 권장합니다.

센티넬 오류

각 오류 코드는 센티넬 오류 변수에 해당합니다.

go
var (
    ErrNilConfig          = errors.New("config cannot be nil")
    ErrNilWriter          = errors.New("writer cannot be nil")
    ErrNilFilter          = errors.New("filter cannot be nil")
    ErrNilHook            = errors.New("hook cannot be nil")
    ErrNilExtractor       = errors.New("context extractor cannot be nil")
    ErrLoggerClosed       = errors.New("logger is closed")
    ErrWriterNotFound     = errors.New("writer not found")
    ErrInvalidLevel       = errors.New("invalid log level")
    ErrInvalidFormat      = errors.New("invalid log format")
    ErrMaxWritersExceeded = errors.New("maximum writer count exceeded")
    ErrEmptyFilePath      = errors.New("file path cannot be empty")
    ErrPathTooLong        = errors.New("file path too long")
    ErrPathTraversal      = errors.New("path traversal detected")
    ErrNullByte           = errors.New("null byte in input")
    ErrInvalidPath        = errors.New("invalid file path")
    ErrSymlinkNotAllowed  = errors.New("symlinks not allowed")
    ErrHardlinkNotAllowed = errors.New("hardlinks not allowed")
    ErrOverlongEncoding   = errors.New("UTF-8 overlong encoding detected")
    ErrMaxSizeExceeded    = errors.New("maximum size exceeded")
    ErrMaxBackupsExceeded = errors.New("maximum backup count exceeded")
    ErrBufferSizeTooLarge = errors.New("buffer size too large")
    ErrInvalidPattern     = errors.New("invalid regex pattern")
    ErrEmptyPattern       = errors.New("pattern cannot be empty")
    ErrPatternTooLong     = errors.New("pattern length exceeds maximum")
    ErrReDoSPattern       = errors.New("pattern contains dangerous nested quantifiers that may cause ReDoS")
    ErrPatternFailed      = errors.New("failed to add pattern")
    ErrConfigValidation   = errors.New("configuration validation failed")
    ErrWriterAdd          = errors.New("failed to add writer")
    ErrMultipleConfigs    = errors.New("multiple configs provided, expected 0 or 1")
    ErrNilMultiWriter     = errors.New("multiwriter is nil")
)

오류 검사

go
if errors.Is(err, dd.ErrLoggerClosed) {
    // 로거가 이미 종료됨
}

if errors.Is(err, dd.ErrPathTraversal) {
    // 경로 순회 공격 감지
}

오류 타입

LoggerError

go
type LoggerError struct {
    Code    string
    Message string
    Cause   error
    Context map[string]any
}

메서드: Error(), Unwrap(), Is(target), WithContext(key, value), WithField(key, value)

go
// LoggerError 는 오류 코드, 메시지, 원인, 컨텍스트를 포함
// errors.Is 로 센티넬 오류 검사
if errors.Is(err, dd.ErrLoggerClosed) {
    // 로거가 이미 종료됨
}

WriterError

go
type WriterError struct {
    Index  int
    Writer io.Writer
    Err    error
}

메서드: Error(), Unwrap()

MultiWriterError

go
type MultiWriterError struct {
    Errors []WriterError
}

메서드: Error(), Unwrap(), HasErrors(), ErrorCount(), FirstError()

다음 단계