Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How not to show last bracket in a monolog log line?

Tags:

// in my PHP code $log = new Logger('LaurentCommand'); $log->pushHandler(new StreamHandler('./app/logs/LaurentCommand.log')); $log->addInfo("Start command",array('username' => 'Joe', 'Age' => '28')); 

Result in log file LaurentCommand.log :

[2012-12-20 10:28:11] LaurentCommand.INFO: Start command {"username":"Joe","Age":"28"} []

Why this bracket at the end ?

like image 541
stloc Avatar asked Dec 20 '12 09:12

stloc


People also ask

What are the different levels of logs we can have in monolog?

Monolog log levels DEBUG - detailed debug information. INFO - interesting events. NOTICE - normal but significant events. WARNING - exceptional occurrences that are not errors.

How Monolog works?

It gives you the right amount of data that you need to help you monitor your application, logs, databases, code, and web services. Monolog is the existing standard logging library for PHP. It is most popular in PHP frameworks such as Laravel and Symfony, where it implements a common interface for logging libraries.


2 Answers

That's the extra data. The default format of the LineFormatter is "[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n". the username/age is the context, and extra that is typically empty results in this empty array [].

If you use processors to attach data to log records they typically write it to the extra key to avoid conflicts with context info. If it really is an issue for you you can change the default format and omit %extra%.

Edit: As of Monolog 1.11 the LineFormatter has a $ignoreEmptyContextAndExtra parameter in the constructor that lets you remove these, so you can use this:

// the last "true" here tells it to remove empty []'s $formatter = new LineFormatter(null, null, false, true); $handler->setFormatter($formatter); 
like image 168
Seldaek Avatar answered Oct 20 '22 11:10

Seldaek


Old question, but throwing out another simple option:

$slackHandler = new \Monolog\Handler\SlackWebhookHandler(...); $slackHandler->getFormatter()->ignoreEmptyContextAndExtra(true); 
like image 42
Chris Avatar answered Oct 20 '22 11:10

Chris