Elastic Beanstalk 으로 백엔드 배포를 하게 되면서 django 내부의 로그가 아닌 aws의 클라우드 워치를 이용해 로그를 관리하기로 했다.
1. 먼저 장고에서 아래의 패키지들을 설치한다.
$ pip install watchtower
$ pip install boto3
AWS_LOG_GROUP='생성한 로그 그룹 이름'
AWS_LOG_STREAM='생성한(또는 생성할) 로그 스트림 이름'
AWS_LOG_GROUP = env('AWS_LOG_GROUP')
AWS_LOG_STREAM = env('AWS_LOG_STREAM')
boto3_client = boto3.client('logs', region_name=AWS_REGION)
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'aws': {
'format': u"%(asctime)s [%(levelname)-8s] %(funcName)s - %(message)s [%(pathname)s:%(lineno)d]",
'datefmt': "%Y-%m-%d %H:%M:%S"
},
'net_aws': {
'format': u"%(asctime)s [%(levelname)-8s] %(funcName)s - %(message)s [%(pathname)s:%(lineno)d]",
'datefmt': "%Y-%m-%d %H:%M:%S"
}
},
'handlers': {
'default': {
'level': 'INFO',
'class': 'watchtower.CloudWatchLogHandler',
'boto3_client': boto3_client,
'log_group': AWS_LOG_GROUP,
'stream_name': AWS_LOG_STREAM,
'formatter': 'aws', # 앞에서 생성한 format
'use_queues': True,
},
'network': {
'level': 'INFO',
'class': 'watchtower.CloudWatchLogHandler',
'boto3_client': boto3_client,
'log_group': AWS_LOG_GROUP,
'stream_name': AWS_LOG_STREAM,
'formatter': 'net_aws', # 앞에서 생성한 format
'use_queues': True,
}
# 추가로 핸들러를 더 생성할 수도 있다. 프로젝트에 맞게 설정
},
'loggers': {
'default-logger': {
'level': 'INFO',
'handlers': ['default'],
'propagate': False,
},
'network-logger': {
'level': 'DEBUG',
'handlers': ['network'],
'propagate': False,
}
# 추가로 로거를 더 생성할 수도 있다. 프로젝트에 맞게 설정
},
}
log = logging.getLogger('default-logger')
net_log = logging.getLogger('network-logger')
# example
from configs.settings import log, net_log
class some_class_view:
...
def some_func(request):
net_log.info('network log message')
log.info('log message')
...
return