9

Im currently working on a WebApp created with AWS Amplify, where I have to implement logging. Based on the AWS Amplify Docs there is a built in Logger function, which i tried to implement, but cant seem to find it anywhere in my AWS console.

Does anybody know where to find the logs?

dev.tom
  • 489
  • 2
  • 5
  • 16
  • Cloudwatch logs – BMW May 08 '20 at 08:27
  • That is my issue, it does not appear in the cloudwatch logs, so i was wondering if it was logged somewhere else? – dev.tom May 08 '20 at 08:33
  • where you run the application? ec2, lambda? – BMW May 08 '20 at 12:23
  • Thats a great question, I am testing it locally on my react app, but not sure if that should affect the logging? – dev.tom May 09 '20 at 13:22
  • ic. That's the document you can follow up: https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/install-CloudWatch-Agent-on-premise.html – BMW May 11 '20 at 04:52
  • @dev.tom- did you found the solution to this? - Did you checked the browser console? – Raj Feb 18 '21 at 08:18
  • Same question for me. When I go to https://console.aws.amazon.com/cloudwatch/home?region=us-east-1#logsV2:home and click "View Log Groups" I see many that contains /lambda/amplify. I'm guessing one of those has it, but I don't know which one, or how to search all of them at once. – NealWalters Apr 14 '21 at 04:58
  • 1
    @NealWalters its been a while since I worked on this project/issue, but from what I can remember, I think we ended up with just using regular 'console.log' in our lambdas, and it would show up in the log groups for that specific lambda function. hope this helped. – dev.tom Apr 14 '21 at 08:10

3 Answers3

2

I would like to add another solution.

Yes, you can see them in AWS Cloudwatch Logs, but you need a bit of configuration.

Here the PR which introduced the feature. You already have everything installed if you get the latest Amplify version (quite everything for React Native users, see later).

Setup the add-on:

Amplify.configure({
  Logging: {
    logGroupName: '<loggroup-name>',
    logStreamName: '<logstream-name>',
    region: '<region-code>',
  },
  ...awsconfig,
});

const logger = new Logger('yourloggername', 'DEBUG');
Amplify.register(logger);
logger.addPluggable(new AWSCloudWatchProvider());

Now you can log to cloudwatch:

logger.debug('hello logging');

You need to configure IAM in AWS, I report a working example for simplicity.
Add this policy to the IAM identity you are using to access Cloudwatch:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": "logs:DescribeLogGroups",
            "Resource": "arn:aws:logs:<region-code>:<youraccount>:log-group:*"
        },
        {
            "Sid": "VisualEditor1",
            "Effect": "Allow",
            "Action": [
                "logs:CreateLogGroup",
                "logs:DescribeLogStreams"
            ],
            "Resource": "arn:aws:logs:<region-code>:<youraccount>:log-group:<loggroup-name>:log-stream:"
        },
        {
            "Sid": "VisualEditor2",
            "Effect": "Allow",
            "Action": [
                "logs:CreateLogStream",
                "logs:PutLogEvents"
            ],
            "Resource": "arn:aws:logs:<region-code>:<youraccount>:log-group:<loggroup-name>:log-stream:<logstream-name>"
        }
    ]
}

And you are ready to log to CloudWatch.
I do not know why this feature is not described in the official documentation.

Last but not least, if you are using React Native like me, you will not have util package and TextExncoder() utility. For me it was enought to install a polyfill like this one:

npm install fastestsmallesttextencoderdecoder

and import it as the first line in my index.js:

import 'fastestsmallesttextencoderdecoder';
andreav
  • 541
  • 5
  • 18
  • I don't see it very useful. I want to see any console output of my application. Specifically made with console.log and console.error. Or basically stdoutput and stderror. Obviously not only what I sent to CloudWatch using some custom functions after a lot of configuration. – Роман Коптев May 01 '23 at 17:15
1

Hi all, I see this is an older question and just thought I would provide an answer to assist with anyone who is directed here (with similar questions, specifically in regards to the Amplify utility - Logger) The question appears to be referring more towards logging for historical reasons as opposed to the Logger which logs to the console window and the answer is probably not what the questioner requires, however the question itself could bring a number of people here looking for answers on how to use the Amplify Logger or where to find its output. If so I hope this helps.

Amplify Logger

The simple answer to the question

Where to find AWS Amplify Logger logs

is that Amplify Logger outputs content directly to your web browser console window.

More Details

The Amplify Logger utility logs content to the browser console. While developing your app you can set the global log level of your browser console by typing the following (into your console) or you can set it directly in your code:

window.LOG_LEVEL = 'INFO';

You have several log levels you can choose from including DEBUG, INFO, WARN, ERROR and VERBOSE.

When you call Logger from your code:

logger.info(`user has signed in with ${username}`);

and have set the appropriate log level, you should see the output (in your console):

[INFO] 20:09.950 YourLoggerName - user has signed in with johndoe@example.com

And just for completeness, this is how to use Amplify Logger in an App:

import { Logger } from 'aws-amplify';

const logger = new Logger('YourLoggerName');

const exampleSignIn = () => {
    const { username, password } = inputs;
    logger.info(`user signing in with ${username}`);
    Auth.signIn(username, password)
        .then(user => signInSuccess(user))
        .catch(err => signInError(err));
}

const signInError = (err) => {
    logger.error('error signing in', err); 
    // more code
}  

There are a few more options and ways to use it, as described in the documentation linked in the question, such as setting logging levels etc.

Here is that link again:

https://docs.amplify.aws/lib/utilities/logger/q/platform/js

0

There is this solution: https://github.com/aws-amplify/amplify-js/pull/8309

Even though this pull request was merged and some people claim to have it working. I personally ran into permission trouble and this "feature" isn't named anywhere in the docs of the amplify.aws website.

Leejjon
  • 680
  • 2
  • 7
  • 24