Can't get Laravel logs through stdout of Docker Container

i am trying to get logs from Laravel through the stdout of my PHP Container, but when i simulate and error inside the container:

fernando@c64d676157c2:/var/www$ php artisan command5
Command "command5" is not defined.  
fernando@c64d676157c2:/var/www$ 

The error is writed in laravel.log:

fernando@c64d676157c2:/var/www$ tail storage/logs/laravel.log
"} 
[2022-02-22 12:43:52] dev.ERROR: Command "command5" is not defined. {"exception":"[object] (Symfony\\Component\\Console\\Exception\\CommandNotFoundException(code: 0): Command \"command5\" is not defined. at /var/www/vendor/symfony/console/Application.php:644)
[stacktrace]
#0 /var/www/vendor/symfony/console/Application.php(228): Symfony\\Component\\Console\\Application->find('command5')
#1 /var/www/vendor/symfony/console/Application.php(140): Symfony\\Component\\Console\\Application->doRun(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#2 /var/www/vendor/laravel/framework/src/Illuminate/Console/Application.php(93): Symfony\\Component\\Console\\Application->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#3 /var/www/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(129): Illuminate\\Console\\Application->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#4 /var/www/artisan(37): Illuminate\\Foundation\\Console\\Kernel->handle(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#5 {main}
"} 
fernando@c64d676157c2:/var/www$ 

But the error is not showed in stdout of the PHP Container:

fernando@notebookdell-1487   /lab/treinamento/travellist-laravel-demo     main   docker logs -f travellist-app                                                               SIGINT(2) ↵  10345  09:44:30  
[22-Feb-2022 12:40:57] NOTICE: [pool www] 'user' directive is ignored when FPM is not running as root
[22-Feb-2022 12:40:57] NOTICE: [pool www] 'user' directive is ignored when FPM is not running as root
[22-Feb-2022 12:40:57] NOTICE: [pool www] 'group' directive is ignored when FPM is not running as root
[22-Feb-2022 12:40:57] NOTICE: [pool www] 'group' directive is ignored when FPM is not running as root
[22-Feb-2022 12:40:57] NOTICE: fpm is running, pid 1
[22-Feb-2022 12:40:57] NOTICE: ready to handle connections

I tried a lot of combinations of configurations of php.ini file, config/logging.php, LOG_CHANNEL of .env, many options of stack configuration and much more.

I am using the PHP version 7.4.28. laravel/framework v7.11.0 Composer version 2.2.6

The project was cloned from Github: https://github.com/do-community/travellist-laravel-demo

All the containers are up and running:

fernando@notebookdell-1487   /lab/treinamento/travellist-laravel-demo     main   docker container ls                                                                                     10344  09:40:58  
CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS         PORTS                                   NAMES
c64d676157c2   travellist     "docker-php-entrypoi…"   5 seconds ago   Up 3 seconds   9000/tcp                                travellist-app
2995a82595f7   mysql:5.7      "docker-entrypoint.s…"   5 seconds ago   Up 3 seconds   3306/tcp, 33060/tcp                     travellist-db
736ede87e337   nginx:alpine   "/docker-entrypoint.…"   5 seconds ago   Up 3 seconds   0.0.0.0:8000->80/tcp, :::8000->80/tcp   travellist-nginx
 fernando@notebookdell-1487   /lab/treinamento/travellist-laravel-demo     main                                                                                                           10345  09:41:01  

How can i get the logs from Laravel in the stdout of my Docker Container?

Upvotes: 2

Views: 5095

Answers (1)

Editing.

For anyone having a similar problem, when trying to get Laravel logs from Container stdout. In my case, I use the container with Laravel in a Kubernetes Pod, so I added the third step I needed to do.

Solution:

1 - Create logging.php file and set the stack with the single channel.

[... rest of the code omitted, for short...]

'channels' => [
    'stack' => [
        'driver' => 'stack',
        'channels' => ['single'],
        'ignore_exceptions' => false,
    ],

    'single' => [
        'driver' => 'single',
        'path' => storage_path('logs/laravel.log'),
        'level' => 'debug',
        'permission' => 0660,

[... rest of the code omitted, for short...]

2 - Configure the LOG_CHANNEL environment variable with the stack value:

LOG_CHANNEL=stack

3 - Use the Lifecycle that does the tail in the log file:

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: $K8S_DEPLOY_NAME_PHP
  namespace: $K8S_NAMESPACE
  labels:
    app: $K8S_APP_NAME_PHP
    role: $K8S_APP_ROLE
  annotations:
    co.elastic.logs/enabled: $ELASTIC_ANNOTATIONS_HINTS
spec:
  replicas: $K8S_APP_SCALE_MIN
  selector:
    matchLabels:
      app: $K8S_APP_NAME_PHP
      role: $K8S_APP_ROLE
  template:
    metadata:
      labels:
        app: $K8S_APP_NAME_PHP
        role: $K8S_APP_ROLE
      annotations:
        co.elastic.logs/enabled: $ELASTIC_ANNOTATIONS_HINTS
    spec:
      containers:
      - name: $K8S_DEPLOY_NAME_PHP
        image: $K8S_DEPLOY_IMAGE_NAME_PHP:$K8S_DEPLOY_IMAGE_TAG_PHP
        lifecycle:
            postStart:
              exec:
                command:
                    - /bin/bash
                    - -c
                    - >
                        touch /var/www/storage/logs/laravel.log &&
                        tail -f /var/www/storage/logs/laravel.log > /proc/1/fd/2 &
        resources:
          requests:
            cpu: $K8S_REQUESTS_CPU
            memory: $K8S_REQUESTS_MEMORY
          limits:
            cpu: $K8S_LIMITS_CPU
            memory: $K8S_LIMITS_MEMORY
        ports:
        - containerPort: $K8S_DEPLOY_IMAGE_PORT_PHP
        envFrom:
        - configMapRef:
            name: $K8S_DEPLOY_NAME_PHP

Upvotes: 3

Related Questions