|
1 | 1 | # Wp-logger
|
2 | 2 |
|
3 |
| -### An actual logger for WordPress. |
| 3 | +## An actual logger for WordPress. |
4 | 4 |
|
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! |
10 | 7 |
|
11 |
| -Intended to be used as a MU-plugin. |
| 8 | +### More in-depth |
12 | 9 |
|
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: |
14 | 14 |
|
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 | +``` |
16 | 29 |
|
17 |
| -## Usage |
| 30 | +### Auto-loading |
18 | 31 |
|
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. |
21 | 43 |
|
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. |
25 | 86 |
|
26 |
| -By setting the `WP_LOGGER_FORMAT` environment variable to `json`, the logger will use json output instead |
27 |
| -of standard text. |
| 87 | +## Loggers |
28 | 88 |
|
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. |
30 | 92 |
|
31 | 93 | ## License
|
32 | 94 |
|
|
0 commit comments