简体   繁体   中英

Logging PHP API Request Info from Docker container

I'm working on a project where I have an iOs app connecting to a PHP API. I want to log all incoming requests to the website service for development purposes (ie I need a solution that can turn off and based on an environment variable). The API is run in a docker container, which is launched as a docker-compose service.

The PHP API is not using any sort of MVC framework.

My PHP experience is limited, so I know I've got some research ahead of me, but in the meantime, I'd appreciate any jump start to the following questions:

  • Is there a composer library that I can plug into my PHP code that will write to a tailed log?
  • Can I plug anything at the nginx or php-fpm container level so that requests to those containers are logged before even getting to the PHP code?
  • Is there anything I need to configure to in either nginx or php-fpm containers to ensure that logs are tailed when I run docker-compose up ?

Here are my logging needs:

  • request method
  • request URL
  • GET query parameters, PUT and POST parameters (these will be in JSON format)
  • response code
  • response body
    • The logs I'm interested are all application/json. However, I don't mind the kitchen sink option where anything out gets logged.
  • request and response headers
    • I will not need these 99% of the time, so they aren't a need. But it'd be nice to configure them off/on.

Below is the docker-compose.yml file.

version: '2'
services:

  gearman:
    image:gearmand

  redis:
    image: redis

  database:
    image: mariadb
    environment:
     MYSQL_ROOT_PASSWORD: ******
     MYSQL_DATABASE: database
    volumes:
     - dbdata:/var/lib/mysql
    ports:
      - 3306:3306

  sphinx:
    image:sphinx
    links:
      - database

  memcached:
    image: memcached
    ports:
     - "11211:11211"

  php_fpm:
    image:php-fpm
    links:
     - redis
     - memcached
     - database
    environment:
     REDIS_SERVER: redis
     DATABASE_HOST: database
     RW_DATABASE_HOST: database
     RO_DATABASE_HOST0: database
     DATABASE_USER: root
     DATABASE_PASS: ******
    volumes:
     - ./website:/var/www/website/
     - /var/run

  nginx:
    image:nginx
    links:
     - php_fpm
    ports:
     - "80:80"
    volumes_from:
      - php_fpm

volumes:
  dbdata:
    driver: local

The container was logging using all the default settings, but my client was pointing to another server, but just to leave a trail.

Inside php_fpm container ( docker exec -it dev_php_fpm_1 /bin/bash ), you can cat /etc/php5/fpm/php.ini , which shows the default error_log settings:

; Log errors to specified file. PHP's default behavior is to leave this value
; empty.
; http://php.net/error-log
; Example:
;error_log = php_errors.log
; Log errors to syslog (Event Log on Windows).
;error_log = syslog

Just user error_log function to write to default OS logger.

Log your the php_fpm service with docker logs -f -t dev_php_fpm_1 .


Update:

In case error_log is truncated for your purposes, you can also simply write a function

file_put_contents(realpath(dirname(__FILE__)) . './requests.log', $msg, FILE_APPEND);

and then tail it: tail -f ./requests.log - either from inside the container or from outside the container if you are using a local volumes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM