![]() |
Is there a way to have a numerically-indexed array in a .env file? My use-case is the configuration app/Config/Logger.php: you can setup the $threshold variable as a number or, since relatively recently, an array. The number indicates to log requests of that level or less, e.g.
PHP Code: public $threshold = 3; // log requests of levels 1, 2 and 3. PHP Code: public $threshold = [1, 2, 3, 8]; PHP Code: $threshold = 8; It would be ideal if I could set this somehow on my .env file, so as not to have to modify the app/Config/Logger.php file, which is committed to git, every time I want to debug stuff. This way, my setup shows debug messages, but others like testing or production don't. I've tried putting this into my .env, but it does not work, PHP Code: logger.threshold.0 = 1 PHP Code: logger.threshold = '[1, 2, 3, 8]'
02-03-2024, 06:07 AM
(This post was last modified: 02-03-2024, 06:43 AM by adrianovaroli. Edit Reason: Added code example ) (02-02-2024, 06:57 PM)kenjis Wrote: See https://codeigniter.com/user_guide/gener...s-for-data Yes, I don't see anything in there that relates to my question. If you meant Quote:"You cannot add a property that is not defined in the Config class, nor can you change it to an array if the value of the defined property is a scalar." the property in the original *is* an array. I just want to define a different array in my .env file. An example of what I'm trying to do: app/Config/Logger.php contains PHP Code: public $threshold = [1, 2, 3, 4]; PHP Code: logger.threshold.0 = 1 in a controller's method, I added PHP Code: log_message('error', 'test error'); and when that method is called, writable/logs/log-2024-02-02.log only shows PHP Code: ERROR - 2024-02-02 18:21:31 --> test error
Okay, thank you. You are correct. The description is not complete.
In fact, 1. You cannot add a property that is not defined in the Config class. 2. You cannot change a scalar value in a property to an array. 3. You cannot add an element to an existing array. PHP Code: public $threshold = [1, 2, 3, 4]; PHP Code: logger.threshold.0 = 1 // You can set the value because the element exists
Ok, this is useful to know. I could set different arrays of the same length in different environments. I don't like it, it's dirty, but I could. Thanks!
HI , i set in my .env file :
logger.threshold = [1,2,3,4,7] but it shows WARNINGS message that maybe 5, why?
dotenv files, traditionally, do not support arrays. The right hand side of the assignment statement is treated as a string (assuming it is syntactically correct). You would be better of using Registrars.
(03-16-2025, 01:57 PM)grimpirate Wrote: dotenv files, traditionally, do not support arrays. The right hand side of the assignment statement is treated as a string (assuming it is syntactically correct). You would be better of using Registrars. In env must i set logger.threshold= '1,2,3,4,7' instead as array ?
You have to do what kenji showed in his last reply. Which is populate the $threshold array with 5 numbers and then you can refer to each index in the .env file
|
Welcome Guest, Not a member yet? Register Sign In |