0

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

Mohan vel
  • 505
  • 9
  • 29
  • 1
    I'm not sure if I understood correctly, but if you want to log something from lambda to cloudwatch, you simply log it out to console. `System.out.println("your log")` should do the work. for logging lambda also needs `'logs:CreateLogGroup'`, `'logs:CreateLogStream'`, `'logs:PutLogEvents'`, `'logs:DescribeLogStreams',` permissions in execution role – Piekarski D Jan 04 '20 at 15:23
  • Previously we use to log all the exceptions in Elasticsearch for that we use a separate class file and pass the exception stackrace and few attributes as parameters and the method we created will insert the document in Elasticsearch – Mohan vel Jan 04 '20 at 18:32
  • In our new approach instead of elastic search we have to log the exception occurred with in java service in AWS cloud watch – Mohan vel Jan 04 '20 at 18:34
  • Should i have to call the lamada function inside my java code to log the exception in AWS cloudwatch? – Mohan vel Jan 04 '20 at 18:35
  • I hope you understand my requirement in case if i'am not clear please let me know and thanks for your previous post – Mohan vel Jan 04 '20 at 18:37

1 Answers1

3

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.

Mark B
  • 183,023
  • 24
  • 297
  • 295