问题
I have some "Python Shell" type Glue jobs and I want to send the job logs to a custom CloudWatch log group instead of the default log group. I am able to achieve this for "Spark" type glue jobs by providing job parameters as below:
"--enable-continuous-cloudwatch-log" = true
"--continuous-log-logGroup" = "/aws-glue/jobs/glue-job-1"
but the same parameters doesn't work for Python Shell jobs (logs still going to the default log groups /aws-glue/python-jobs/output and /aws-glue/python-jobs/error). Is there any way to achieve this for Python Shell jobs?
回答1:
continuous-log-logGroup
is something that comes with AWS Glue Spark jobs and it's not available to Python Shell jobs. The closest thing you can do is to configure a log handler that writes to CloudWatch. Watchtower is a popular one:
import watchtower, logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
logger.addHandler(watchtower.CloudWatchLogHandler(log_group='watchtower', stream_name='bla'))
logger.info("Hi")
logger.info(dict(foo="bar", details={}))
You can also directly use Cloudwatch Logs API:
logs = boto3.client('logs')
LOG_GROUP='TUTORIAL-DEV2'
LOG_STREAM='stream1'
logs.create_log_group(logGroupName=LOG_GROUP)
logs.create_log_stream(logGroupName=LOG_GROUP, logStreamName=LOG_STREAM)
timestamp = int(round(time.time() * 1000))
response = logs.put_log_events(
logGroupName=LOG_GROUP,
logStreamName=LOG_STREAM,
logEvents=[
{
'timestamp': timestamp,
'message': time.strftime('%Y-%m-%d %H:%M:%S')+'\tHello world, here is our first log message!'
}
]
)
This example was from this gist: https://gist.github.com/olegdulin/fd18906343d75142a487b9a9da9042e0
来源:https://stackoverflow.com/questions/61625190/how-to-use-a-cloudwatch-custom-log-group-with-python-shell-glue-job