0Pricing
AWS for Backend Developers (EC2, S3, RDS, Lambda) · 课时

CloudWatch 日志与日志组

汇总、监控并存储各类 AWS 服务和自定义应用程序的日志,用于故障排查和审计。

CloudWatch 日志与日志组 是 CoddyKit 上的免费 AWS for Backend Developers (EC2, S3, RDS, Lambda) 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 AWS for Backend Developers (EC2, S3, RDS, Lambda) 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 AWS for Backend Developers (EC2, S3, RDS, Lambda) 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

CloudWatch Logs Intro

Imagine trying to fix a problem in your application without knowing what happened! That's where logs come in.

CloudWatch Logs is an AWS service that helps you collect, monitor, and store logs from your applications and various AWS services.

It's crucial for troubleshooting, auditing, and understanding how your systems are performing.

Log Groups Explained

At the core of CloudWatch Logs is the concept of a Log Group.

Think of a Log Group as a dedicated folder or container for logs that share the same characteristics, like logs from a specific application or service.

For example, all logs from your 'WebApp-Production' service might go into one Log Group.

Log Streams within Groups

Inside each Log Group, you'll find Log Streams.

A Log Stream is a sequence of log events from a single source within that Log Group.

  • For an EC2 instance, each instance might have its own Log Stream.
  • For a Lambda function, each invocation or version could generate a Log Stream.

They help you pinpoint the exact source of your log data.

Setting Up Log Groups

Many AWS services automatically create Log Groups for you when they generate logs, like AWS Lambda or AWS Fargate.

For custom applications, you'll typically create a Log Group manually and then configure your application or the CloudWatch Agent to send logs to it.

You can create and manage Log Groups via the AWS Management Console, AWS CLI, or SDKs.

Ingesting EC2 Logs

To send logs from applications running on an EC2 instance to CloudWatch Logs, you use the CloudWatch Agent.

  • Install the agent on your EC2 instance.
  • Configure it to monitor specific log files (e.g., /var/log/myapp.log).
  • The agent then streams these logs to a specified Log Group and Log Stream in CloudWatch Logs.

Lambda's Automatic Logs

AWS Lambda automatically integrates with CloudWatch Logs! Any output your Lambda function generates (e.g., using print() in Python or console.log() in Node.js) is sent to CloudWatch Logs.

Try running this simple Python Lambda function and check its logs:

import json

def lambda_handler(event, context):
    print("INFO: Lambda function execution started.")
    
    # Simulate processing an event
    if 'detail' in event and 'message' in event['detail']:
        print(f"DEBUG: Processing message: {event['detail']['message']}")
    else:
        print("DEBUG: No specific message in event.")
        
    # Simulate an error condition based on input
    if 'statusCode' in event and event['statusCode'] >= 400:
        print("ERROR: An error status was detected in the input event!")
        return {
            'statusCode': event['statusCode'],
            'body': json.dumps('Error processed!')
        }
    
    print("INFO: Lambda function execution finished successfully.")
    return {
        'statusCode': 200,
        'body': json.dumps('Hello from Lambda!')
    }

Finding Info in Logs

Once your logs are in CloudWatch Logs, you can easily search and filter them.

  • Search: Look for specific keywords, phrases, or patterns across all your log events.
  • Filter: Narrow down results by time range, Log Stream, or even specific fields if your logs are structured (e.g., JSON).

This helps you quickly diagnose issues or find relevant events.

Managing Log Retention

Logs can consume a lot of storage, and you might have compliance requirements for how long to keep them.

Log Retention Policies allow you to define how long CloudWatch Logs should store your log events.

  • Options range from 'Never Expire' to 1 day, 30 days, 1 year, etc.
  • Setting appropriate retention helps manage costs and adhere to data policies.

Log Metrics Filters

Beyond just viewing logs, you can extract meaningful data from them using Metric Filters.

A Metric Filter defines a pattern to look for in your log events (e.g., the word "ERROR" or a specific status code).

When the pattern is matched, CloudWatch increments a custom metric, which you can then use to create alarms (as seen in the previous lesson!).

Log Group Basics

Time for a quick check on CloudWatch Logs!

Which statement best describes the relationship between CloudWatch Log Groups and Log Streams?

Logs Recap

Great job! You've learned the essentials of CloudWatch Logs.

We covered:

  • Log Groups for organizing logs.
  • Log Streams for individual log sources.
  • How to ingest logs from services like EC2 and Lambda.
  • How to search and filter logs efficiently.
  • Configuring retention policies and creating metric filters from logs.

Next, we'll explore CloudTrail for event-driven monitoring and auditing.

常见问题解答

「CloudWatch 日志与日志组」课时是免费的吗?

是的 — 「CloudWatch 日志与日志组」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 AWS for Backend Developers (EC2, S3, RDS, Lambda) 课程的其余内容,请升级到 CoddyKit PRO。 AWS for Backend Developers (EC2, S3, RDS, Lambda) 课程共包含 4 节课。

「CloudWatch 日志与日志组」这节课中我会学到什么?

汇总、监控并存储各类 AWS 服务和自定义应用程序的日志,用于故障排查和审计。 你通过在浏览器中直接运行的动手代码来练习 AWS for Backend Developers (EC2, S3, RDS, Lambda),全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 AWS for Backend Developers (EC2, S3, RDS, Lambda) 需要有经验吗?

无需任何先前经验。CoddyKit 上的 AWS for Backend Developers (EC2, S3, RDS, Lambda) 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。

「CloudWatch 日志与日志组」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 AWS for Backend Developers (EC2, S3, RDS, Lambda) 课中编写并运行代码吗?

能。每节 AWS for Backend Developers (EC2, S3, RDS, Lambda) 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. CloudWatch 指标与告警
  2. CloudWatch 日志与日志组
  3. 使用 CloudTrail 进行事件驱动监控
  4. 构建 CloudWatch 仪表板
← 返回 AWS for Backend Developers (EC2, S3, RDS, Lambda)