Laravel:在 Apache 服务器中运行应用程序时,将“error_log(...)”生成的消息记录到终端

Laravel:在 Apache 服务器中运行应用程序时,将“error_log(...)”生成的消息记录到终端

问题是这样的:

当我从 PHP 服务器运行 Laravel 应用程序时,就是这样,使用php artisan serve,我可以看到在终端中打印由“error_log(...)”生成的每条消息,如下所示:

leandro@leandro-Lenovo-B50-10:~/laravel-leaflet-example$ php artisan serve
Illuminate\Foundation\Application
/home/leandro/laravel-leaflet-example/bootstrap
Log desde 'register' de AppServiceProvider
Log desde 'register' de TelescopeServiceProvider. Este método se ejecuta ni bien corremos el server

Log desde 'boot' de AppServiceProvider
Illuminate\Foundation\Application
Log desde 'boot' de AuthServiceProvider. Este método se ejecuta ni bien corremos el server

Tipo de $this->policies():
array


App\Policies\OutletPolicy
App\Policies\ModelPolicy


App\Outlet
App\Policies\OutletPolicy
App\Model
App\Policies\ModelPolicy
Fin de Log desde 'boot' de AuthServiceProvider

   INFO  Server running on [http://127.0.0.1:8000].  

  Press Ctrl+C to stop the server

   WARN  Illuminate\Foundation\Application.  

   WARN  bootstrap.  

   WARN  Log desde 'register' de AppServiceProvider.  

   WARN  Log desde 'register' de TelescopeServiceProvider. Este método se ejecuta ni bien corremos el server.  

   WARN  Log desde 'boot' de AppServiceProvider.  

   WARN  Illuminate\Foundation\Application.  

然而,当我尝试在 Apache 服务器(而不是 PHP 服务器)上实现类似的功能时,我发现了一些麻烦。

phpinfo();通过在测试文件中运行该函数,我得到了以下行:

加载配置文件 | /etc/php/8.2/apache2/php.ini

然后,通过将该行添加error_log = /home/leandro/log/php_errors.log 到该文件,然后重新启动 Apache,我可以通过在控制台中执行以下操作来获得类似的结果:
tail -n 0 -f /home/leandro/log/php_errors.log

尽管这种方法可行,但它意味着大量的磁盘 I/O 操作等,从长远来看,性能不佳并且可能对磁盘造成损害。

我试图将“php.ini”中提到的行替换为,error_log = /dev/stderr然后在终端中执行命令:
tail -f /dev/stderrcat /dev/stderr,但它们都不起作用,即使使用前缀sudo

有人知道如何实现我想要的结果吗?

如果需要更多信息,或者有些内容不太理解,请告诉我。

非常感谢!
Leandro

编辑:

我的“config/logging.php”:

<?php

use Monolog\Handler\NullHandler;
use Monolog\Handler\StreamHandler;
use Monolog\Handler\SyslogUdpHandler;

return [

    /*
    |--------------------------------------------------------------------------
    | Default Log Channel
    |--------------------------------------------------------------------------
    |
    | This option defines the default log channel that gets used when writing
    | messages to the logs. The name specified in this option should match
    | one of the channels defined in the "channels" configuration array.
    |
    */

    'default' => env('LOG_CHANNEL', 'stack'),

    /*
    |--------------------------------------------------------------------------
    | Deprecations Log Channel
    |--------------------------------------------------------------------------
    |
    | This option controls the log channel that should be used to log warnings
    | regarding deprecated PHP and library features. This allows you to get
    | your application ready for upcoming major versions of dependencies.
    |
    */

    'deprecations' => [
        'channel' => env('LOG_DEPRECATIONS_CHANNEL', 'null'),
        'trace' => false,
    ],

    /*
    |--------------------------------------------------------------------------
    | Log Channels
    |--------------------------------------------------------------------------
    |
    | Here you may configure the log channels for your application. Out of
    | the box, Laravel uses the Monolog PHP logging library. This gives
    | you a variety of powerful log handlers / formatters to utilize.
    |
    | Available Drivers: "single", "daily", "slack", "syslog",
    |                    "errorlog", "monolog",
    |                    "custom", "stack"
    |
    */

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

        'single' => [
            'driver' => 'single',
            'path' => storage_path('logs/laravel.log'),
            'level' => env('LOG_LEVEL', 'debug'),
        ],

        'daily' => [
            'driver' => 'daily',
            'path' => storage_path('logs/laravel.log'),
            'level' => env('LOG_LEVEL', 'debug'),
            'days' => 14,
        ],

        'slack' => [
            'driver' => 'slack',
            'url' => env('LOG_SLACK_WEBHOOK_URL'),
            'username' => 'Laravel Log',
            'emoji' => ':boom:',
            'level' => env('LOG_LEVEL', 'critical'),
        ],

        'papertrail' => [
            'driver' => 'monolog',
            'level' => env('LOG_LEVEL', 'debug'),
            'handler' => env('LOG_PAPERTRAIL_HANDLER', SyslogUdpHandler::class),
            'handler_with' => [
                'host' => env('PAPERTRAIL_URL'),
                'port' => env('PAPERTRAIL_PORT'),
                'connectionString' => 'tls://'.env('PAPERTRAIL_URL').':'.env('PAPERTRAIL_PORT'),
            ],
        ],

        'stderr' => [
            'driver' => 'monolog',
            'level' => env('LOG_LEVEL', 'debug'),
            'handler' => StreamHandler::class,
            'formatter' => env('LOG_STDERR_FORMATTER'),
            'with' => [
                'stream' => 'php://stderr',
            ],
        ],

        'syslog' => [
            'driver' => 'syslog',
            'level' => env('LOG_LEVEL', 'debug'),
        ],

        'errorlog' => [
            'driver' => 'errorlog',
            'level' => env('LOG_LEVEL', 'debug'),
        ],

        'null' => [
            'driver' => 'monolog',
            'handler' => NullHandler::class,
        ],

        'emergency' => [
            'path' => storage_path('logs/laravel.log'),
        ],
    ],

];

我的“.env”:

APP_NAME=InfoAlq
APP_ENV=local
APP_KEY=base64:N+QlCYsU2lDVNWiJ+KEP+QraqWsDbar7eLWwtJa+M3s=
APP_DEBUG=true
APP_URL=http://www.infoalq.local

LOG_CHANNEL=stack
LOG_DEPRECATIONS_CHANNEL=null
LOG_LEVEL=debug

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=jetstream3
DB_USERNAME=root
DB_PASSWORD=root

BROADCAST_DRIVER=log
CACHE_DRIVER=file
FILESYSTEM_DISK=public
QUEUE_CONNECTION=sync
SESSION_DRIVER=database
SESSION_LIFETIME=120

MEMCACHED_HOST=127.0.0.1

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_MAILER=smtp
MAIL_HOST=sandbox.smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=0a3a3bb4fcb540
MAIL_PASSWORD=7a9e2495cc9668
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS="[email protected]"
MAIL_FROM_NAME="Leandro Caplan"

AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=
AWS_USE_PATH_STYLE_ENDPOINT=false

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_HOST=
PUSHER_PORT=443
PUSHER_SCHEME=https
PUSHER_APP_CLUSTER=mt1

VITE_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
VITE_PUSHER_HOST="${PUSHER_HOST}"
VITE_PUSHER_PORT="${PUSHER_PORT}"
VITE_PUSHER_SCHEME="${PUSHER_SCHEME}"
VITE_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

相关内容