Welcome Guest, Not a member yet? Register   Sign In
Using numerically indexed arrays in .env file
#1

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. 
On the other hand, the array lets you indicate precisely what you want, like 

PHP Code:
public $threshold = [1238]; 
Thing is, 8 is the 'debug' level, and for a development environment, it makes sense to have an array like that, whilst
PHP Code:
$threshold 8
includes too much stuff (all warnings and notices, for example).

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
logger
.threshold.1 2
logger
.threshold.2 3
logger
.threshold.3 
I've seen suggested to use a string like 

PHP Code:
logger.threshold '[1, 2, 3, 8]' 
and then parse it, but this being a class that comes with Codeigniter, I'd rather find a different way to do this if possible.
Reply
#2

See https://codeigniter.com/user_guide/gener...s-for-data
Reply
#3

(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 = [1234]; 
.env contains

PHP Code:
logger.threshold.0 1
logger
.threshold.1 2
logger
.threshold.2 3
logger
.threshold.3 4
logger
.threshold.4 


in a controller's method, I added

PHP Code:
log_message('error''test error');
log_message('debug''test debug'); 


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 
Reply
#4

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.
Reply
#5

PHP Code:
public $threshold = [1234]; 

PHP Code:
logger.threshold.0 1  // You can set the value because the element exists
logger.threshold.1 2  // You can set the value because the element exists
logger.threshold.2 3  // You can set the value because the element exists
logger.threshold.3 4  // You can set the value because the element exists
logger.threshold.4 8  // You cannot add the element 
Reply
#6

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!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB