Remove color markers when logging to files (#13541)

* Remove color markers when logging to files

* Update lint

* safety checks

* back to simple formatters, but two of them

* more correct comment
This commit is contained in:
Tony Murray
2021-11-24 16:33:02 -06:00
committed by GitHub
parent 21edc086ca
commit b94f08cfdb
4 changed files with 68 additions and 23 deletions

View File

@@ -19,39 +19,41 @@
*
* @link https://www.librenms.org
*
* @copyright 2018 Tony Murray
* @copyright 2021 Tony Murray
* @author Tony Murray <murraytony@gmail.com>
*/
namespace LibreNMS\Util;
namespace App\Logging;
class CliColorFormatter extends \Monolog\Formatter\LineFormatter
{
/**
* @var \Console_Color2
*/
private $console_color;
/**
* @var bool
*/
private $console;
public function __construct()
{
$this->console_color = new \Console_Color2();
$this->console = \App::runningInConsole();
parent::__construct(
"%message% %context% %extra%\n",
null,
true,
true
);
$this->console_color = new \Console_Color2();
$this->console = \App::runningInConsole();
}
public function format(array $record): string
{
// only format messages where color is enabled
if (isset($record['context']['color']) && $record['context']['color']) {
if ($this->console) {
$record['message'] = $this->console_color->convert($record['message']);
} else {
$record['message'] = $this->console_color->strip($record['message']);
}
$record['message'] = $this->console_color->convert($record['message'], $this->console);
unset($record['context']['color']);
}

View File

@@ -0,0 +1,51 @@
<?php
/**
* FileColorFormatter.php
*
* Always strip colors from the output.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* @link https://www.librenms.org
*
* @copyright 2021 Tony Murray
* @author Tony Murray <murraytony@gmail.com>
*/
namespace App\Logging;
class NoColorFormatter extends \Monolog\Formatter\LineFormatter
{
/**
* @var \Console_Color2
*/
private $console_color;
public function __construct()
{
parent::__construct(null, null, true, true);
$this->console_color = new \Console_Color2();
}
public function format(array $record): string
{
// only strip messages where color is enabled
if (isset($record['context']['color']) && $record['context']['color']) {
$record['message'] = $this->console_color->convert($record['message'], false);
unset($record['context']['color']);
}
return parent::format($record);
}
}

View File

@@ -64,12 +64,14 @@ return [
'single' => [
'driver' => 'single',
'path' => env('APP_LOG', \LibreNMS\Config::get('log_file', base_path('logs/librenms.log'))),
'formatter' => \App\Logging\NoColorFormatter::class,
'level' => env('LOG_LEVEL', 'error'),
],
'daily' => [
'driver' => 'daily',
'path' => env('APP_LOG', \LibreNMS\Config::get('log_file', base_path('logs/librenms.log'))),
'formatter' => \App\Logging\NoColorFormatter::class,
'level' => env('LOG_LEVEL', 'error'),
'days' => 14,
],
@@ -95,7 +97,7 @@ return [
'stderr' => [
'driver' => 'monolog',
'handler' => StreamHandler::class,
'formatter' => \LibreNMS\Util\CliColorFormatter::class,
'formatter' => \App\Logging\CliColorFormatter::class,
'with' => [
'stream' => 'php://stderr',
],
@@ -105,7 +107,7 @@ return [
'stdout_debug' => [
'driver' => 'monolog',
'handler' => StreamHandler::class,
'formatter' => \LibreNMS\Util\CliColorFormatter::class,
'formatter' => \App\Logging\CliColorFormatter::class,
'with' => [
'stream' => 'php://output',
],
@@ -115,7 +117,7 @@ return [
'stdout' => [
'driver' => 'monolog',
'handler' => StreamHandler::class,
'formatter' => \LibreNMS\Util\CliColorFormatter::class,
'formatter' => \App\Logging\CliColorFormatter::class,
'with' => [
'stream' => 'php://output',
],

View File

@@ -5380,16 +5380,6 @@ parameters:
count: 1
path: LibreNMS/Util/CiHelper.php
-
message: "#^Property LibreNMS\\\\Util\\\\CliColorFormatter\\:\\:\\$console has no type specified\\.$#"
count: 1
path: LibreNMS/Util/CliColorFormatter.php
-
message: "#^Property LibreNMS\\\\Util\\\\CliColorFormatter\\:\\:\\$console_color has no type specified\\.$#"
count: 1
path: LibreNMS/Util/CliColorFormatter.php
-
message: "#^Method LibreNMS\\\\Util\\\\Colors\\:\\:percentage\\(\\) has no return type specified\\.$#"
count: 1