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

implemented PSR3 #68

Merged
merged 5 commits into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
40 changes: 20 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ return [
'writers' => [
'FileWriter' => [
'name' => 'FileWriter',
'priority' => \Dot\Log\Manager\Logger::ALERT, // this is equal to 1
'level' => \Dot\Log\Manager\Logger::ALERT, // this is equal to 1
alexmerlin marked this conversation as resolved.
Show resolved Hide resolved
'options' => [
'stream' => __DIR__ . '/../../log/dk.log',
],
Expand All @@ -58,9 +58,9 @@ 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.
The writer level key is not affecting the errors that are written, it is a way to organize writers.

The writer priority key is optional.
The writer level key is optional.

To write into a file the key stream must be present in the writer options array. This is required only if writing into streams/files.

Expand All @@ -79,7 +79,7 @@ The full list of format specifiers is available [here](https://www.php.net/manua

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)
The log levels are: emergency (0), alert (1), critical (2), error (3), warn (4), notice (5), info (6), debug (7) (in order of level/importance)

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.
alexmerlin marked this conversation as resolved.
Show resolved Hide resolved

Expand All @@ -98,33 +98,33 @@ return [
'my_logger' => [
'writers' => [
'FileWriter' => [
'name' => 'FileWriter',
'priority' => \Dot\Log\Manager\Logger::ALERT,
'name' => 'FileWriter',
'level' => \Dot\Log\Manager\Logger::ALERT,
'options' => [
'stream' => __DIR__ . '/../../log/dk.log',
'filters' => [
'allMessages' => [
'name' => 'priority',
'name' => 'level',
'options' => [
'operator' => '>=',
'priority' => \Dot\Log\Manager\Logger::EMERG,
'level' => \Dot\Log\Manager\Logger::EMERG,
]
],
],
],
],
// Only warnings
'OnlyWarningsWriter' => [
'name' => 'stream',
'priority' => \Dot\Log\Manager\Logger::ALERT,
'name' => 'stream',
'level' => \Dot\Log\Manager\Logger::ALERT,
'options' => [
'stream' => __DIR__ . '/../../log/warnings_only.log',
'filters' => [
'warningOnly' => [
'name' => 'priority',
'name' => 'level',
'options' => [
'operator' => '==',
'priority' => \Dot\Log\Manager\Logger::WARN,
'level' => \Dot\Log\Manager\Logger::WARN,
],
],
],
Expand All @@ -133,17 +133,17 @@ return [
// Warnings and more important messages
'WarningOrHigherWriter' => [
'name' => 'stream',
'priority' => \Dot\Log\Manager\Logger::ALERT,
'level' => \Dot\Log\Manager\Logger::ALERT,
'options' => [
'stream' => __DIR__ . '/../../log/important_messages.log',
'filters' => [
'importantMessages' => [
'name' => 'priority',
'name' => 'level',
'options' => [
// note, the smaller the priority, the more important is the message
// note, the smaller the level, the more important is the message
// 0 - emergency, 1 - alert, 2- error, 3 - warn. .etc
'operator' => '<=',
'priority' => \Dot\Log\Manager\Logger::WARN,
'level' => \Dot\Log\Manager\Logger::WARN,
],
],
],
Expand Down Expand Up @@ -203,17 +203,17 @@ return [
'my_logger' => [
'writers' => [
'FileWriter' => [
'name' => 'FileWriter',
'priority' => \Dot\Log\Manager\Logger::ALERT,
'name' => 'FileWriter',
'level' => \Dot\Log\Manager\Logger::ALERT,
'options' => [
'stream' => __DIR__ . '/../../log/dk.log',
// explicitly log all messages
'filters' => [
'allMessages' => [
'name' => 'priority',
'name' => 'level',
'options' => [
'operator' => '>=',
'priority' => \Dot\Log\Manager\Logger::EMERG,
'level' => \Dot\Log\Manager\Logger::EMERG,
],
],
],
Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
{
"name": "dotkernel/dot-log",
"type": "library",
"description": "Dotkernel log component extending and customizing laminas-log",
"description": "Dotkernel log component implementing PSR-3",
"license": "MIT",
"homepage": "https://github.com/dotkernel/dot-log",
"keywords": [
"log",
"logging",
"services",
"mezzio",
alexmerlin marked this conversation as resolved.
Show resolved Hide resolved
"laminas",
"laminas-log"
"laminas"
alexmerlin marked this conversation as resolved.
Show resolved Hide resolved
alexmerlin marked this conversation as resolved.
Show resolved Hide resolved
],
"authors": [
{
Expand All @@ -21,7 +20,8 @@
"require": {
"php": "~8.1.0 || ~8.2.0 || ~8.3.0 || ~8.4.0",
"laminas/laminas-servicemanager": "^4.0",
"laminas/laminas-validator": "^3.0"
"laminas/laminas-validator": "^3.0",
"psr/log": "^3.0.2"
},
"require-dev": {
"laminas/laminas-coding-standard": "^3.0",
Expand Down
2 changes: 0 additions & 2 deletions docs/book/v4/overview.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# Overview

Robust, composite logger with filtering, formatting, and PSR-3 support.

> dot-log is a wrapper on top of [laminas-log](https://github.com/laminas/laminas-log)
2 changes: 1 addition & 1 deletion docs/book/v4/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,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 Dot\Log\Manager\Logger;
use Dot\Log\Logger;
```

...
Expand Down
11 changes: 11 additions & 0 deletions docs/book/v5/adding-config-provider.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# 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.:
alexmerlin marked this conversation as resolved.
Show resolved Hide resolved
* `\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.
alexmerlin marked this conversation as resolved.
Show resolved Hide resolved

> `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`.
43 changes: 43 additions & 0 deletions docs/book/v5/configuring-writer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# 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',
'level' => \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 `level` key does not affect the errors that are written.
It is a way to organize writers.

The `level` key is optional.

The key `stream` is required only if writing into streams/files.
41 changes: 41 additions & 0 deletions docs/book/v5/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',
'level' => \Dot\Log\Manager\Logger::ALERT,
'options' => [
'stream' => __DIR__ . '/../../log/dk.log',
// explicitly log all messages
'filters' => [
'allMessages' => [
'name' => 'level',
'options' => [
'operator' => '>=',
'level' => \Dot\Log\Manager\Logger::EMERG,
],
],
],
'formatter' => [
'name' => \Dot\Log\Manager\Formatter\Json::class,
],
],
],
],
],
],
],
];
```
95 changes: 95 additions & 0 deletions docs/book/v5/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 level/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',
'level' => \Dot\Log\Manager\Logger::ALERT,
'options' => [
'stream' => __DIR__ . '/../../log/dk.log',
'filters' => [
'allMessages' => [
'name' => 'level',
'options' => [
'operator' => '>=',
'level' => \Dot\Log\Manager\Logger::EMERG,
]
],
],
],
],
// Only warnings
'OnlyWarningsWriter' => [
'name' => 'stream',
'level' => \Dot\Log\Manager\Logger::ALERT,
'options' => [
'stream' => __DIR__ . '/../../log/warnings_only.log',
'filters' => [
'warningOnly' => [
'name' => 'level',
'options' => [
'operator' => '==',
'level' => \Dot\Log\Manager\Logger::WARN,
],
],
],
],
],
// Warnings and more important messages
'WarningOrHigherWriter' => [
'name' => 'stream',
'level' => \Dot\Log\Manager\Logger::ALERT,
'options' => [
'stream' => __DIR__ . '/../../log/important_messages.log',
'filters' => [
'importantMessages' => [
'name' => 'level',
'options' => [
// note, the smaller the level, the more important is the message
// 0 - emergency, 1 - alert, 2- error, 3 - warn etc.
'operator' => '<=',
'level' => \Dot\Log\Manager\Logger::WARN,
],
],
],
],
],
],
],
],
],
];
```

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

> 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.
19 changes: 19 additions & 0 deletions docs/book/v5/formatting-messages.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Formatting Messages

When using `dot-log`, the logged value is not limited to a string.
Arrays can be logged as well.
For better readability, these arrays can be serialized.
DotLog provides String and JSON formatting.

The formatter accepts following parameters:

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

The following snippet formats the message as JSON data:

```php
'formatter' => [
'name' => \Dot\Log\Formatter\Json::class,
],
```
12 changes: 12 additions & 0 deletions docs/book/v5/grouping-log-files-by-date.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Grouping log files by date

By default, logs will be written to the same file: `log/dk.log`.

Optionally, you can use date format specifiers wrapped between curly braces in your FileWriter's `stream` option to automatically group your logs by day, week, month, year etc.

Examples:

* `log/dk-{Y}-{m}-{d}.log` will create a new log file each day (eg: log/dk-2021-01-01.log)
* `log/dk-{Y}-{W}.log` will create a new log file each week (eg: log/dk-2021-10.log)

The full list of format specifiers is available in the [PHP docs for datetime formatting](https://www.php.net/manual/en/datetime.format.php).
3 changes: 3 additions & 0 deletions docs/book/v5/overview.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Overview

Robust, composite logger with filtering, formatting, and PSR-3 support.
Loading