ImageUs-로깅

codakcodak·2023년 3월 3일
0

ImageUs

목록 보기
14/17
post-thumbnail

문제상황

  • gunicorn을 통해 app을 운영할 때 로깅이 제대로 안 된 상태에서 에러가 난다면 어느 코드에서 에러가 났는지 원인을 알기가 힘들다.

해결방법

  • gunicorn의 로깅을 보여주는 인자인 --log-level,--capture-output,--error-logfile인자 옵션과
    app단에서 print옵션에 in_flush를 통해 상세한 로그 표현

적용과정

  • app.py
    @app.before_request
    def block_method():
        if 'x-real-ip' in request.headers:
            ip=request.headers['x-real-ip']
        else:
            ip=request.environ.get('REMOTE_ADDR')
        print("Current IP Address:",ip,flush=True)
        print("Current Process:",os.getpid(),flush=True)

        splited_ip=ip.split('.')
        ip_range=splited_ip[0]+'.'+splited_ip[1]
    
        if ip not in app.config['GOOD_IP_LIST'] and ip_range not in app.config['GOOD_IP_RANGE']:
            abort(403)

*위는 app에 접근하는 사용자의 ip를 추출하여 print를 통해 로깅한다.이때 print옵션에 flush=True를 줘야 로그에 출력된다.

  • gunicorn 실행 시 로그 옵션 설정
gunicorn --worker-class=gevent --worker-connections=1000 --workers 9 --bind unix:/run/imageus_back.sock --log-level=debug --capture-output --error-logfile error.log --max-requests 1000 --max-requests-jitter 1000 --timeout 5 -m 007 app:create_app()

*log-level은 디버그레벨까지 설정,error-logfile은 로그 파일의 이름을 설정,capture-output는 print로 출력시킬 로그 설정

  • 로깅파일 확인

*tail -f는 가장 최근 10줄부터 모니터링하는 명령어 이다.파일 이름은 gunicorn의 error-logfile인자에 입력했던 파일 이름을 넣어준다.

*flush옵션을 넣어줬던 print를 통해 사용자가 요청마다 사용자의 ip를 출력하도록 로깅을 구현해놨다.

결과

  • 디버깅레벨까지 로깅을 하기때문에 에러가 나면 정확한 지점을 찾아 고칠 수 있다.
  • print를 통한 디버깅을 통해 사용자의 요청때마다 관찰해야하는 데이터를 쉽게 볼 수 있다.
profile
숲을 보는 코더

0개의 댓글