dev.tom
dev.tom

Reputation: 679

Where to find AWS Amplify Logger logs

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?

Upvotes: 12

Views: 19375

Answers (4)

Nathan Parrish
Nathan Parrish

Reputation: 31

I struggled with this as well; I'm frankly new with lambda/serverless, but needed to deploy a very simple API endpoint in node.js via Amplify. Note that what I'm describing is for a Backend environment, that is, code which runs serverless, not connected to your browser (previous answer mentions "Amplify Logger outputs content directly to your web browser console window" - that's not what I'm talking about here).

The directions previously given about Cloudwatch are correct but insufficient; I could not find the "log group" corresponding to my amplify app. It took considerable hunting through the AWS console to find a direct link to my logs -- and I discovered I could not find the corresponding log group through the Cloud Watch panel.

Ultimately I found joy via the following path starting from the Amplify console:

  1. Go to my app
  2. Chose Backend environments "tab" (I have no Hosting environments so this happens automatically for me)
  3. Click on Functions (under Categories added)
  4. Here you see a window which purports to show logs. Hah!
  5. Click "View in CloudWatch" link above this blank "log" window

This gets me to the relevant log group, where I can see my log streams and "tail."

Note that I am able to see what I'm logging in my node.js with simple console.log() calls. I did not set up any of the additional configuration described in other answers (but it's probable that I'm executing my code with a role which already has the required permissions which are explicitly recommended, you may not be so lucky).

Upvotes: 3

andreav
andreav

Reputation: 567

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';

Upvotes: 3

Leejjon
Leejjon

Reputation: 756

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.

Upvotes: 0

Jason Williams
Jason Williams

Reputation: 29

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 [email protected]

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

Upvotes: 1

Related Questions