如何检查 Docker 容器微服务中的 python-logging 日志

use*_*892 0 python docker docker-compose python-logging

我刚刚开始使用 Docker。

我正在开发另一个开发人员编码的项目。在项目 Docker 容器中,我有三个微服务(aggregatore、classificatore、testmicro),每个服务都使用 python 模块logging进行调试。

我的问题是我不知道在哪里可以查看输出logging

docker-compose.yml

version: '2.1'
services:
  files:
    image: busybox
    volumes:
     [..]

  grafana:
     [..]

  prometheus:
     [..]

  aggregatore:
   [..] 

  classificatore:
    build: classificatore/.
    volumes:    
      - [..]
    volumes_from: 
      - files
    ports: 
      - [..]
    command: ["python", "/src/main.py"]
    depends_on: 
      rabbit:
        condition: service_healthy

  testmicro:
    [..]    
  rabbit:
    [..]
Run Code Online (Sandbox Code Playgroud)

我是终端,我运行

$docker-compose up -d
Run Code Online (Sandbox Code Playgroud)

这将启动所有微服务。

让我们关注分类器服务。

分类器/Dockerfile

FROM python:3
RUN mkdir /src
ADD requirements.txt /src/.
WORKDIR /src
RUN pip install -r requirements.txt
ADD . /src/.
RUN mkdir -p /tmp/reqdoc
CMD ["python", "main.py"]
Run Code Online (Sandbox Code Playgroud)

分类器/main.py

import logging

logging.basicConfig(format='%(asctime)s %(message)s', level=logging.DEBUG)
logging.getLogger('pika').setLevel(logging.WARNING)
log = logging.getLogger()
[..]
app = Flask( __name__ , template_folder='./web')

@app.route("/")
def index(message=None):
    log.info("classificatore index!! ")
    [..]
    return render_template('index.html', files1=files1, files2=files2, message=message)
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,输出文本“ classificatore index”去了哪里?

感谢您提供的任何支持。

LeC*_*oux 5

docker logs classificatore

另一种方法是docker exec -it classificatore bash然后在你的容器中闲逛

  • 使用 `docker container ps` 并检查所需的容器 ID 或容器名称,并将其传递给 `docker log` 命令。如果您想“实时”检查您的日志,请查看我的答案。 (3认同)