Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load config based on APP_ENV #948

Open
gam6itko opened this issue Jun 21, 2023 · 2 comments
Open

Load config based on APP_ENV #948

gam6itko opened this issue Jun 21, 2023 · 2 comments

Comments

@gam6itko
Copy link
Contributor

gam6itko commented Jun 21, 2023

Description

It would be very useful to be able to load configuration files based on APP_ENV from corresponding folder. Like in a Symfony

Example

Imagine that we have such a file structure

app/
├─ config/
│  ├─ prod/
│  │  ├─ queue.php
│  │  ├─ cache.php
│  ├─ dev/
│  │  ├─ database.php
│  ├─ monolog.php
│  ├─ database.php
│  ├─ cache.php

For APP_ENV=prod configuration loader must first search configs in app/config/prod. If config does not exists then it should take it from default folder app/config.

In this case, files will be loaded from folders:

APP_ENV=prod

  • app/config/prod/queue.php
  • app/config/prod/cache.php
  • app/config/monolog.php
  • app/config/database.php

APP_ENV=dev

  • app/config/dev/database.php
  • app/config/cache.php
  • app/config/monolog.php

APP_ENV=*

  • app/config/database.php
  • app/config/cache.php
  • app/config/monolog.php
@butschster
Copy link
Member

It can be done via separate pacakge

@butschster butschster removed this from the 3.9 milestone Sep 9, 2023
@gam6itko
Copy link
Contributor Author

gam6itko commented Aug 29, 2024

Workaround.

In prod will be logged only ERROR level to `errors.log file.

// config/prod/monolog.php

return [
    'handlers' => [
        'default' => [
            'class' => 'log.rotate',
            'options' => [
                'filename' => directory('runtime').'logs/errors.log',
                'level' => Logger::toMonologLevel('ERROR'),
                'maxFiles' => 10,
            ],
        ]
    ],
];
// config/monolog.php

$envConfig = (function(): array {
    $path = \sprintf('%s%s/%s', directory('config'), env('APP_ENV', 'local'), basename(__FILE__));
    return \file_exists($path)
        ? include $path
        : [];
})();

return $envConfig + [
    'handlers' => [
        'default' => [
            'class' => 'log.rotate',
            'options' => [
                'filename' => directory('runtime').'logs/app.log',
                'level' => Logger::toMonologLevel('DEBUG'),
                'maxFiles' => 2,
                'bubble' => true,
            ],
        ]
    ],
];

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Todo
Development

No branches or pull requests

2 participants