Search code examples
javaaws-lambdaaws-java-sdkelasticsearch-java-apiaws-cloudwatch-log-insights

Logging exception of my java service in AWS cloudwatch


I have to log the exception occurred in my java service in AWS cloud-watch

Here is my sample service

@GetMapping(value = "/GetUrl")
    public String getURL(String key) throws FileNotFoundException
    {
        try {
            return S3client.getUrl("xxxx", "xxxx.pdf").toString();
        }
        catch (AmazonS3Exception exception){
            if(exception.getStatusCode() == 404){
                throw new FileNotFoundException(key);
            }
            else{
                throw exception;
            }
        }
    }

If my catch block executed instead of throwing the exception have to call a class file to log the exception in AWS cloudwatch

how to achieve this functionality?

My java service will be going to be deployed in AWS instance docker container

Also if anyone has a better idea to achieve this functionality in some other way also please suggest


Solution

  • Usually you would just log to a local log file using standard Java logging tools, and a CloudWatch Logs Agent configured on the host machine would periodically send those logs messages to CloudWatch Logs.

    If however you want to send the log to CloudWatch Logs directly from your application code, you could use the CloudWatchLogsClient in the AWS SDK for Java.