공부하기싫어
article thumbnail

ethereum_autotrade.py 를 nohup 으로 실행할때 발생하는 로그들을

flush 모듈을 사용해 바로바로 output.log 에 기록하고 그날 프로그램이 종료할때

s3 버킷에 저장하도록 해보자

 

S3 버킷 생성

ACL 비활성화

모든 퍼블릭 액세스 차단

버전 관리 비활성화

 

테스트

import os
import boto3

def send_logs_to_s3():
    bucket_name="logs-ethereum-autotrade"
    session = boto3.Session(profile_name='default')

    s3=session.client('s3')
    f=open("test.log", 'rb')
    s3.upload_fileobj(f, bucket_name, "test.log")

send_logs_to_s3()

성공

 

python 코드 수정

nohup 으로 백그라운드 실행 시 중간에 log 파일이 기록되지 않아 실시간으로 output.log 파일에 로그가 기록되도록 모듈을 만들었음

 

log_defs.py

import boto3
import os

def open_logfile(n):
    log_file_name=n
    f=open(log_file_name, 'a')
    return f

def write_and_flush_logs(f, log_string):
    logs=log_string+"\n"
    f.write(logs); f.flush()

def close_logfile(f):
    f.close()

def send_logs_to_s3(n):
    bucket_name="logs-ethereum-autotrade"
    file_path=n
    session = boto3.Session(profile_name='default')

    s3=session.client('s3')
    f=open(file_path, 'rb')
    s3.upload_fileobj(f, bucket_name, file_path)

 

ethereum_autotrade.py

import pyupbit
import datetime
import aws_defs as m_aws
import upbit_defs as m_upbit
import log_defs as m_log


def main():
    d = datetime.datetime.now()
    year=str(d.strftime("%Y"))
    month=str(d.strftime("%m"))
    day=str(d.strftime("%d"))
    log_file_name="output"+year+month+day+".log"

    logfile=m_log.open_logfile(log_file_name)

    try :
        upbit_access_key, upbit_secret_key = m_aws.get_parameter_fromSSM()
        bestk,endprice=m_aws.read_dynamoDB_table()
        logs="success : get_parameter_fromSSM, read_dynamoDB_table"
        m_log.write_and_flush_logs(logfile, logs)
    except Exception as e:
        logs="failure : get_parameter_fromSSM, read_dynamoDB_table"
        m_log.write_and_flush_logs(logfile, logs)
        logs="Exception : "+str(e)
        m_log.write_and_flush_logs(logfile, logs)

    try :
        upbit_login = pyupbit.Upbit(upbit_access_key, upbit_secret_key)
        logs="success : login"
        m_log.write_and_flush_logs(logfile, logs)
    except Exception as e:
        logs="failure : login"
        m_log.write_and_flush_logs(logfile, logs)
        logs="Exception : "+str(e)
        m_log.write_and_flush_logs(logfile, logs)
    
    try :
        m_upbit.autotrade(upbit_login, bestk, endprice, logfile)
    except Exception as e:
        logs="failure : autotrade"
        m_log.write_and_flush_logs(logfile, logs)
        logs="Exception : "+str(e)
        m_log.write_and_flush_logs(logfile, logs)
    
    m_log.close_logfile(logfile)
    m_log.send_logs_to_s3(log_file_name)

main()

 

음... 코드가 좀 더러워졌는데...

여기다가 x-ray 세그먼트까지 더하면 더해지겠는걸