Skip to content

Commit d30c04e

Browse files
committed
Uploading files
0 parents  commit d30c04e

13 files changed

+541
-0
lines changed

.gitattributes

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto

.gitignore

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/vendor
2+
composer.phar
3+
composer.lock
4+
.DS_Store
5+
Thumbs.db
6+
/phpunit.xml
7+
/.idea
8+
/.fleet
9+
/.vscode
10+
.phpunit.result.cache

README.md

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# PHP LARAVEL LOGGER
2+
This library provides the use of laravel logger along with the output of information to the console.
3+
4+
***This package can be useful when running laravel commands***
5+
6+
## Installation
7+
```sh
8+
composer require qu1eeeoj/laravel-logger
9+
```
10+
11+
## Example
12+
```php
13+
<?php
14+
15+
namespace App\Console\Commands;
16+
17+
use App\Models\User;
18+
use Illuminate\Console\Command;
19+
use Qu1eeeOJ\LaravelLogger\LoggerService;
20+
21+
class CreateUserCommand extends Command
22+
{
23+
/**
24+
* The name and signature of the console command.
25+
*
26+
* @var string
27+
*/
28+
protected $signature = 'user:create {email}';
29+
30+
/**
31+
* The console command description.
32+
*
33+
* @var string
34+
*/
35+
protected $description = 'Create user with admin privileges';
36+
37+
/**
38+
* CreateUserCommand constructor
39+
*
40+
* @param LoggerService $logger - Logger with output console
41+
*/
42+
public function __construct(private readonly LoggerService $logger = new LoggerService('daily'))
43+
{
44+
// Call parent constructor
45+
parent::__construct();
46+
}
47+
48+
/**
49+
* Execute the console command.
50+
*
51+
* @return int
52+
*/
53+
public function handle(): int
54+
{
55+
// Create user
56+
$user = User::query()->create([]);
57+
$this->logger->info('User created successfully');
58+
59+
// Adding admin privileges
60+
$user->permissions()->setRole('admin');
61+
$this->logger->info('Now user has admin privileges');
62+
63+
return Command::SUCCESS;
64+
}
65+
}
66+
```
67+
68+
## Methods - LoggerService
69+
- alert(string $message): void
70+
- critical(string $message): void
71+
- debug(string $message): void - works with the console, but in working mode it does not write information to the log, but only the console!
72+
- emergency(string $message): void
73+
- error(string $message): void - works with console
74+
- info(string $message): void - works with console
75+
- notice(string $message): void
76+
- warning(string $message): void - works with console
77+
- withConsoleLogger(): bool - Determine whether the console logger is used

composer.json

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "qu1eeeoj/laravel-logger",
3+
"description": "Logger with console logger",
4+
"keywords": [
5+
"Laravel logger",
6+
"Laravel log",
7+
"Laravel logger with console output"
8+
],
9+
"type": "library",
10+
"autoload": {
11+
"psr-4": {
12+
"Qu1eeeOJ\\LaravelLogger\\": "src/"
13+
}
14+
},
15+
"authors": [
16+
{
17+
"name": "qu1eeeoj"
18+
}
19+
],
20+
"minimum-stability": "dev",
21+
"require": {
22+
"php": "^8.1"
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Qu1eeeOJ\LaravelLogger\Exceptions;
4+
5+
class MethodNotFoundException extends \Exception
6+
{
7+
//
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Qu1eeeOJ\LaravelLogger\Exceptions;
4+
5+
class StyleNotFoundException extends \Exception
6+
{
7+
//
8+
}
+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace Qu1eeeOJ\LaravelLogger\Formatters;
4+
5+
use Illuminate\Support\Carbon;
6+
use Qu1eeeOJ\LaravelLogger\Interfaces\FormatterWithStyleInterface;
7+
use Qu1eeeOJ\LaravelLogger\Traits\MessageWithPrefix;
8+
9+
class ConsoleMessageFormatter implements FormatterWithStyleInterface
10+
{
11+
use MessageWithPrefix;
12+
13+
/**
14+
* @var string
15+
*/
16+
private string $style = '';
17+
18+
/**
19+
* Set style
20+
*
21+
* @param string $style
22+
*
23+
* @return static
24+
*/
25+
public function setStyle(string $style): static
26+
{
27+
$this->style = $style;
28+
29+
return $this;
30+
}
31+
32+
/**
33+
* Format the message text
34+
*
35+
* @param string $message
36+
*
37+
* @return string
38+
*/
39+
public function getFormattedMessage(string $message): string
40+
{
41+
return sprintf(
42+
'<%s>[%s] %s: %s: %s</%s>',
43+
$this->style,
44+
Carbon::now()->toDateTimeLocalString(),
45+
mb_strtoupper($this->style),
46+
$this->getPrefix(),
47+
$message,
48+
$this->style
49+
);
50+
}
51+
}
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Qu1eeeOJ\LaravelLogger\Formatters;
4+
5+
use Qu1eeeOJ\LaravelLogger\Interfaces\FormatterInterface;
6+
use Qu1eeeOJ\LaravelLogger\Traits\MessageWithPrefix;
7+
8+
class LogMessageFormatter implements FormatterInterface
9+
{
10+
use MessageWithPrefix;
11+
12+
/**
13+
* Format the message text
14+
*
15+
* @param string $message
16+
*
17+
* @return string
18+
*/
19+
public function getFormattedMessage(string $message): string
20+
{
21+
return empty($this->getPrefix())
22+
? $message
23+
: sprintf('%s: %s', $this->getPrefix(), $message);
24+
}
25+
}

src/Interfaces/FormatterInterface.php

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Qu1eeeOJ\LaravelLogger\Interfaces;
4+
5+
interface FormatterInterface
6+
{
7+
// /**
8+
// * Set prefix to message
9+
// *
10+
// * @param string $prefix
11+
// *
12+
// * @return static
13+
// */
14+
// public function setPrefix(string $prefix): static;
15+
16+
/**
17+
* Format the message text
18+
*
19+
* @param string $message
20+
*
21+
* @return string
22+
*/
23+
public function getFormattedMessage(string $message): string;
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Qu1eeeOJ\LaravelLogger\Interfaces;
4+
5+
interface FormatterWithStyleInterface extends FormatterInterface
6+
{
7+
/**
8+
* Set style
9+
*
10+
* @param string $style
11+
*
12+
* @return static
13+
*/
14+
public function setStyle(string $style): static;
15+
}

src/LoggerConsoleService.php

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
3+
namespace Qu1eeeOJ\LaravelLogger;
4+
5+
use Illuminate\Support\Facades\App;
6+
use Qu1eeeOJ\LaravelLogger\Exceptions\StyleNotFoundException;
7+
use Qu1eeeOJ\LaravelLogger\Formatters\ConsoleMessageFormatter;
8+
use Qu1eeeOJ\LaravelLogger\Interfaces\FormatterWithStyleInterface;
9+
use Symfony\Component\Console\Output\ConsoleOutput;
10+
11+
/**
12+
* @method void info(string $message)
13+
* @method void error(string $message)
14+
* @method void warning(string $message)
15+
* @method void debug(string $message)
16+
*/
17+
class LoggerConsoleService
18+
{
19+
/**
20+
* @var ConsoleOutput
21+
*/
22+
private readonly ConsoleOutput $console;
23+
24+
/**
25+
* We are casting the style of operation from laravel logger for console output
26+
*
27+
* @var array<string, string>
28+
*/
29+
private array $styles = [
30+
'info' => 'info',
31+
'error' => 'error',
32+
'warning' => 'warn',
33+
'debug' => 'info'
34+
];
35+
36+
/**
37+
* LoggerConsoleService constructor
38+
*
39+
* @param string|array|null $prefix
40+
* @param FormatterWithStyleInterface $formatter
41+
*/
42+
public function __construct(
43+
protected readonly string|array|null $prefix = null,
44+
public readonly FormatterWithStyleInterface $formatter = new ConsoleMessageFormatter()
45+
)
46+
{
47+
$this->console = App::make(ConsoleOutput::class);
48+
49+
// if the prefix is not null, then we put the prefix
50+
if (! is_null($this->prefix)) {
51+
$this->formatter->setPrefix($this->prefix);
52+
}
53+
}
54+
55+
/**
56+
* Magic method
57+
*
58+
* @param string $style
59+
* @param array $arguments
60+
*
61+
* @return void
62+
*
63+
* @throws StyleNotFoundException
64+
*/
65+
public function __call(string $style, array $arguments): void
66+
{
67+
$this->writeln($style, $arguments[0]);
68+
}
69+
70+
/**
71+
* Writing to console output with new line
72+
*
73+
* @param string $style
74+
* @param string $message
75+
*
76+
* @return void
77+
*
78+
* @throws StyleNotFoundException
79+
*/
80+
public function writeln(string $style, string $message): void
81+
{
82+
$this->write($style, $message, true);
83+
}
84+
85+
/**
86+
* Writing to console output
87+
*
88+
* @param string $style
89+
* @param string $message
90+
* @param bool $newLine
91+
*
92+
* @return void
93+
*
94+
* @throws StyleNotFoundException
95+
*/
96+
public function write(string $style, string $message, bool $newLine = false): void
97+
{
98+
// Check style for console output
99+
if (! array_key_exists($style, $this->styles)) {
100+
throw new StyleNotFoundException(sprintf("Style [%s] not found in console styles", $style));
101+
}
102+
103+
// Setting the style to the formatter
104+
$this->formatter->setStyle($this->styles[$style]);
105+
106+
$this->console->write($this->formatter->getFormattedMessage($message), $newLine);
107+
}
108+
}

0 commit comments

Comments
 (0)