Skip to content

Commit 4938a9b

Browse files
Updated the plugin with a lot of new features!
Signed-off-by: Johannes Tegnér <johannes@jitesoft.com>
1 parent 2486286 commit 4938a9b

File tree

6 files changed

+576
-84
lines changed

6 files changed

+576
-84
lines changed

README.md

Lines changed: 80 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,94 @@
11
# Wp-logger
22

3-
### An actual logger for WordPress.
3+
## An actual logger for WordPress.
44

5-
Plugin which overrides the default php error handling with a step in which a PSR logger is
6-
invoked to output logs in other formats than the default php error log.
7-
Further, the plugin contains a `GlobalLogger` class with a static `logger()` method to allow
8-
user defined logging to be made with the same configuration.
9-
Depending on the WP_ENV define or env variable, different log levels will be enabled.
5+
This plugin does two things. It piggybacks on the internal php logger (via `set_error_handler`) and
6+
it creates a PSR logger for you to use!
107

11-
Intended to be used as a MU-plugin.
8+
### More in-depth
129

13-
## Installation
10+
When the plugin have loaded completely in WordPress, it will fire the `jitesoft_logger_loaded` action,
11+
the callback will be passed the global logger object.
12+
If you _know_ that the plugin is loaded, you can as well use the `jitesoft('logger')` function to request the logger
13+
instance. In case it is not yet loaded, the function will return null:
1414

15-
Use a composer based WordPress installation and require this plugin.
15+
```php
16+
$logger = jitesoft('logger'); // null if not lodaed
17+
add_action('jitesoft_logger_loaded', static fn($logger) => $logger); // will not be null when loaded.
18+
```
19+
20+
_Deprecation notice, the following will be removed in next major version_
21+
22+
It's also possible to fetch the logger through the `GlobalLogger` class through the `logger()` method:
23+
24+
```php
25+
Jitesoft\WordPress\Plugins\Logger\GlobalLogger;
26+
27+
$logger = GlobalLogger::logger();
28+
```
1629

17-
## Usage
30+
### Auto-loading
1831

19-
To use the logger in your own code, just call the `Jitesoft\Wordpress\Plugins\Logger\GlobalLogger::logger()` object,
20-
which will return a PSR logger to use!
32+
The plugin requires auto-loading to function properly. Without the autoloader, the plugin won't be able to find
33+
the libraries it uses and hence will not create a logger.
34+
35+
## Configuring
36+
37+
The plugin is currently configured via environment variables or defines. The values must be
38+
defined _before_ loading the plugin, so the wp-config.php file or actual environment variables are preferable
39+
places to put those.
40+
41+
`WP_LOGGER_OVERRIDE` is default set to override, can be changed to `disable` to _not_ override the internal
42+
php logger.
2143

22-
The PSR Logger in the implementation currently only uses a STD logger (stdout and stderr) but
23-
it's a "multi logger" (see [jitesoft/logger](https://packagist.org/packages/jitesoft/loggers) for more information)
24-
which allow you to add more loggers if needed.
44+
`WP_ENV` sets the default logging level on the logger:
45+
46+
* `production`: errors and above
47+
* `staging`: info and above
48+
* `development`: debug and above
49+
50+
In case you wish to change the logging level manually, you can set the `WP_LOGGER_LEVEL` to an appropriate level,
51+
the following are accepted: `debug`, `notice`, `info`, `warning`, `error`, `critical`, `alert`, `emergency`.
52+
53+
You can also change the log level by fireing the `jitesoft_log_level` hook with a single string parameter
54+
with a value of the above log levels.
55+
56+
### Formatting
57+
58+
Currently, the logger supports two default types: `json`, `stdout`.
59+
They are possible to set with the `WP_LOGGER_FORMAT` variable (`stdout` is default).
60+
Both of the types will output to the stdout/stderr channel (the terminal) while they
61+
will produce either clear text logs or json formatted logs.
62+
63+
If you wish to change the loggers on the logging object, it's possible to do so by querying the logger and
64+
add (or remove) loggers from it. Multiple loggers can be added:
65+
66+
```php
67+
jitesoft('logger')->removeLogger('stdout');
68+
jitesoft('logger')->removeLogger('json');
69+
70+
jitesoft('logger')->addLogger(new MyPsrLogger(), 'myLogger');
71+
```
72+
73+
Or directly in the hook:
74+
75+
```php
76+
add_action('jitesoft_logger_loaded', static function($logger) {
77+
$logger->removeLogger('stdout');
78+
$logger->removeLogger('json');
79+
$logger->addLogger(new MyPsrLogger(), 'myLogger');
80+
});
81+
```
82+
83+
## Installation
84+
85+
Use a composer based WordPress installation and require this plugin.
2586

26-
By setting the `WP_LOGGER_FORMAT` environment variable to `json`, the logger will use json output instead
27-
of standard text.
87+
## Loggers
2888

29-
In future versions, more loggers might be added.
89+
If you wish to add more loggers, the package depends on the [`jitesoft/loggers`](https://packagist.org/packages/jitesoft/loggers)
90+
php package, which contains multiple different loggers.
91+
If you wish to use your own loggers, the logger must implement the [PSR-3 logger](https://www.php-fig.org/psr/psr-3/) interface.
3092

3193
## License
3294

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
"composer/installers": "^1.11",
1616
"ext-json": "*",
1717
"jitesoft/loggers": ">=2.3.0",
18-
"php": ">=7.4.2"
18+
"php": ">=7.4.2",
19+
"jitesoft/wp-base": "~1.0.0"
1920
},
2021
"require-dev": {
2122
"roave/security-advisories": "dev-latest"

0 commit comments

Comments
 (0)