Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Json formatter #462

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
strategy:
fail-fast: true
matrix:
php: [8.2, 8.3]
php: [8.2,8.3]
dependency-version: [prefer-lowest, prefer-stable]

name: PHP ${{ matrix.php }} - ${{ matrix.dependency-version }}
Expand Down
18 changes: 9 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@
"email": "[email protected]",
"homepage": "https://github.com/arcanedev-maroc",
"role": "Developer"
}
],
}
],
"type": "library",
"license": "MIT",
"require": {
"php": "^8.2",
"require": {
"php": ">=7.3",
"ext-json": "*",
"arcanedev/support": "^11.0",
"arcanedev/support": ">=8.0",
"psr/log": "^1.0|^2.0|^3.0"
},
"require-dev": {
"laravel/framework": "^11.0",
"mockery/mockery": "^1.6",
"orchestra/testbench-core": "^9.0",
"phpunit/phpunit": "^10.5"
"laravel/framework": ">=8.0",
"mockery/mockery": ">=1.4.2",
"orchestra/testbench-core": ">=6.27",
"phpunit/phpunit": ">=9.5.10"
},
"autoload": {
"psr-4": {
Expand Down
9 changes: 9 additions & 0 deletions config/log-viewer.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@

'per-page' => 30,

/* -----------------------------------------------------------------
| Type of log formatter used
| -----------------------------------------------------------------
| Supported formatters :
| 'LineFormatter', 'JsonFormatter'
*/

'formatter' => 'default',

/* -----------------------------------------------------------------
| Download settings
| -----------------------------------------------------------------
Expand Down
22 changes: 22 additions & 0 deletions src/Contracts/LogEntryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Arcanedev\LogViewer\Contracts;

/**
* Interface LogEntryInterface
*
* @author iyogesharma <[email protected]>
*/
interface LogEntryInterface {

/**
* Check if same log level.
*
* @param string $level
*
* @return bool
*/
public function isSameLevel($level);
}
291 changes: 291 additions & 0 deletions src/Entities/JsonLogEntry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,291 @@
<?php

declare(strict_types=1);

namespace Arcanedev\LogViewer\Entities;

use Arcanedev\LogViewer\Contracts\LogEntryInterface;
use Carbon\Carbon;
use Illuminate\Contracts\Support\{Arrayable, Jsonable};
use JsonSerializable;

/**
* Class LogEntry
*
* @author iyogesharma <[email protected]>
*/
class JsonLogEntry implements Arrayable, Jsonable, JsonSerializable, LogEntryInterface
{
/* -----------------------------------------------------------------
| Properties
| -----------------------------------------------------------------
*/

/** @var string */
public $env;

/** @var string */
public $level;

/** @var \Carbon\Carbon */
public $datetime;

/** @var string */
public $header;

/** @var string */
public $stack;

/** @var array */
public $context = [];

/* -----------------------------------------------------------------
| Constructor
| -----------------------------------------------------------------
*/

/**
* Construct the log entry instance.
*
* @param string $level
* @param string $header
* @param string|null $stack
*/
public function __construct($data)
{
$this->setLevel($data);
$this->setHeader($data);
$this->setStack($data);
}

/* -----------------------------------------------------------------
| Getters & Setters
| -----------------------------------------------------------------
*/

/**
* Set the entry level.
*
* @param string $level
*
* @return self
*/
private function setLevel($data)
{
$this->level = strtolower($data['level_name']);

return $this;
}

/**
* Set the entry header.
*
* @param string $header
*
* @return self
*/
private function setHeader($data)
{
$this->setDatetime($data['datetime']);

$header = $data['message'];

$this->header = trim($header);

$this->setContext($data['context']);

$this->setEnv($data['channel']);

return $this;
}

/**
* Set the context.
*
* @param array $context
*
* @return $this
*/
private function setContext(array $context)
{
$this->context = $context;

return $this;
}

/**
* Set entry environment.
*
* @param string $env
*
* @return self
*/
private function setEnv($env)
{
$this->env = head(explode('.', $env));

return $this;
}

/**
* Set the entry date time.
*
* @param string $datetime
*
* @return \Arcanedev\LogViewer\Entities\LogEntry
*/
private function setDatetime($datetime)
{
$this->datetime = Carbon::createFromFormat('Y-m-d H:i:s', date('Y-m-d H:i:s',strtotime($datetime)));

return $this;
}

/**
* Set the entry stack.
*
* @param string $stack
*
* @return self
*/
private function setStack($data)
{
$this->stack = isset($data['stack']) ? json_encode($data['stack']): "";

return $this;
}

/**
* Get translated level name with icon.
*
* @return string
*/
public function level()
{
return $this->icon()->toHtml().' '.$this->name();
}

/**
* Get translated level name.
*
* @return string
*/
public function name()
{
return log_levels()->get($this->level);
}

/**
* Get level icon.
*
* @return \Illuminate\Support\HtmlString
*/
public function icon()
{
return log_styler()->icon($this->level);
}

/**
* Get the entry stack.
*
* @return string
*/
public function stack()
{
return trim(htmlentities($this->stack));
}

/**
* Get the entry context as json pretty print.
*
* @return string
*/
public function context()
{
return json_encode($this->context, JSON_PRETTY_PRINT);
}

/* -----------------------------------------------------------------
| Check Methods
| -----------------------------------------------------------------
*/

/**
* Check if same log level.
*
* @param string $level
*
* @return bool
*/
public function isSameLevel($level)
{
return $this->level === $level;
}

/* -----------------------------------------------------------------
| Convert Methods
| -----------------------------------------------------------------
*/

/**
* Get the log entry as an array.
*
* @return array
*/
public function toArray()
{
return [
'level' => $this->level,
'datetime' => $this->datetime->format('Y-m-d H:i:s'),
'header' => $this->header,
'stack' => $this->stack
];
}

/**
* Convert the log entry to its JSON representation.
*
* @param int $options
*
* @return string
*/
public function toJson($options = 0)
{
return json_encode($this->toArray(), $options);
}

/**
* Serialize the log entry object to json data.
*
* @return array
*/
public function jsonSerialize(): array
{
return $this->toArray();
}

/* -----------------------------------------------------------------
| Check Methods
| -----------------------------------------------------------------
*/

/**
* Check if the entry has a stack.
*
* @return bool
*/
public function hasStack()
{
return $this->stack !== "\n";
}

/**
* Check if the entry has a context.
*
* @return bool
*/
public function hasContext()
{
return ! empty($this->context);
}
}
3 changes: 2 additions & 1 deletion src/Entities/LogEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Arcanedev\LogViewer\Entities;

use Arcanedev\LogViewer\Contracts\LogEntryInterface;
use Arcanedev\LogViewer\Helpers\LogParser;
use Carbon\Carbon;
use Illuminate\Contracts\Support\{Arrayable, Jsonable};
Expand All @@ -14,7 +15,7 @@
*
* @author ARCANEDEV <[email protected]>
*/
class LogEntry implements Arrayable, Jsonable, JsonSerializable
class LogEntry implements Arrayable, Jsonable, JsonSerializable,LogEntryInterface
{
/* -----------------------------------------------------------------
| Properties
Expand Down
Loading