Skip to content

Commit

Permalink
Merge pull request #50 from dotkernel/issue-49
Browse files Browse the repository at this point in the history
Removed laminas-log dependency
  • Loading branch information
alexmerlin authored Aug 29, 2024
2 parents 62dec30 + d943bb4 commit 4ccfaab
Show file tree
Hide file tree
Showing 52 changed files with 2,616 additions and 153 deletions.
58 changes: 20 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
# dot-log

> [!IMPORTANT]
> dot-log is a wrapper on top of [laminas/laminas-log](https://github.com/laminas/laminas-log)
>
> ![Dynamic JSON Badge](https://img.shields.io/badge/dynamic/json?url=https%3A%2F%2Fapi.github.com%2Frepos%2Flaminas%2Flaminas-log%2Fproperties%2Fvalues&query=%24%5B%3F(%40.property_name%3D%3D%22maintenance-mode%22)%5D.value&label=Maintenance%20Mode)
## dot-log badges

![OSS Lifecycle](https://img.shields.io/osslifecycle/dotkernel/dot-log)
![PHP from Packagist (specify version)](https://img.shields.io/packagist/php-v/dotkernel/dot-log/3.4.4)
![PHP from Packagist (specify version)](https://img.shields.io/packagist/php-v/dotkernel/dot-log/4.0.0)

[![GitHub issues](https://img.shields.io/github/issues/dotkernel/dot-log)](https://github.com/dotkernel/dot-log/issues)
[![GitHub forks](https://img.shields.io/github/forks/dotkernel/dot-log)](https://github.com/dotkernel/dot-log/network)
Expand All @@ -29,13 +24,13 @@
* Open the `Dot\Log\ConfigProvider`
* In the dependencies section you will see an abstract factory (`LoggerAbstractServiceFactory::class`)
* This class responds to "selectors" instead of class names
Instead of requesting the `Laminas\Log\Logger::class` from the container, `dot-log.my_logger` should be requested (or just `my_logger` if using `laminas-log`)
Instead of requesting the `Dot\Log\Logger::class` from the container, `dot-log.my_logger` should be requested

## Configuring the writer(s)

Loggers must have at least one writer.

A writer is an object that inherits from `Laminas\Log\Writer\AbstractWriter`. A writer's responsibility is to record log data to a storage backend. (from laminas-log's writer documentation)
A writer is an object that inherits from `Dot\Log\Writer\AbstractWriter`. A writer's responsibility is to record log data to a storage backend.

### Writing to a file (stream)

Expand All @@ -52,7 +47,7 @@ return [
'writers' => [
'FileWriter' => [
'name' => 'FileWriter',
'priority' => \Laminas\Log\Logger::ALERT, // this is equal to 1
'priority' => \Dot\Log\Manager\Logger::ALERT, // this is equal to 1
'options' => [
'stream' => __DIR__ . '/../../log/dk.log',
],
Expand All @@ -67,12 +62,7 @@ return [
* The `FileWriter` key is optional, otherwise the writers array would be enumerative instead of associative.
* The writer name key is a developer-provided name for that writer, the writer name key is **mandatory**.

The writer priority key is not affecting the errors that are written, it is a way to organize writers, for example:

1 - FILE
2 - SQL
3 - E-mail
It is the most important to write in the file, the sql or e-mail are more probably fail because the servers can be external and offline, the file is on the same server.
The writer priority key is not affecting the errors that are written, it is a way to organize writers.

The writer priority key is optional.

Expand All @@ -95,7 +85,7 @@ As per PSR-3 document.

The log levels are: emergency (0), alert (1), critical (2), error (3), warn (4), notice (5), info (6), debug (7) (in order of priority/importance)

Although the plain Logger in Laminas Log is not fully compatible with PSR-3, it provides a way to log all of these message types.
Although the plain Logger in Dot Log is not fully compatible with PSR-3, it provides a way to log all of these message types.

The following example has three file writers using filters:

Expand All @@ -113,15 +103,15 @@ return [
'writers' => [
'FileWriter' => [
'name' => 'FileWriter',
'priority' => \Laminas\Log\Logger::ALERT,
'priority' => \Dot\Log\Manager\Logger::ALERT,
'options' => [
'stream' => __DIR__ . '/../../log/dk.log',
'filters' => [
'allMessages' => [
'name' => 'priority',
'options' => [
'operator' => '>=',
'priority' => \Laminas\Log\Logger::EMERG,
'priority' => \Dot\Log\Manager\Logger::EMERG,
]
],
],
Expand All @@ -130,15 +120,15 @@ return [
// Only warnings
'OnlyWarningsWriter' => [
'name' => 'stream',
'priority' => \Laminas\Log\Logger::ALERT,
'priority' => \Dot\Log\Manager\Logger::ALERT,
'options' => [
'stream' => __DIR__ . '/../../log/warnings_only.log',
'filters' => [
'warningOnly' => [
'name' => 'priority',
'options' => [
'operator' => '==',
'priority' => \Laminas\Log\Logger::WARN,
'priority' => \Dot\Log\Manager\Logger::WARN,
],
],
],
Expand All @@ -147,7 +137,7 @@ return [
// Warnings and more important messages
'WarningOrHigherWriter' => [
'name' => 'stream',
'priority' => \Laminas\Log\Logger::ALERT,
'priority' => \Dot\Log\Manager\Logger::ALERT,
'options' => [
'stream' => __DIR__ . '/../../log/important_messages.log',
'filters' => [
Expand All @@ -157,7 +147,7 @@ return [
// note, the smaller the priority, the more important is the message
// 0 - emergency, 1 - alert, 2- error, 3 - warn. .etc
'operator' => '<=',
'priority' => \Laminas\Log\Logger::WARN,
'priority' => \Dot\Log\Manager\Logger::WARN,
],
],
],
Expand All @@ -178,26 +168,24 @@ The filter added on the first writer is equal to not setting a filter, but it wa

It was added opposite to the others just to demonstrate the other operator is also an option.

More examples on filters: https://docs.laminas.dev/laminas-log/filters/

## Formatting Messages

When using `dot-log` or `laminas-log`, the logged value is not limited to a string. Arrays can be logged as well.
When using `dot-log`, the logged value is not limited to a string. Arrays can be logged as well.

For a better readability, these arrays can be serialized.

Laminas Log provides String formatting, XML, JSON and FirePHP formatting.
Dot Log provides String formatting and JSON formatting.

The formatter accepts following parameters:

name - the formatter class (it must implement `Laminas\Log\Formatter\FormatterInterface`)
name - the formatter class (it must implement `Dot\Log\Formatter\FormatterInterface`)
options - options to pass to the formatter constructor if required

The following formats the message as JSON data:

```php
'formatter' => [
'name' => \Laminas\Log\Formatter\Json::class,
'name' => \Dot\Log\Manager\Formatter\Json::class,
],
```

Expand All @@ -220,7 +208,7 @@ return [
'writers' => [
'FileWriter' => [
'name' => 'FileWriter',
'priority' => \Laminas\Log\Logger::ALERT,
'priority' => \Dot\Log\Manager\Logger::ALERT,
'options' => [
'stream' => __DIR__ . '/../../log/dk.log',
// explicitly log all messages
Expand All @@ -229,12 +217,12 @@ return [
'name' => 'priority',
'options' => [
'operator' => '>=',
'priority' => \Laminas\Log\Logger::EMERG,
'priority' => \Dot\Log\Manager\Logger::EMERG,
],
],
],
'formatter' => [
'name' => \Laminas\Log\Formatter\Json::class,
'name' => \Dot\Log\Manager\Formatter\Json::class,
],
],
],
Expand All @@ -252,7 +240,7 @@ Basic usage of the logger is illustrated below.
The messages are written to see which logs are written and which are not written.

```php
use Laminas\Log\Logger;
use Dot\Log\Manager\Logger;
```

...
Expand All @@ -272,10 +260,4 @@ $logger->debug('7 debug');
$logger->log(Logger::NOTICE, 'NOTICE from log()');
```

Sources:

* https://docs.laminas.dev/laminas-log/
* https://docs.laminas.dev/laminas-log/writers/
* https://docs.laminas.dev/laminas-log/filters/

Extracted from [this article](https://www.dotkernel.com/dotkernel/logging-with-dot-log-in-mezzio-and-dotkernel)
4 changes: 1 addition & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@
],
"require": {
"php": "~8.1.0 || ~8.2.0 || ~8.3.0",
"psr/http-message": "^1.0 || ^2.0",
"laminas/laminas-servicemanager": "^3.22",
"laminas/laminas-log": "^2.17",
"dotkernel/dot-mail": "^4.1"
"laminas/laminas-validator": "^2.64"
},
"require-dev": {
"phpunit/phpunit": "^10.2",
Expand Down
10 changes: 10 additions & 0 deletions docs/book/v4/adding-config-provider.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Adding The Config Provider

* In `config/config.php` add an entry for the config provider `\Dot\Log\ConfigProvider::class`
* Make sure it is added before with the Application-Specific components, eg.:
* `\Frontend\App\ConfigProvider.php`
* `\Admin\App\ConfigProvider::class`
* `\MyProject\ConfigProvider::class` etc.
* Add the logger configuration in an autoload config file, e.g. you can create `config/autoload/logger.global.php`. Follow the `Configuring the writer(s)` chapter for a simple working example.

Note: `Dot\Log\ConfigProvider` has an abstract factory `LoggerAbstractServiceFactory::class` which corresponds to the alias, not the class name. Instead of requesting `Dot\Log\Logger::class` from the container, use `dot-log.my_logger`.
40 changes: 40 additions & 0 deletions docs/book/v4/configuring-writer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Configuring the writer(s)

Loggers must have at least one writer.

A writer is an object that inherits from `Dot\Log\Writer\AbstractWriter`. A writer's responsibility is to record log data to a storage backend.

## Writing to a file (stream)

You can separate logs into multiple files using writers and filters. For example *warnings.log*, *errors.log*, *all_messages.log*.

The following is the simplest example to write all log messages to `/log/dk.log`

```php
return [
'dot_log' => [
'loggers' => [
'my_logger' => [
'writers' => [
'FileWriter' => [
'name' => 'FileWriter',
'priority' => \Dot\Log\Manager\Logger::ALERT, // this is equal to 1
'options' => [
'stream' => __DIR__ . '/../../log/dk.log',
],
],
],
]
],
],
];
```

* The `FileWriter` key is optional, otherwise the writers array would be enumerative instead of associative.
* The `name` key is a developer-provided name for that writer, the writer name key is **mandatory**.

The `priority` key does not affect the errors that are written. It is a way to organize writers.

The `priority` key is optional.

The key `stream` is required only if writing into streams/files.
41 changes: 41 additions & 0 deletions docs/book/v4/example-with-formatter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Example with formatter

* The log is used through `dot-log`
* The logger name is `my_logger`
* It writes to file: `log/dk.log`
* It is configured to explicitly write all messages
* The messages are formatted as JSON

```php
<?php
return [
'dot_log' => [
'loggers' => [
'my_logger' => [
'writers' => [
'FileWriter' => [
'name' => 'FileWriter',
'priority' => \Dot\Log\Manager\Logger::ALERT,
'options' => [
'stream' => __DIR__ . '/../../log/dk.log',
// explicitly log all messages
'filters' => [
'allMessages' => [
'name' => 'priority',
'options' => [
'operator' => '>=',
'priority' => \Dot\Log\Manager\Logger::EMERG,
],
],
],
'formatter' => [
'name' => \Dot\Log\Manager\Formatter\Json::class,
],
],
],
],
],
],
],
];
```
95 changes: 95 additions & 0 deletions docs/book/v4/filtering-log-messages.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# Filtering log messages

The following conforms to the `PSR-3: Logger Interface` document.

The log levels are in order of priority/importance:

* emergency (0)
* alert (1)
* critical (2)
* error (3)
* warning (4)
* notice (5)
* info (6)
* debug (7)

Although the plain Logger in `dot-log` is not fully compatible with PSR-3, it provides a way to log all of these message types.

The following example has three file writers using filters:

* First Example: `FileWriter` - All messages are logged in `/log/dk.log`
* Second Example: `OnlyWarningsWriter` - Only warnings are logged in `/log/warnings.log`
* Third Example: `WarningOrHigherWriter` - All important messages (`warnings` or critical) are logged in `/log/important_messages.log`

```php
<?php

return [
'dot_log' => [
'loggers' => [
'my_logger' => [
'writers' => [
'FileWriter' => [
'name' => 'FileWriter',
'priority' => \Dot\Log\Manager\Logger::ALERT,
'options' => [
'stream' => __DIR__ . '/../../log/dk.log',
'filters' => [
'allMessages' => [
'name' => 'priority',
'options' => [
'operator' => '>=',
'priority' => \Dot\Log\Manager\Logger::EMERG,
]
],
],
],
],
// Only warnings
'OnlyWarningsWriter' => [
'name' => 'stream',
'priority' => \Dot\Log\Manager\Logger::ALERT,
'options' => [
'stream' => __DIR__ . '/../../log/warnings_only.log',
'filters' => [
'warningOnly' => [
'name' => 'priority',
'options' => [
'operator' => '==',
'priority' => \Dot\Log\Manager\Logger::WARN,
],
],
],
],
],
// Warnings and more important messages
'WarningOrHigherWriter' => [
'name' => 'stream',
'priority' => \Dot\Log\Manager\Logger::ALERT,
'options' => [
'stream' => __DIR__ . '/../../log/important_messages.log',
'filters' => [
'importantMessages' => [
'name' => 'priority',
'options' => [
// note, the smaller the priority, the more important is the message
// 0 - emergency, 1 - alert, 2- error, 3 - warn etc.
'operator' => '<=',
'priority' => \Dot\Log\Manager\Logger::WARN,
],
],
],
],
],
],
],
],
],
];
```

As in the writer configuration, the developer can optionally use keys for associating the filters with a name.

IMPORTANT NOTE: the operator for more important messages is `<=`, this is because the number representation is smaller for a more important message type.

The filter added on the first writer is equivalent to not setting a filter, but it was added to illustrate the usage of the operator to explicitly allow all messages.
Loading

0 comments on commit 4ccfaab

Please sign in to comment.