0

I want to use Monolog to log some info.

This is my code:

// Create the logger
$logger = new Monolog\Logger('my_logger');

$path = storage_path('app/logs/mc.log');
$logger->pushHandler(new Monolog\Handler\StreamHandler($path, Monolog\Logger::INFO));

// You can now use your logger
$logger->info('My logger is now ready');

This is how my log file looks after I executed the code a couple of times:

[2018-02-13 15:06:12] my_logger.INFO: My logger is now ready [] []

[2018-02-13 15:06:29] my_logger.INFO: My logger is now ready [] []

I would like to have a more clean log file like this:

[2018-02-13 15:06:12] My logger is now ready

[2018-02-13 15:06:29] My logger is now ready

Is this possible? Or can one at least remove the two empty brackets at the end?

Community
  • 1
  • 1
Adam
  • 25,960
  • 22
  • 158
  • 247

1 Answers1

2

You can customize the log format. From the docs:

// the default date format is "Y-m-d H:i:s"
$dateFormat = "Y n j, g:i a";
// the default output format is "[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n"
$output = "%datetime% > %level_name% > %message% %context% %extra%\n";
// finally, create a formatter
$formatter = new LineFormatter($output, $dateFormat);

// Create a handler
$stream = new StreamHandler(__DIR__.'/my_app.log', Logger::DEBUG);
$stream->setFormatter($formatter);
// bind it to a logger object
$securityLogger = new Logger('security');
$securityLogger->pushHandler($stream);

You'd be removing the %channel%.%level_name%, %context%, and %extra% components of the standard format.

That said, severity can be pretty important - you want to know if something is a DEBUG log item versus a CRITICAL one. Same thing with the [] bits - they may contain, at times, important information for further analysis.

These logs aren't typically intended for non-technical users to review, so I question the value of suppressing useful information for the sake of mild-at-best readability adjustments.

ceejayoz
  • 176,543
  • 40
  • 303
  • 368