日志流是随机散列而不是记录器名称

Log streams are random hash instead of logger name

所以最近我将我的应用移到了 docker 容器中。

我注意到,日志组的日志流将其名称更改为一些随机哈希值。

移动到 docker 之前:

搬到docker后:

每个文件中的记录器初始化为

logger = logging.getLogger(__name__)

记录器的配置在 __main__

中设置
def setup_logger(config_file):
    with open(config_file) as log_config:
        config_yml = log_config.read()
        config_dict = yaml.safe_load(config_yml)
        logging.config.dictConfig(config_dict)

使用从此文件加载的配置

version: 1
disable_existing_loggers: False
formatters:
  json:
    format: "[%(asctime)s] %(process)d %(levelname)s %(name)s:%(funcName)s:%(lineno)s - %(message)s"
  plaintext:
    format: "%(asctime)s %(levelname)s %(name)s - %(message)s"
    datefmt: "%Y-%m-%d %H:%M:%S"
handlers:
  console:
    class: logging.StreamHandler
    formatter: plaintext
    level: INFO
    stream: ext://sys.stdout
root:
  level: DEBUG
  propagate: True
  handlers: [console]

docker 图像 运行 带有标志

--log-driver=awslogs \
      --log-opt awslogs-group=XXXXX \
      --log-opt awslogs-create-group=true \

有没有办法保留原始日志流名称?

这就是 awslogs 驱动程序的工作方式。

根据 documentation,您可以使用 awslogs-stream-prefix 选项稍微控制名称:

The awslogs-stream-prefix option allows you to associate a log stream with the specified prefix, the container name, and the ID of the Amazon ECS task to which the container belongs. If you specify a prefix with this option, then the log stream takes the following format:

prefix-name/container-name/ecs-task-id

If you don't specify a prefix with this option, then the log stream is named after the container ID that is assigned by the Docker daemon on the container instance. Because it is difficult to trace logs back to the container that sent them with just the Docker container ID (which is only available on the container instance), we recommend that you specify a prefix with this option.

如果您使用的是 awslogs 驱动程序,则无法更改此行为。唯一的选择是禁用日志驱动程序并使用 AWS SDK 手动将事件放入 CloudWatch,但我认为这不是一个好主意。

明确地说,您的容器 settings/code 在使用 awslogs 时根本不会影响流名称 - 日志驱动程序只是将容器的所有 STDOUT 重定向到 CloudWatch。