I have tried with both inbuilt channels and custom ones as well whenever I am logging something in the terminal/stdout WARN keyword is added before the log.
but the same logs are coming as they are supposed to be in the log file
Laravel log in file
in my use case I need the logs in json format in the terminal but because of the keyword it is coming as string.
This is logging method(I have tried the same with inbuilt channels like single … but same)
'methodlog' => [
'driver' => 'custom',
'via' => AppLoggingMethodLogChannel::class,
'formatter' => AppLoggingCustomJsonFormatter::class,
'name' => 'methodlog',
'with' => [
'path' => storage_path('logs/laravel.log'),//'php://stdout',
'level' => env('LOG_LEVEL', 'debug'),
],
],
Edit:
CustomJsonFormatter.php
class CustomJsonFormatter extends JsonFormatter
{
private $DEFAULT_JSON_FLAGS = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRESERVE_ZERO_FRACTION | JSON_INVALID_UTF8_SUBSTITUTE | JSON_PARTIAL_OUTPUT_ON_ERROR ;
public function format(LogRecord $record): string
{
$formatted = $record['context'];
if ($this->includeStacktraces) {
$formatted['stack_trace'] = $record['formatted']['exception'];
}
return $this->toJson($formatted, true) . ($this->appendNewline ? "n" : '');
}
}
MethodLogChannel.php
class MethodLogChannel
{
public function __invoke(array $config)
{
$log = new Logger($config['name']);
$handler = new StreamHandler($config['with']['path'], $config['with']['level']);
$handler->setFormatter(new CustomJsonFormatter());
$log->pushHandler($handler);
$log->pushProcessor(new IntrospectionProcessor());
$log->pushProcessor(new WebProcessor($_SERVER));
return $log;
}
}
for saving logs
Log::channel(‘methodlog’)->info(”, $log_message);