0

I am trying to send an array via a function, but I only get Uncaught TypeError: Psr\Log\AbstractLogger::error(): Argument #2 ($context) must be of type array, string given, called in.... I'm using KLogger so I can log important stuff on my website and sometimes, putting stuff in an array would be quite useful sometimes.

Here's how everything looks like in my code:

function logger($string, Array $array, $type = 'info') {
    global $filename, $filename_get;    # these variables contains the filename and GET that the visitor are currently at

    $dir = '/destination/somewhere/on-the-server';
    if(!file_exists($dir)) {
        create_folder($dir);    # a function that creates a new folder if it doesn't exists
    }


    $logger = new Katzgrau\KLogger\Logger($dir);

    if($type == 'emergency') {
        $logger->emergency($string.' - '.$filename . ($filename_get == '-' ? null : '?'.$filename_get), (empty($array) ? $array : ''));
    } elseif($type == 'alert') {
        $logger->alert($string.' - '.$filename . ($filename_get == '-' ? null : '?'.$filename_get), (empty($array) ? $array : ''));
    } elseif($type == 'critical') {
        $logger->critical($string.' - '.$filename . ($filename_get == '-' ? null : '?'.$filename_get), (empty($array) ? $array : ''));
    } elseif($type == 'error') {
        $logger->error($string.' - '.$filename . ($filename_get == '-' ? null : '?'.$filename_get), (empty($array) ? $array : ''));
    } elseif($type == 'warning') {
        $logger->warning($string.' - '.$filename . ($filename_get == '-' ? null : '?'.$filename_get), (empty($array) ? $array : ''));
    } elseif($type == 'notice') {
        $logger->notice($string.' - '.$filename . ($filename_get == '-' ? null : '?'.$filename_get), (empty($array) ? $array : ''));
    } elseif($type == 'info') {
        $logger->info($string.' - '.$filename . ($filename_get == '-' ? null : '?'.$filename_get), (empty($array) ? $array : ''));
    } elseif($type == 'debug') {
        $logger->debug($string.' '.$filename . ($filename_get == '-' ? null : '?'.$filename_get), (empty($array) ? $array : ''));
    }
}

logger('regular string', [
    'test' => 'hi'
], 'error');

I've tried "everything"! Array $array, &$array, and $array = []. What have I missed? How can I solve this?

8
  • shouldn't it be !empty($array) ? $array : '' ? Commented Oct 5, 2021 at 10:56
  • You mean on line 1 in my code in my question? I tried, but I got this error: Parse error: syntax error, unexpected token "!", expecting variable in... Commented Oct 5, 2021 at 10:58
  • Please extract a minimal reproducible example before asking a question here. Your question is off-topic without it. Commented Oct 5, 2021 at 11:06
  • I can't make a minimal reproducible example here because KLogger requires composer. If I could make the reproducible example, I would. Commented Oct 5, 2021 at 11:11
  • Note that almost all of the code in the branches is duplicated, which makes errors more likely. For one, the log message in the 'debug' branch is slightly different (' ' as a separator, rather than ' - '). Assuming this is a mistake, there are two general techniques that can remove the repeated code: 1. calculate the arguments before the block and pass as variables 2. use a variable method name instead of the branches ($logger->$type($string ...);). Commented Oct 5, 2021 at 11:11

1 Answer 1

1

You have error in your code. Your second parameter is empty($array) ? $array : ''.

If $array is empty you send the $array, but if it's not empty you send an empty string.

It probably should be like this empty($array) ? [] : $array.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you :) I totally missed that one and only focused on line 1 of the code in my question.
We've all been there, more times than we want to admit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.