0

I'm using Monolog to create my app's logging system. In the core app file, after I create a new Monolog object, I need to select the log level that I want to print in the log file. I want to use a global constant LOG_LEVEL which could be 'DEBUG', 'INFO', etc. I need the Monolog class to treat its value as a class constant.

// content of config.php
// Here I declare the constants in a separate file called 'config.php'
define("LOG_FILE", "patch/to/my/log.log");
define("LOG_LEVEL", "ERROR");

// content of app.php
require 'config.php';
require 'vendor/autoload.php';

$container['logger'] = function($c) {
    $logger = new \Monolog\Logger('logger');
    error_log('log level ' . LOG_LEVEL); // prints 'log level ERROR'

    $fileHandler = new \Monolog\Handler\StreamHandler(LOG_FILE, $logger::LOG_LEVEL); // here I get the error 'Undefined class constant LOG_LEVEL'
    //the normal syntax would be '$logger::ERROR' in this case and that works fine

    $logger->pushHandler($fileHandler);
    return $logger;
};

I need the 'LOG_LEVEL' constant to be used as 'ERROR' by the monolog class, not as 'LOG_LEVEL'. What am I doing wrong here, been searching an answer for hours now without any luck.

miken32
  • 42,008
  • 16
  • 111
  • 154
Godryc
  • 3
  • 1

4 Answers4

1

You are now doing $logger::LOG_LEVEL, which is taking the 'LOG_LEVEL' out of the class whichever $logger is (in this case a \Monolog\Logger). That doesn't have a static variable named LOG_LEVEL, thus you get the undefined.
You have just have 'LOG_LEVEL' defined, out of any class, so:

 $fileHandler = new \Monolog\Handler\StreamHandler(LOG_FILE, LOG_LEVEL); 

Fancy solution:

You could do a static class and include that in your main page:

Class CONFIG {
    public static $LOG_LEVEL = 'default Value';
}

// Then you can use this anywhere:
CONFIG::$LOG_LEVEL
$fileHandler = new \Monolog\Handler\StreamHandler(LOG_FILE, CONFIG::$LOG_LEVEL); 

The advantage of this is having only one file for configs, not scattered across all kinds of files, which'll become very annoying very fast.

Martijn
  • 15,791
  • 4
  • 36
  • 68
  • But they don't want to pass the string value "ERROR". If `LOG_FILE` is set to "ERROR" they want to pass `$logger::ERROR`. Basically they want variable variables, but with constants. (I think.) – miken32 May 17 '19 at 16:12
  • Now that I see it I feel so stupid. Don't know where my head was, didn't code for some time but it seams I have a huge blank in my head. I need to be more careful in the future. Your solution is perfect, even the solutions from the colleagues bellow are good. Thank you sir. – Godryc May 20 '19 at 06:35
0

You can also use Logger::getLevels() like the following:

$log_level = $logger->getLevels()[LOG_LEVEL];
$fileHandler = new ...StreamHandler(LOG_FILE, $log_level);
John
  • 577
  • 4
  • 20
0

Make a static class and include that...

class GLOBALCONF{
    public static $VALUE= 'Something in here';
}

// Use it where you want
GLOBALCONF::$VALUE
DigitalJedi
  • 1,577
  • 1
  • 10
  • 29
0

You're making this more complicated than it needs to be. Monolog has a function to convert an error level as as string to its own internal value. Just change your code to this:

$fileHandler = new \Monolog\Handler\StreamHandler(LOG_FILE, $logger::toMonologLevel(LOG_LEVEL));
miken32
  • 42,008
  • 16
  • 111
  • 154